Java.sql.SQLException:Operation not allowed after ResultSet closed

Source: Internet
Author: User

Transferred from: 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)

Baidu is the problem, the solution is:

One stmt multiple RS for operation. Then the rs1 obtained from stmt, must immediately operate this rs1, in order to get another rs2, and then rs2 operation.

Cannot be used interchangeably with each other, causing the RS to be closed incorrectly. The code for the error is as follows:

Stmt=conn.createstatement ();

Rs=stmt.executequery ("SELECT * FROMT1");

Rst=stmt.executequery ("Select * from T2");

Rs.last ();//due to the execution of Rst=stmt.executequery (sql_a), RS will be shut down! So program execution to this will prompt resultset has been closed.

The error message is: Java.sql.SQLException:Operation not allowed after ResultSet closed rst.last ();

The correct code:

Stmt=conn.createstatement ();

Rs=stmt.executequery ("SELECT * FROMT1");

Rs.last ();//operation of the RS should be done immediately, after the operation of the database to obtain the RST, and then the RST operation

Rst=stmt.executequery ("Select * from T2");

Rst.last ();


This is, of course, one of the causes of this error, but there is another reason for this, see the following code:

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;


The reason is that the executeupdate function and the Execute function automatically return 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 is (1) executing a stored procedure so you know may return multiple results or (2) You is 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 methods getResultSet or getUpdateCount to retrieve the result, and to move to any getMoreResults subsequent result (s).

Java.sql.SQLException:Operation not allowed after ResultSet closed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.