Posted on 2011-12-05 22:14
哈希 阅读(1223)
评论(0) 编辑 收藏 所属分类:
Js and Jquery 常用总结
好久没用List去保存一个JDBC查询结果,查询出来的ResultSet只执行了一次,然后就报NullPointException。明明数据库中有数据,就是执行不了第二次,一到把对象添加到list上面就直接报异常,郁闷了半天。
- public List<Terminal> getTerm(String user){
- List<Terminal> termlist= null;
-
-
- try {
- con = DBPool.getPool().getConnection();
- ps =con.prepareStatement("select * from gps_termcodetable where kh_user=?");
- ps.setString(1, user);
- rs = ps.executeQuery();
- while(rs.next()){
- Terminal term = new Terminal();
- String number = rs.getString("hm_number");
- String nick = rs.getString("mc_nick");
- term.setZhNumber(number);
- term.setLisence(rs.getString("mc_nick"));
- termlist.add(term);
- }
- rs.close();
- ps.close();
- con.close();
- } catch (SQLException ex) {
- Logger.getLogger(GetTerminal.class.getName()).log(Level.SEVERE, null, ex);
- }
- return termlist;
- }
public List<Terminal> getTerm(String user){
List<Terminal> termlist= null;//这里出现问题,没有new一个List
//改为List<Terminal> termlist= new ArrayList();,
//new一个ArrayList就没事了,日,以前初学的时候也得过这种问题...汗
try {
con = DBPool.getPool().getConnection();
ps =con.prepareStatement("select * from gps_termcodetable where kh_user=?");
ps.setString(1, user);
rs = ps.executeQuery();
while(rs.next()){
Terminal term = new Terminal();
String number = rs.getString("hm_number");
String nick = rs.getString("mc_nick");
term.setZhNumber(number);
term.setLisence(rs.getString("mc_nick"));
termlist.add(term);
}
rs.close();
ps.close();
con.close();
} catch (SQLException ex) {
Logger.getLogger(GetTerminal.class.getName()).log(Level.SEVERE, null, ex);
}
return termlist;
}
后面才发现没有new一个ArrayList,郁闷了,还犯这种错误,久了没用也会生疏