转自: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. Android 自己定义ImageView实现圆角/圆形 附加OnTouchListener具体凝视以及Button圆角

    转载请注明出处:王亟亟的大牛之路 平时要用一些非方方正正的button之类的小伙伴们是怎样实现的?RadioButton? ImageButton? 还是其它? 今天亟亟上的是ImageView来实现 ...

  2. Oracle:复合触发器

    ----- CF_DEPTUCORGANIZATION  INSERT UPDATE DELETE 触发器CREATE  OR REPLACE TRIGGER tr_del_CF_DEPTUCORGA ...

  3. FileInputStream 的读取操作

    package xinhuiji_day07; import java.io.File;import java.io.FileInputStream;import java.io.FileNotFou ...

  4. tao.opengl+C#绘制三维模型

    一.tao.Opengl技术简介 Opengl是一种C风格的图形库,即opengl中没有类和对象,只有大量的函数.Opengl在内部就是一个状态机,利用不同的函数来修改opengl状态机的状态,以达到 ...

  5. 前端要给力之:语句在JavaScript中的值

    文件夹 文件夹 问题是语句有值吗 那么说你骗我咯 有啥米用呢 研究这个是不是闲得那个啥疼 ES5ES6有什么差异呢 结论是ES6是改了规则但更合理 最后不不过if语句 这两天在写语言精髓那本书的第三版 ...

  6. ch.poweredge.ntlmv2-auth

    <dependency> <groupId>ch.poweredge.ntlmv2-auth</groupId> <artifactId>ntlmv2- ...

  7. 【BZOJ3217】ALOEXT 替罪羊树+Trie树

    [BZOJ3217]ALOEXT Description taorunz平时最喜欢的东西就是可移动存储器了……只要看到别人的可移动存储器,他总是用尽一切办法把它里面的东西弄到手. 突然有一天,taor ...

  8. 计算机网络 --万维网www

    万维网是一个分布式的超媒体系统,客户程序向服务器程序发出请求,服务器程序向客户程序送回客户所需要的万维网文档.万维网必须解决的几个问题:1.怎样标志分布在整个因特网上的万维网文档?答:万维网使用统一的 ...

  9. 使用OpenSessionInViewFilter的注意事项

    假设在你的应用中Hibernate是通过spring 来管理它的session.如果在你的应用中没有使用OpenSessionInViewFilter或者OpenSessionInViewInterc ...

  10. [转]解析ASP.NET WebForm和Mvc开发的区别

    因为以前主要是做WebFrom开发,对MVC开发并没有太深入的了解.自从来到创新工场的新团队后,用的技术都是自己以前没有接触过的,比如:MVC 和EF还有就是WCF,压力一直很大.在很多问题都是不清楚 ...