转自: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的更多相关文章

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

    原网址:http://blog.csdn.net/sku0923/article/details/1722370 一个stmt多个rs进行操作引起的ResultSet已经关闭错误 一个stmt多个rs ...

  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. resultset 对象获取行字段数据时报:java.sql.SQLException: Column 'id' not found.

    resultset 对象获取行字段数据时报:java.sql.SQLException: Column 'id' not found. 代码: String sql="SELECT d.co ...

  5. 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日  ...

  6. java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

    org.springframework.dao.TransientDataAccessResourceException: ### Error updating database. Cause: ja ...

  7. 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 ...

  8. Caused by: java.sql.SQLException: ResultSet is from UPDATE. No Data.

    1.错误描述 org.hibernate.exception.GenericJDBCException: error executing work at org.hibernate.exception ...

  9. 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 ...

随机推荐

  1. zabbix-agent active 配置自动探测

    1. zabbix-agent 被动模式配置文件: PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_a ...

  2. 【caffe】Caffe的Python接口-官方教程-01-learning-Lenet-详细说明(含代码)

    01-learning-Lenet, 主要讲的是 如何用python写一个Lenet,以及用来对手写体数据进行分类(Mnist).从此教程可以知道如何用python写prototxt,知道如何单步训练 ...

  3. 摩根大通银行被黑客攻克, ATM机/网银危在旦夕,winxp退市灾难来临了

    winxp4月退市到如今还不到半年,就出现故障了 7600多万个消费者银行账户被黑.此外还有700万个小企业账户的信息也被黑客窃取,这个算不算灾难呢?假设等到银行业彻底崩溃,资金彻底丧失,那不仅仅是灾 ...

  4. 【C语言】一句printf代码——{ a[0] ? 0[a] }

    这是前段时间做的http://fun.coolshell.cn/中的一道题,很有意思,涉及的其实是C的基础,不过当时第一次看见这行代码确实把我弄懵了: printf(&unix["\ ...

  5. 在安装ubuntu时,卡在启动画面

    在我安装ubuntu时发生的情况,记录下来希望能帮助到需要帮助的朋友. 我先后尝试16.04与14.05两个版本的Ubuntu系统,方法均为:https://www.ubuntu.com/downlo ...

  6. iOS 10 的杂碎资料

    兼容iOS 10 资料整理笔记   1.Notification(通知) 自从Notification被引入之后,苹果就不断的更新优化,但这些更新优化只是小打小闹,直至现在iOS 10开始真正的进行大 ...

  7. spring AOP(切面) 表达式介绍

    在 spring AOP(切面) 例子基础上对表达式进行介绍 1.添加接口删除方法 2.接口实现类 UserDaoServer 添加实现接口删除方法 3.测试类调用delUser方法 4. 输出结果截 ...

  8. 怎么利用Aspose.Cells 获取excel 数据表中sheet的名称

    说明:开发环境 vs2012 asp.net mvc4 c# 利用Aspose.Cells 获取Excel数据表的sheet的名称,并把获取的名称赋值给easyUI 的combobox 1.运行效果 ...

  9. wimdows安装mongodb,开机启动

    > d: > cd D:\Program Files\MongoDB\Server\3.0\bin > .\mongod --logpath "D:\Program Fil ...

  10. Android笔记之Retrofit与RxJava的组合

    依赖 implementation 'com.squareup.retrofit2:retrofit:2.5.0' implementation 'com.squareup.retrofit2:con ...