SQLExecption:Operation not allowed after ResultSet closed解决办法

简介:

原网址:http://blog.csdn.net/sku0923/article/details/1722370

一个stmt多个rs进行操作引起的ResultSet已经关闭错误

一个stmt多个rs进行操作. 那么从stmt得到的rs1,必须马上操作此rs1后,才能去得到另外的rs2,再对rs2操作. 不能互相交替使用,会引起rs已经关闭错误错误的代码如下:  stmt=conn.createStatement();  

rs=stmt.executeQuery("select * from t1");  

rst=stmt.executeQuery("select * from t2");

rst.last();

 rs.last();//由于执行了rst=stmt.executeQuery(sql_a);rs就会被关闭掉!所以程序执行到此会提示ResultSet

已经关闭.错误信息为:java.sql.SQLException: Operation not allowed fter ResultSet closed  

正确的代码:
 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();

目录
相关文章
|
6月前
|
Web App开发 前端开发
【前端异常】Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.
【前端异常】Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.
457 0
|
4月前
|
自然语言处理 数据库
Expected one result (or null) to be returned by selectOne(), but found: 2
Expected one result (or null) to be returned by selectOne(), but found: 2
35 0
|
10月前
Java.sql.SQLException: Illegal operation on empty result set.
Java.sql.SQLException: Illegal operation on empty result set.
109 0
|
Java 数据库连接 mybatis
A query was run and no Result Maps were found for the Mapped Statement
A query was run and no Result Maps were found for the Mapped Statement
160 0
|
SQL 关系型数据库 MySQL
MySQL - Expected one result (or null) to be returned by selectOne(), but found: 2
MySQL - Expected one result (or null) to be returned by selectOne(), but found: 2
505 0
|
数据库
Multiple Server Query Execution报The result set could not be merged..
在SQL Server中使用Multiple Server Query Execution这个功能做数据库维护或脚本发布时非常方便,昨天由于磁盘空间原因,删除清理了大量的软件和组件,结果导致SSMS客户端出了问题,重装过后,使用Multiple Server Query Execution时,出现了...
954 0
|
Java Shell Linux
[ERROR] Result: { execute timeout }异常解决
记一次AlibabaCloudToolKit插件使用过程中云助手执行脚本时,脚本超时的异常的解决方案.
10492 0