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

时间: 2024-12-13 07:05:00

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进行操作. 那么从stmt得到的rs1,必须马上操作此rs1后,才能去得到另外的rs2,再对rs2操作. 不能互相交替使用,会引起rs已经关闭错误. 错误的代码如下:  stmt=conn.createStatement(); rs=stmt.executeQuery("select * from

错误:Operation not allowed after ResultSet closed

?????????????????? 一.错误信息 Operation not allowed after ResultSet closed  意为:在结果集关闭不允许操作 二.为什么resultSet会自动关闭 错误关键在于:一个stmt有多个rs进行操作 正确操作应该:从stmt得到的rs1,必须马上操作此rs1后,才能去得到另外的rs2,再对rs2操作.不能互相交替使用:   或者   不用的rs,使用不同的stmt 错误的代码如下: stmt=conn.createStatement()

Operation not allowed after ResultSet closed

首先应该按照网上搜到的方法检查一下是否是一个statement对多个ResultSet进行操作. 比方说: Statement st = conn.CreateStatement(); ResultSet rs1 = st.executeQuery(); ResultSet rs2 = st.executeQuery(); 则必须先对rs1进行操作,然后再对rs2进行操作,不能交替操作.否则会引起rs已经关闭错误,因为执行第三条语句的时候,程序默认将rs2关闭,再对rs2进行操作的时候,就会发生

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日 ⁄ 综合 ⁄ 共 637字 ⁄ 字号 小 中 大 ⁄ 评论关闭 java.sql.SQLException: null,  message from server: "Host 'xxx' is not allowed to connect to this MySQL server": j

Caused by: java.sql.SQLException: Couldn't perform the operation commit

1.错误描述 Caused by: java.sql.SQLException: Couldn't perform the operation commit: You can't perform any operations on this connection. It has been automatically closed by Proxool for some reason (see logs). at org.logicalcobwebs.proxool.WrappedConnecti

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

1.错误描述 org.hibernate.exception.GenericJDBCException: error executing work at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(Sql

resultset 对象获取行字段数据时报:java.sql.SQLException: Column 'id' not found.

resultset 对象获取行字段数据时报:java.sql.SQLException: Column 'id' not found. 代码: String sql="SELECT d.content,c.name AS categoryName FROM news_detail d,news_category c WHERE d.categoryId=c.id"; Object[] params ={}; System.out.println(this.executeQuery(sq

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

org.springframework.dao.TransientDataAccessResourceException: ### Error updating database. Cause: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed ### The error may involve defaultParameterMap ### T

java.sql.SQLException: null, message from server: "Host 'xxx' is not allowed to connect to this MySQL server"

java.sql.SQLException: null,  message from server: "Host 'xxx' is not allowed to connect to this MySQL server": 表示该对象不是远程对象,不能通过该对象远程访问数据 解决: 方案一:改表: use mysql ;select user,host,password from user; update user set host = '%' where user='root'; 方