0
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {        
ConnectionDTB cdtb = new ConnectionDTB();

ResultSet rs = cdtb.retrieveData("select svcode from listSV");
if(!rs.isBeforeFirst()){
        System.out.println("System is null");
        //add
        cdtb.close();
    }
else{
    while(rs.next()){
        if(Integer.parseInt(jTextField1.getText()) == rs.getInt("svcode")){       
            System.out.println("ERROR");
            cdtb.close();
            break;
        }
        else{
            if(rs.isLast()){
                //add
                cdtb.close();
            }else{
                cdtb.close();
        }
 }

My error is

Apr 25, 2015 2:21:29 AM GUI.Them1 jButton1ActionPerformed SEVERE: null java.sql.SQLException: Operation not allowed after ResultSet closed

What did I do wrong?

3
  • 2
    Why did you close a connection in while loop?:) Commented Apr 24, 2015 at 19:35
  • do you mean i should close after add . Can u fix my code and post it pleas :( Commented Apr 24, 2015 at 19:58
  • hey Alex can u look my problem again pleas. i fixed my problem for clearly understand Commented Apr 24, 2015 at 20:04

3 Answers 3

1

It looks like you are closing the database connection inside your loop, before you are done with the result set.

You can't close the database connection if you are still using statements or resultsets associated with the database connection. (Well, you can do it, but closing the connection will also close the resultset, and you get the kind of behavior you observe.)

The normative pattern is to use try/catch/finally blocks, and to close the resultset(s), statements(s) and connection in the finally block(s).

For example:

ConnectionDTB cdtb = null;
ResultSet rs = null;

try {

   cdtb = new ConnectionDTB();
   rs = cdtb.retrieveData("select svcode from listSV");

   while(rs.next()){
       // whatever processing you need to do on each row, but
       // do NOT close the result set here
       // do NOT close the database connection here!
   }

} catch (SQLException ex) {
    // anything you want to do if an exception is thrown
} finally {
    if (rs != null) {
        try { rs.close(); } catch (SQLException e) { /*ignore*/ }
    }
    if (cdtb != null) {
        try { cdtb.close(); } catch (SQLException e) { /*ignore*/ }
    }
}

That finally block could be simplified, removing the unnecessary try/catch. This would be fine too:

} finally {
    if (rs != null) {
        rs.close();
    }
    if (cdtb != null) {
        cdtb.close();
    }
}

The important thing here is to do the closing in the "finally" block, so that's going to be run even if an exception occurs. And with this pattern, there is one "close" of the database connection, it's not multiple calls scattered through your code.


Some other notes:

It looks as if your code means to "add" a row if one doesn't already exist. This is a lot of overhead, pulling back every value from the database and inspecting it.

It would be much more efficient to ask the database if such a row exists. To ask the database whether a row like that exists, use a statement like this:

SELECT svcode FROM listSV WHERE svcode = ?

Prepare the statement, bind a value to the placeholder (the value you are looking for), and execute the statement, and check if a row is returned or not.

If you are performing this check to see whether a row needs to be added to the listSV table, you could actually use a single INSERT ... SELECT statement to conditionally insert a row.

Sign up to request clarification or add additional context in comments.

2 Comments

stackoverflow.com/questions/29861519/… Could u see my another problem here :(
0

Although I don't think its actually shown from the information provided, I think that you are closing the result set (in this case 'rs') and then attempting to run an operation again.

If I had to guess as well, I'd say take out the rs.close because it is closing the result set prematurely. Maybe replace with a continue if you wanted to keep reading from the same set?

2 Comments

do you mean rs.close(); after cdtb.close();
@QuangVu I am referring to rs.close, since that is the ResultSet (which is what the error indicates). However closing both probably doesn't help.
0
  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {        
  ConnectionDTB cdtb = new ConnectionDTB();

  ResultSet rs = cdtb.retrieveData("select svcode from listSV");
  if(!rs.isBeforeFirst()){
        System.out.println("System is null");
        //add
        cdtb.close();
        rs.close();
    }
    else{
    while(rs.next()){
        if(Integer.parseInt(jTextField1.getText()) == rs.getInt("svcode")){       
            System.out.println("ERROR");
            cdtb.close();
            rs.close();
            break;
        }
        else{
            if(rs.isLast()){
                //add
                cdtb.close();
                rs.close();
            }
      }

is this right? by the way could u tell me AfterLast meaning?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.