原网址: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();

SQLExecption:Operation not allowed after ResultSet closed解决办法的更多相关文章

  1. java.sql.SQLException: Operation not allowed after ResultSet closed

    转自:http://blog.csdn.net/hellobobantang/article/details/7173622 java.sql.SQLException: Operation not ...

  2. jsp操作MySQL时报错:Operation not allowed after ResultSet closed

    一个stmt对多个rs进行操作引起的ResultSet关闭的错误 解决办法:创建新的stmt,一个rs对应一个stmt

  3. Operation not allowed after ResultSet closed--操作mysql数据库

    一个stmt多个rs进行操作.那么从stmt得到的rs1,必须马上操作此rs1后,才能去得到另外的rs2,再对rs2操作.不能互相交替使用,会引起rs已经关闭错误——Operation not all ...

  4. PHP内存溢出Allowed memory size of 解决办法

    PHP内存溢出Allowed memory size of 解决办法 博客分类: php   ============================Allowed memory size of  x ...

  5. ASP.Net WebAPI HttpDelete/PUT方法运行或发布到生产服务器上后出现405(Method Not Allowed)错误的解决办法

    原文:ASP.Net WebAPI HttpDelete/PUT方法运行或发布到生产服务器上后出现405(Method Not Allowed)错误的解决办法 本文只是个人的理解和学习记录,如果觉得本 ...

  6. com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed. 解决

    ERROR - No operations allowed after connection closed. 2011-12-07 11:36:09 - ERROR - query failed or ...

  7. 懒加载session 无法打开 no session or session was closed 解决办法(完美解决)

           首先说明一下,hibernate的延迟加载特性(lazy).所谓的延迟加载就是当真正需要查询数据时才执行数据加载操作.因为hibernate当中支持实体对象,外键会与实体对象关联起来.如 ...

  8. java.io.IOException: Stream closed解决办法

    1.出现这个bug的大体逻辑代码如下: private static void findMovieId() throws Exception { File resultFile = new File( ...

  9. The database could not be exclusively locked to perform the operation(SQL Server 5030错误解决办法)(转)

    Microsoft SQL Server 5030错误解决办法 今天在使用SQL Server时,由于之前创建数据库忘记了设置Collocation,数据库中插入中文字符都是乱码,于是到DataBas ...

随机推荐

  1. Android APK 签名 (转发)

    Cordova 3.5 为 Android APK 签名,生成可发布的 APK 程序文件  任侠  2014-06-07 00:04  移动开发  抢沙发  16,288 views  目录 [隐藏] ...

  2. day10---multiprocess 多进程

    multiprocess Queue  \ Pipe 只是实现进程间数据的传递 Manager 实现了进程间数据的共享,即多个进程可以修改同一份数据   进程模块 multiprocessing #! ...

  3. 状态开关按钮(ToggleButton)和开关(Switch)

    ToggleButton支持的XML属性及相关方法1.android:checked----->setChecked(boolean) ----->设置该按钮是否被选中2.android: ...

  4. 重建Windows 8的图标缓存

    Windows 8的图标缓存路径与Win7不同,重置方法如下: rem 关闭explorer.exe taskkill /f /im explorer.exe attrib -h -i %userpr ...

  5. poj 1702 三进制问题

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3906   Accepted: 1924 Description Eva h ...

  6. C++混合编程之idlcpp教程Python篇(5)

    上一篇在这  C++混合编程之idlcpp教程Python篇(4) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相似,工程PythonTutorial3中,同样加入了三个文件:Py ...

  7. centos 6.5 中设置mysql 5.1.73 主从同步配置过程

    本文章给大家介绍centos 6.5设置mysql主从同步过程记录,希望文章对各位会带来帮助.  涉及到的centos系统均为虚拟机,VM下安装的版本. 在centos 6.5上设置了mysql主从功 ...

  8. 一个App完成入门篇(三)-完善主框架

    本节教程将继续带领大家完善教学demo 导入项目 完善主框架 完成viewShower子视图 打开新页 启动动画 将要学习的demo效果图如下所示 1. 如何导入完整项目 本节示例demo请参考下载地 ...

  9. Unity3D热更新全书-脚本(二) 两级分化

    上篇明确了我们探讨的脚本是什么:是写在文本文件里面的代码,可以作为资源加载,取得字符串再执行. 可是为什么世界上会有那么多的脚本?而其使用方法完全看起来不一样呢?这是因为每种脚本都有自己的定位,在不同 ...

  10. rsync permission denied created directories have no permissions

    这个问题坑了好几天 最后被此贴解决 http://stackoverflow.com/questions/5798807/rsync-permission-denied-created-directo ...