A STMT multi-RS operates the resultSet has turned off error

xiaoxiao2021-03-06  20

A STMT multiple RS operates.

Then the RS1 obtained from the STMT must be operated immediately after this RS1 can be obtained to get another RS2, and then the RS2 is operated.

You cannot use each other, causing RS to turn off errors.

The error code is as follows:

STMT = conn.createstatement ();

RS = Stmt.executeQuery ("SELECT * from T1");

RST = Stmt.executeQuery ("SELECT * from T2");

rs.last (); // Due to the executed RST = Stmt.executeQuery (SQL_A); RS will be closed! So the program executes this will prompt the ResultSet has been turned off. Error information is: java.sql.sqlexception: Operation Not ALOWED AFTER ResultSet Closed

Rst.last ();

Correct code:

STMT = conn.createstatement ();

RS = Stmt.executeQuery ("SELECT * from T1");

rs.last (); // The operation of the RS should be manipulated immediately, and then get RST from the database after operation, and then operate the RST

RST = Stmt.executeQuery ("SELECT * from T2");

Rst.last ();

The reason is that:

The Object Used for Executing A Static SQL Statement and Returning The Results It Products.

By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement INTERFACE IMPLICITLY Close A Stact's Current ResultSet Object if An Open ONE EXISTS.

One STMT is best corresponding to one RS, and this happens if two RS is opened with a STMT in one time.

So resolve such questions: 1. Create a few STMTs, a STMT corresponds to a RS; 2. If you use a STMT to correspond to multiple RS, then you can only get an RS, after processing the first RS Other, as "correct code".

Multiple STMT corresponds to their respective RS.

STMT = conn.createstatement ();

STMT2 = conn.createstatement ();

RS = Stmt.executeQuery ("SELECT * from T1");

RST = Stmt2.executeQuery ("SELECT * from T2");

Rs.last ();

Rst.last ();

转载请注明原文地址:https://www.9cbs.com/read-39470.html

New Post(0)