首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

ResultSet关闭有关问题

2013-06-26 
ResultSet关闭问题做一些处理需要从一个表根据条件查出一个ID然后通过这个ID去查另一个表里的一些值privat

ResultSet关闭问题
做一些处理
需要从一个表根据条件查出一个ID
然后通过这个ID去查另一个表里的一些值


private void updateSms() {     
Log.getInstance().printInfo("", "正在更新");     
Connection conn = null;     
Statement stat = null;
ResultSet rsRecord = null;
//全部
ResultSet rsMoSmsAll = null;
        int nArg = 0;
int nArg1 = 0;
int nArg2 = 0;
String ctrl=null;
try {
conn = DataBaseManage.getInstance().getConnection();
stat = conn.createStatement();
conn.setAutoCommit(false);
rsRecord = stat.executeQuery( "select SendID from table_a where Flag=1" );
while (rsRecord.next()) {
nArg = stat.executeUpdate( "update table_b set PushStatus = 5 " +"where SendID='"+rsRecord.getString(1)+"'" );
        rsMoSmsAll=stat.executeQuery( "select Number from table_b " +"where SendID='"+rsRecord.getString(1)+"' and PushStatus=5" );
        }
conn.commit();
} catch (Exception e) {
        ExceptionHeading.getException(this.getClass().getName(), e);
}finally{
        try {
        rsRecord.close();
rsMoSmsAll.close();
stat.close();
conn.close();
} catch (SQLException e) {
ExceptionHeading.getException(this.getClass().getName(), e);
}
}




现在的问题是如果rsRecord没查到值的话,在finally时关闭rsMoSmsAll报空指针错误,因为rsMoSmsAll没进行初始化,怎么能初始化rsMoSmsAll呢? Java mysql? 异常 空指针
[解决办法]
不知道为什么把这么多的操作都丢到一个对RS的遍历里面,建议是先把结果拿出来,然后做之后的操作。对于你说的这个问题在finally的时候判断是否为空就好了,空的话就不需要关闭
[解决办法]
引用:
不知道为什么把这么多的操作都丢到一个对RS的遍历里面,建议是先把结果拿出来,然后做之后的操作。对于你说的这个问题在finally的时候判断是否为空就好了,空的话就不需要关闭


判空 乃是正解~~
[解决办法]

if (rsMoSmsAll != null) {
rsMoSmsAll.close();
}

热点排行