Java Jdbc MySQL Exception: "Operation Not Allowed After Resultset Closed"

Operation not allowed after ResultSet closed, mysql

You would rather check whether they have any reference or not, then close them.

 finally {

if (resultset != null) {
resultset.close();
}

if (s != null) {
s.close();
}

if (con != null) {
con.close();
}

}

I suspect, issue with

return resultset;

Since, you are closing the Connection and Statement before returning the ResultSet, so ResultSet might not be available. You must be aware finally block will execute before returning the value. That is also why, your code is working when you left your Connection open.

So, instead you should store the result into some data structure and return.

Operation not allowed after ResultSet closed (mysql, java)

A Statement object can have only one active ResultSet, so when you execute rs2 = stmt.executeQuery(sql), the first ResultSet (rs) gets closed.

Create two Statement objects, one for rs and another for rs2.

Quoting the javadoc of Statement:

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 statment's current ResultSet object if an open one exists.

Operation not allowed after ResultSet closed?

A better way of doing what you want is using a single ResultSet

  String sql = "SELECT * FROM test";
ResultSet rs = stmt.executeQuery(sql);

if(rs.next()){
do {

if (rs.getString("sent").equals("0")) {

try{
//if there is a row and it is equals to 0.. so update it to 1..

String sql2 = "UPDATE test SET test='1' WHERE test=....";
Statement updateStatement = <create statement here>;

int rs3 = updateStatement.executeUpdate();
updateStatement.close();
}catch (SQLException sqlex) {
sqlex.printStackTrace();
}
}
}while(rs.next());
}else {
try{
//insert if i couldn't get any rows in the table
String sql2 = "INSERT INTO test......";
Statement insertStatement = <create statement here>;
int rs3 = insertStatement.executeUpdate();
insertStatement.close();
}catch (SQLException sqlex) {
sqlex.printStackTrace();
}
}

rs.close();
stmt.close();
conn.close();

Hope this helps.

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

You are iterating over the ResultSet bound to a Statement , then while iterating, you are using the exact same Statement object to issue a new query and get and iterate over another ResultSet .

This won't work as long as you are not done with the processing of the first ResultSet, so consider using a distinct Statement object for your second query .

Operation not allowed after ResultSet closed after using ResultSet in AbstractTableModel

Always use the memory structure such as ArrayList or Map to cache the result got from DB. As connection to DB is expensive, keeping this connection is also expensive. So calling DB as quick as possible, as short as possbile. Once you got data in memory, you are free to manipulate as you wish.



Related Topics



Leave a reply



Submit