aspose file tools*
The moose likes JDBC and the fly likes SQL Exception? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "SQL Exception?" Watch "SQL Exception?" New topic
Author

SQL Exception?

Chris Cairns
Ranch Hand

Joined: Jan 31, 2003
Posts: 48
I got the following error and am hoping that someone could explain why.
The error is: java.sql.SQLException: Closed Statement: next.
And it was produced because of the following order of statements:
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
If you read the docs, you need to obtain all the the information from a ResultSet before any of the following can occur:
Close the connection
Close the Statement
Close the ResultSet
Reuse the Statement for another query
Reuse the ResultSet for another query


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1135

Chris,
A "java.sql.ResultSet" object is _always_ associated with a "java.sql.Statement" object. Therefore, when you close the "Statement", you are effectively also closing the "ResultSet". Hence, as Carl has said, first deal with your "ResultSet" and then youcan close the "Statement".
However, my closing order is the opposite of what Carl has advised. Rather than do this:

Close the connection
Close the Statement
Close the ResultSet

I usually do this:
  • close the "ResultSet"
  • close the "Statement"
  • close the "Connection"

  • Hope this has helped you.
    Good Luck,
    Avi.
    Peter den Haan
    author
    Ranch Hand

    Joined: Apr 20, 2000
    Posts: 3252
    Originally posted by Avi Abrami:
    [...] I usually do this:
  • close the "ResultSet"
  • close the "Statement"
  • close the "Connection"

  • In fact, the JDBC specification stipulates that closing a Statement should close all the ResultSets associated with that Statement -- with a fully compliant driver, if you execute the Statement just once and then close it, you shouldn't have to close the ResultSet explicitly.
    Don't think that Connection.close() should close all open Statements, though: if you're using a connection pool that may not happen at all.
    Be pedantic about closing Statements and Connections in finally blocks! Some JDBC drivers will not forgive you for any resource leaks, e.g. the Oracle driver. Even the smallest leak will eventually kill you (you'll run out of cursors).
    - Peter
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: SQL Exception?