9月15日培训日记
来源:互联网 发布:保险大数据时代 编辑:程序博客网 时间:2024/05/02 03:05
根据李杰将建立数据库连接的作业采用spring bean的方式来完成的,讲解了如下知识:
1.BeanFactoryPostProcessor,PropertyPlaceholderConfigurer(把properties文件中的数据拿来换掉配置文件中的${})
2.BeanPostProcessor的作用,BeanPostProcessor是监听其他bean的初始化过程,并且只能原型的bean。
3.<Bean>元素的init-method属性。
总结流程:
1.读取配置文件
2.创建Bean实例
3.调用BeanFactoryPostProcessor
4.为Bean赋属性值
5.调用BeanPostProcessor(只有ApplicationContext才会自动调用,BeanFactory不自动调用)
5.1 postProcessBeforeInitialization(对初始化方法产生影响的代码)
5.2 <Bean>元素的init-method属性
5.3 postProcessAfterInitialization(要利用初始化方法结果的代码)
王泽佑写了个BeanFactoryPostProcessor,结果spring没有执行其中的方法?怎么回事?还是ApplicationContext与BeanFactory的问题。
看帮助:Connection,statement,ResultSet被垃圾回收后,都会自动关闭资源。Statement关闭会导致Resultset关闭。
要写就一块写(拧螺母故事和造坦克的故事):
rs.close();
rs = null;
点评王涛写的验证用户登录的程序:
import java.io.*;
import java.sql.*;
public class ErrorClass
{
public static void main(String[] args)throws Exception
{
ResultSet rs=null;
Connection con=null;
Statement sm = null;
BufferedReader br = null;
Class.forName("com.mysql.jdbc.Driver");
try{
con = DriverManager.getConnection("jdbc:mysql://localhost/itcast","root","");
sm = con.createStatement(ResultSet.CONCUR_UPDATABLE,ResultSet.TYPE_SCROLL_INSENSITIVE);
br = new BufferedReader(new InputStreamReader(System.in));
rs = sm.executeQuery("select student.name,student.psw from student");
System.out.println("请输入用户名,按回车结束");
String inuse = br.readLine();
System.out.println("请输入密码,按回车结束");
String inpsw = br.readLine();
br.close();
//Boolean bool = false;
while(rs.next())
{
if(inuse.equals(rs.getString(2)) && inpsw.equals(rs.getString(3)))
{
System.out.println("用户名和密码正确");
//bool = true;
break;
}
}
if(rs.isAfterLast())
{
System.out.println("用户名和密码错误");
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
rs.close();
con.close();
sm.close();
}
}
}
问题:
1.变量命名应尽量与大多数人的习惯一致,例如,con改为cn,sm改为stmt
2.判断找到记录的方式还可以改善,while条件中只负责判断是否找到,找到和没找到的打印信息应在while之后,并且rs.isAfterLast()是否可靠也值得怀疑。
3.查询出所有用户后再比较判断用户是否存在,这是一个非常低劣的做法,如果记录较多,运行效率会很低。
4.最后关闭结果集,命令和连接的代码写得太随意,太马虎!
通过王涛写的这个程序,发现学员当前更需要动手练习,然后由老师指正学员程序中的问题。我们应多布置些有实际意义和解决实际问题的练习题,然后对学员完成的作业进行点评。课后调查了一下,学员们现在都急切需要留出一段时间来总结和消化前面所讲的知识。
- 9月15日培训日记
- 9月9日培训日记
- 9月2日培训日记
- 9月3日培训日记
- 9月4日培训日记
- 9月5日培训日记
- 9月7日培训日记
- 9月8日培训日记
- 9月10日培训日记
- 9月11日培训日记
- 9月12日培训日记
- 9月14日培训日记
- 9月16日培训日记
- 9月17日培训日记
- 9月18日培训日记
- 9月19日培训日记
- 9月21日培训日记
- 9月23日培训日记
- 家里的服务器遭到攻击
- bat批处理文件的能力
- 软件试用协议书
- #pragma 预处理指令解析
- 打包,并自动安装SQL数据库
- 9月15日培训日记
- 在Windows中显示Linux的桌面
- 如何减小可执行文件的大小 - minimize your excutables
- KMP字符串模式匹配详解
- 与凌小宁交流--PM要务
- 全文索引完全图解
- C 语言面试题 小集合
- 我的开始
- CSS 控件适配器工具包新版发布