3

Can anyone point me in the write direction to solve the error: SQLException: Operation not allowed after ResultSet closed

Here is the code i believe the error occurs in

Statement stmt = con.createStatement();
String query1 = "";
String query2 = "";

int candidateNo = 42;

query1 = "SELECT * FROM Candidates WHERE CandidateNo = "+ candidateNo;
ResultSet rs = stmt.executeQuery(query1);
//If query can be completed then display name and prompt to continue
if(rs.next()){
    query2 = "SELECT * FROM Achieved WHERE CandidateNo = " + candidateNo;
    ResultSet ts = stmt.executeQuery(query2);
}

I have tried using resultset rs = null; to create the variable but the error still occurs

Dcurrie1
  • 93
  • 2
  • 10

2 Answers2

1

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 .

Arnaud
  • 17,229
  • 3
  • 31
  • 44
1

In your code you used 2 ResultSet executing the same Statement object (stmt)concurrently. Use different Statement Objects for both queries.

Har Krishan
  • 273
  • 1
  • 11