JDBC编程中,结果集为空的判断方法

JDBC编程中,常常会通过ResultSet rs来获得结果集,判断结果集是否为空往往不能直接判断rs == null, 所以一个经常使用的方法如下

try{

conn = DB.getConn();

String sql = "Select count(*) from category where pid = " + id;

rs = DB.executeQury(conn, sql);

rs.next();

int count = rs.getInt(1);

//System.out.println("执行到这里了"+""+id+""+count);

if(count<=0){

stmt = DB.getStmt(conn);

String update = "update category set isleaf=0 where id="+id;

//System.out.println("已经更新"+update);

stmt.executeUpdate(update);

}

}catch(Exception e){

e.printStackTrace();

}finally{

DB.closeRs(rs);

DB.closeStmt(stmt);

DB.closeConn(conn);

}

通过取得结果集数来实现进一步的操作,使用count*效率也更高一些

你可能感兴趣的:(J2EE学习)