Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closed ResultSet without closing statement

Tags:

java

sql

mysql

jdbc

In my application, the following code

ResultSet images = statement.executeQuery("SELECT id, filename, run" +
          " FROM images" +
          " JOIN comparer_results results ON results.toImageId = images.id" +
          " WHERE result <= 100");

while (images.next()) {
    statement.executeUpdate("DELETE FROM images WHERE id = "
        + images.getInt("id"));

    File imageFile = new File(config.getProperty("system.imageDirectory")
        + File.separator
        + images.getString("filename") + ".jpg");
}

Throws the exception

java.sql.SQLException: Operation not allowed after ResultSet closed

In the line where the imageFile get instantiated. It's my understanding that this is caused by the images.getString("filename") operation. But why is the ResultSet closed? I never call such a method and the first operation on the resultset (images.getInt("id")) works just fine.

like image 667
F.P Avatar asked Feb 21 '23 23:02

F.P


1 Answers

Assoon as you called

statement.executeUpdate("DELETE FROM images WHERE id = "
    + images.getInt("id"));

you re-used the statement. This means that any previously created ResultSet is closed automatically.

You must use two Statements in this case.

like image 51
A.H. Avatar answered Mar 02 '23 15:03

A.H.