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 ...
随机推荐
- ubuntu16.04下Cmake学习一
根据网上的资料,我总结了一下,一个工程应该有根目录(bin)存放可执行文件,头文件目录(include)存放头文件,源码文件(src)存放你的算法,还需要一个库文件夹存放你编译的静态库或者动态库.然后 ...
- c# .net Global.asax文件的作用
1 Global.asax文件的作用 先看看MSDN的解释,Global.asax 文件(也称为 ASP.NET 应用程序文件)是一个可选的文件,该文件包含响应 ASP.NET 或HTTP模块所引发的 ...
- golang手动管理内存
作者:John Graham-Cumming. 原文点击此处.翻译:Lubia Yang(已失效) 前些天我介绍了我们对Lua的使用,implement our new Web Applicati ...
- Spring mybatis自动扫描dao
Spring注解方式会出现找不到dao的bean的情况 [解决方案] 在mybatis配置文件中加入以下绑定dao的方式: <!-- mapper接口namepspace绑定方式 -->& ...
- hadoop 出现FATAL conf.Configuration: error parsing conf file,异常
FATAL conf.Configuration: error parsing conf file: com.sun.org.apache.xerces.internal.impl.io.Malfor ...
- 漫反射和Lambert模型
粗糙的物体表面向各个方向等强度地反射光,这种等同地向各个方向散射的现象称为光的漫反射(diffuse reflection).产生光的漫反射现象的物体表面称为理想漫反射体,也称为朗伯(Lambert) ...
- Android 关于软键盘
一..弹出的时候显示Editext框 添加布局replay_input <?xml version="1.0" encoding="utf-8"?> ...
- MessageDigest和DigestUtils加密算法
总结:使用DigestUtils的方法加密的结果与messageDigest的方法加密结果一致,可使用DigestUtils替换MessageDigest 可省掉部分代码 package com.a ...
- Javascript模块化编程-require.js[3]
很多情况下,JS都是放到一个或者多个文件里,只要加载这些文件就可以了. 但是对于一些小型项目而言,这种写法是没有任何问题的. 但是对于某些大型网站,JS的量是很大的,如果还采用这种方式,网站时常在加载 ...
- 题解 CF576C 【Points on Plane】
题解 CF576C [Points on Plane] 一道很好的思维题. 传送门 我们看这个曼哈顿距离,显然如果有一边是按顺序排列的,显然是最优的,那另一边怎么办呢? 假如你正在\(ioi\)赛场上 ...