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 perStatement
object can be open at the same time. Therefore, if the reading of oneResultSet
object is interleaved with the reading of another, each must have been generated by differentStatement
objects. All execution methods in theStatement
interface implicitly close a statment's currentResultSet
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
What's the Point of Guava's Optional Class
How to Fix "Unsupported Class File Major Version 60" in Intellij Idea
Javabean Wrapping with Javafx Properties
Run Command Prompt as Administrator
What Are Good Installanywhere Replacements for Installing a Java Ee Application
Why Does This Generic Code Compile in Java 8
Java - Convert Image to Base64
Read a File Line by Line in Reverse Order
JPA Onetomany Not Deleting Child
How to Change Timezone for a Java.Util.Calendar/Date
Implement Converters for Entities with Java Generics
Java - Generate Random Range of Specific Numbers Without Duplication of Those Numbers - How To
Rotate Bufferedimage Inside JPAnel
Convert JSON Date to Java Date
Reserved Words as Names or Identifiers
How to Write a Key Listener to Track All Keystrokes in Java
Is There a Difference in Removing the Curly Braces from If Statements in Java