|
![]() |
名片设计 CorelDRAW Illustrator AuotoCAD Painter 其他软件 Photoshop Fireworks Flash |
|
一个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. 一个stmt最好对应一个rs, 假如用一个时间内用一个stmt打开两个rs同时操作,会出现这种情况.所以解决此类问题:1.就多创建几个stmt,一个stmt对应一个rs;2.若用一个stmt对应多个rs的话,那只能得到一个rs后就操作,处理完第一个rs后再处理其他的,如上"准确代码".多个stmt对应各自的rs.stmt=conn.createStatement();stmt2=conn.createStatement();rs=stmt.executeQuery("select * from t1");rst=stmt2.executeQuery("select * from t2");rs.last();rst.last(); 返回类别: 教程 上一教程: Mini Java编译器(三) 下一教程: 《Java编程思想》(第二版)第08章:接口与内隐类 您可以阅读与"一个stmt多个rs进行操作引起的ResultSet已经关闭错误"相关的教程: · 一个操作数据库的Java Bean…… · Web开发中防止浏览器的刷新键引起系统操作重复提交 · 果果来看(一个封装了基本JDBC操作的类) · 一个进行Base64编码的类。 · 对Reader进行token的类,可以访入多个split进行解析 |
![]() ![]() |
快精灵印艺坊 版权所有 |
首页![]() ![]() ![]() ![]() ![]() ![]() ![]() |