好久不写简书了,今天遇到一个挺奇怪的问题,如下:
ResultSet rs = stmt.executeQuery("select * from xxx");
ResultSet rs2 = stmt.executeQuery("select * from xxx");
int countWb =0;
while (rs.next()) {
countWb = rs.getRow();
}
while(rs2.next()){
tradeDate=rs2.getString("xxx");
}
执行到while(rs2.next()){ 的时候就报Exception in thread "main" java.sql.SQLException: Statement has already closed.
中间没有关闭Statement ,
尝试修改代码顺序如下:
ResultSet rs = stmt.executeQuery("select * from xxx");
int countWb =0;
while (rs.next()) {
countWb = rs.getRow();
}
ResultSet rs2 = stmt.executeQuery("select * from xxx");
while(rs2.next()){
tradeDate=rs2.getString("xxx");
}
跑通成功,同一个stmt,执行完一次查询后,会关掉查询的入口,rs2.next()就会报错,所以rs2.next()运行之前在打开一次查询入口,即执行ResultSet rs2 = stmt.executeQuery("select * from xxx");
就可以了。
网友评论