operation not allowed after resultset closed错误的解决
来源:互联网 发布:淘宝上的订单险是什么 编辑:程序博客网 时间:2024/05/22 06:27
就我目前碰到的过的,主要有两种情况会产生这样的问题,一个就是在百度里被转来转去的答案:
不能互相交替使用,会引起rs已经关闭错误.错误的代码如下:
stmt=conn.createStatement();
rs=stmt.executeQuery("select
* fromt1");
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
* fromt1");
rs.last();//对rs的操作应马上操作,操作完后再从数据库得到rst,再对rst操作
rst=stmt.executeQuery("select
* from t2");
rst.last();
说的大概意思就是在同一时刻每个Statement对象只能有一个ResultSet对象可以打开,当程序执行执行了rst=stmt.executeQuery(sql_a);rs就会被关闭掉!
关掉了还在上面进行操作的话肯定会有错误的。解决方法就是不在同一时刻打开多个ResultSet对象,打开一个操作一个。
还有一个问题就是诸如:
stmt=conn.createStatement();
sql=
"SELECT LAST_INSERT_ID()from"+tableName;
rs
=stmt.executeQuery(sql);
while(rs.next()){
= "update "+tableName+" set notifyURL='"+URLUtil.getRequestServerContext(request) +"/NotifyAction?id="+rs.getInt(1)+"' where id ="+rs.getInt(1);
}
目前网上的方法有很多以讹传讹的,比如在while循环外关闭rs,这是解决不了问题的。问题错的地方并不是rs关闭的时机,而是因为
每执行一次execute操作rs就会被自动地关闭。对一个已经关闭的rs再执行next显然是错误的。解决的方法就是在while循环内添加以下一句
代码:
stmt=conn.createStatement();
在每次用到stmt的时候都创建一个新的Statement对象。这样就不会重复的使用rs了。
- operation not allowed after resultset closed错误的解决
- 错误:Operation not allowed after ResultSet closed
- 错误:Operation not allowed after ResultSet closed
- Operation not allowed after ResultSet closed 错误的解决方案
- 异常:Operation not allowed after ResultSet closed的解决
- 关于Operation not allowed after ResultSet closed错误的一种解决方式
- mysql错误:Operation not allowed after ResultSet closed
- 数据库操作错误之“Operation not allowed after ResultSet closed”
- Operation not allowed after ResultSet closed
- SQLException: Operation not allowed after ResultSet closed
- Operation not allowed after ResultSet closed
- Operation not allowed after ResultSet closed
- Operation not allowed after ResultSet closed
- Operation not allowed after ResultSet closed 解决方案
- Operation not allowed after ResultSet closed
- Operation not allowed after ResultSet closed
- Operation not allowed after ResultSet closed--Mysql
- Operation not allowed after ResultSet closed
- 递归调用详解
- 概率练习 (16.04.30)
- 单例模式的简单demo
- zookeeper中问题
- UVA 11825Hackers' Crackdown dp+位运算状态压缩(子集枚举)
- operation not allowed after resultset closed错误的解决
- Centos中查看nginx、apache、php、mysql配置文件路径
- LightOJ - 1236 Pairs Forming LCM 合数分解
- HDU - 1548 A strange lift(Dijkstra)
- C++ 左值引用和右值引用(C++11特性)
- 山东省第三届ACM大学生程序设计竞赛-Pixel density(模拟)
- HDU - 3790 最短路径问题(Dijkstra)
- Spring中的AOP(七)(实际上应该是一)——AspectJ的基本使用
- Chrome 插件 Momentum 介绍