java.sql.SQLException: Operation not allowed after ResultSet closed
转自:http://blog.csdn.net/hellobobantang/article/details/7173622
java.sql.SQLException: Operation not allowed after ResultSet closed
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:794)
at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:7145)
at org.xxx.similarity.Similarity.createIndexTable(Similarity.java:26)
at org.xxx.similarity.Similarity.main(Similarity.java:56)
百度这个问题,给出的解决方法都是:
一个stmt多个rs进行操作.那么从stmt得到的rs1,必须马上操作此rs1后,才能去得到另外的rs2,再对rs2操作.
不能互相交替使用,会引起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();
当然这是导致这个错误的一种原因,但还有另外一个原因,请看如下代码:
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/hellosql","root", "123");
stmt=conn.createStatement();
sql=
"SELECT LAST_INSERT_ID()from"+tableName;
rs
=stmt.executeQuery(sql);
while(rs.next()){
sql
= "update "+tableName+" set notifyURL='"+URLUtil.getRequestServerContext(request) +"/NotifyAction?id="+rs.getInt(1)+"' where id ="+rs.getInt(1);
stmt.executeUpdate(sql);
}
rs.close();
rs
=null;
stmt.close();
stmt
=null;
con.close();
con
=null;
原因是executeUpdate函数和execute函数会自动返回resultSet!
boolean java.sql.Statement.execute(String sql) throwsSQLException
Executes the given SQL statement, which may return multiple results. In some (uncommon) situations, a single SQL statement may return multiple result sets and/or update counts. Normally you can ignore this unless you are (1) executing a stored procedure that you know may return multiple results or (2) you are dynamically executing an unknown SQL string.
The execute
method executes an SQL statement and indicates the form of the first result. You must then use the methodsgetResultSet
orgetUpdateCount
to retrieve the result, andgetMoreResults
to move to any subsequent result(s).
java.sql.SQLException: Operation not allowed after ResultSet closed的更多相关文章
- SQLExecption:Operation not allowed after ResultSet closed解决办法
原网址:http://blog.csdn.net/sku0923/article/details/1722370 一个stmt多个rs进行操作引起的ResultSet已经关闭错误 一个stmt多个rs ...
- jsp操作MySQL时报错:Operation not allowed after ResultSet closed
一个stmt对多个rs进行操作引起的ResultSet关闭的错误 解决办法:创建新的stmt,一个rs对应一个stmt
- Operation not allowed after ResultSet closed--操作mysql数据库
一个stmt多个rs进行操作.那么从stmt得到的rs1,必须马上操作此rs1后,才能去得到另外的rs2,再对rs2操作.不能互相交替使用,会引起rs已经关闭错误——Operation not all ...
- resultset 对象获取行字段数据时报:java.sql.SQLException: Column 'id' not found.
resultset 对象获取行字段数据时报:java.sql.SQLException: Column 'id' not found. 代码: String sql="SELECT d.co ...
- java.sql.SQLException: null, message from server: “Host ‘xxx’ is not allowed to connect
java.sql.SQLException: null, message from server: “Host ‘xxx’ is not allowed to connect 2014年06月29日 ...
- java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
org.springframework.dao.TransientDataAccessResourceException: ### Error updating database. Cause: ja ...
- Caused by: java.sql.SQLException: Couldn't perform the operation getAutoCommit: You can't perform any operations on this connection. It has been automatically closed by Proxool for some reason (see lo
系统启动,一段时间不操作,然后在来操作时,报错如下: Caused by: java.sql.SQLException: Couldn't perform the operation getAutoC ...
- Caused by: java.sql.SQLException: ResultSet is from UPDATE. No Data.
1.错误描述 org.hibernate.exception.GenericJDBCException: error executing work at org.hibernate.exception ...
- Mysql报错java.sql.SQLException:null,message from server:"Host '27,45,38,132' is not allowed to connect
Mysql报错java.sql.SQLException:null,message from server:"Host '27,45,38,132' is not allowed to co ...
随机推荐
- FZU 2124 FOJ 2124 吃豆人【BFS】
Problem 2124 吃豆人 Accept: 134 Submit: 575 Time Limit: 1000 mSec Memory Limit : 32768 KB Probl ...
- NGINX配置文件优化示例
Nginx主配置文件 upstream.conf配置文件 # server nginx配置文件最好分开写,不要把所有的逻辑都放在一个文件里面,会看着很麻烦,,之前我的配置文件都放一起了,,导致现在维护 ...
- IPv4地址(二)网络划分
在IPv4地址(一)中提到过,IP地址可以分成两部分,前面一部分是网络号,而后面一部分是主机号. 这里网络可以通过主机数量规模不同而分为3类:大型网络.中型网络和小型网络. 不同网络的特点 大型网络— ...
- YARN/MRv2 中基本术语介绍
YARN/MRv2是下一代MapReduce框架(见Hadoop-0.23.0),该框架完全不同于当前的MapReduce框架,它在扩展性,容错性和通用性等方面更出色,据统计,Yarn有超过15000 ...
- 【Python基础】之不同的文件在不同目录下导入指定模块的方法
如下图三个文件的目录路径 – project |– 1 | |– 2 | | |– 3 | | | |– owen.py ...
- iOS 键盘变中文
plist文件添加 Localizations 添加一项字段Chinese (simplified)
- MySQL 忘记密码解决办法
第一步: 关闭MySQL服务. 第二步: 打开DOS窗口,在里面输入安装MqSQL的目录本机为:C:\Program Files\MySQL\MySQL Server 5.6\bin 第三步: 在命 ...
- boost::noncopyable
/** * boost::noncopyable 实现单例不用麻烦了,直接从这个继承就行了 */ #include <boost/noncopyable.hpp> class myclas ...
- SSIS
http://www.cnblogs.com/codefish/category/557802.html
- 【BZOJ4569】[Scoi2016]萌萌哒 倍增+并查集
[BZOJ4569][Scoi2016]萌萌哒 Description 一个长度为n的大数,用S1S2S3...Sn表示,其中Si表示数的第i位,S1是数的最高位,告诉你一些限制条件,每个条件表示为四 ...