ResultSet rs = toUpdate.executeQuery("select mgxm,mgxsh from mgxfx_m"); while(rs.next()){ n12=rs.getString("mgxm"); n13=rs.getString("mgxsh"); toUpdate.executeUpdate("insert into mgxjshshjchl_m(mgxmch,mgxxsh) values('"+n12+"','"+n13+"')");
} 这段代码为什么在while循环第二次时会出现ResultSet is closed的错误? 谢谢各位好心人!
------解决方案-------------------- 看看你的 mgxfx_m 表里面有多少mgxm,mgxsh属性值,如果都只有1个,当然会关闭!
------解决方案-------------------- 我也遇到过这种情况,如果实在没得办法,你可以另存一个集合,然后循环集合来执行数据插入。
------解决方案-------------------- 一个stmt多个rs进行操作. 那么从stmt得到的rs1,必须马上操作此rs1后,才能去得到另外的rs2,再对rs2操作. 不能互相交替使用,会引起rs已经关闭错误. 错误的代码如下: stmt=conn.createStatement(); rs=stmt.executeQuery("select * from t1"); rst=stmt.executeQuery("select * from t2"); rs.last();//由于执行了rst=stmt.executeQuery(sql_a);rs就会被关闭掉!所以程序执行到此会提示ResultSet已经关闭.错误信息为:java.sql.SQLException: Operation not allowed after ResultSet closed rst.last();
正确的代码:
stmt=conn.createStatement(); rs=stmt.executeQuery("select * from t1"); rs.last();//对rs的操作应马上操作,操作完后再从数据库得到rst,再对rst操作 rst=stmt.executeQuery("select * from t2"); rst.last();
原因是: The object used for executing a static SQL statement and returning the results it produces. By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists.