Getting java.sql.SQLException: Operation not allowed after ResultSet closed
The problem is with the way you fetch data in getStuff()
. Each time you visit getStuff()
you obtain a fresh ResultSet
but you don't close it.
This violates the expectation of the Statement
class (see here - http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html):
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.
What makes things even worse is the rs
from the calling code. It is also derived off-of the statement
field but it is not closed.
Bottom line: you have several ResultSet
pertaining to the same Statement
object concurrently opened.
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 during while loop
You've got static variables named conn1
, ps
, and rs
. Static means there is only one variable for the entire virtual machine.
Clearly then, CandidatesPartyList_JComboBox
is called twice by different threads and thus the variables are overwritten.
The solution is: None of those things should be fields. They should be local variables, and you should be using try-with-resources, which makes this code less than half the size and fixes the problem. Let's also fix the bad error handling ('print the stack trace' is not handling things, so never write that and update your IDE templates).
public static void CandidatesPartyList_JComboBox() {
try (Connection c = VotingSystem.con();
PreparedStatement ps = c.prepareStatement("SELECT * FROM partylist WHERE p_status = 'Active' ORDER BY p_name ASC");
ResultSet rs = ps.executeQuery()) {
candidates_filter_partylist.removeAllItems();
candidates_filter_partylist.addItem("- Select PartyList -");
while (rs.next()) {
candidates_filter_partylist.addItem(rs.getString("p_name"));
}
} catch (SQLException e) {
throw new RuntimeException("Unhandled", e);
}
}
Related Topics
What Happens When a Duplicate Key Is Put into a Hashmap
Hibernate, @Sequencegenerator and Allocationsize
What's the Purpose of Try-With-Resources Statements
Calling Method from Constructor
Base64 Java Encode and Decode a String
Behaviour of Final Static Method
Get Edited Treenode from a Celleditorlistener
Struts2: Updating the Values of a "List of Objects" Inside a Map
Putting Char into a Java String for Each N Characters
Getting Java.Sql.Sqlexception: Operation Not Allowed After Resultset Closed
Differencebetween a Javabean and a Pojo
What Is Simplest Way to Read a File into String
Correctly Implementing the MVC Pattern in Gui Development Using Swing in Java
Why Dec 31 2010 Returns 1 as Week of Year
How to Create Table Using Ascii in a Console
Java: How to Do Dynamic Casting of a Variable from One Type to Another