user2250333
user2250333

Reputation: 133

JDBC exception "Operation not allowed after ResultSet closed" when iterating over ResultSet

I have a problem with this code:

ResultSet dane = statement.executeQuery("SELECT * FROM ProWarns WHERE blahblah = '" + cel + "'");

while (dane.next()) {
    // some code (only get some string from result)
    if (TimeEnd <= EndTime) {
        statement.executeUpdate(
            "DELETE FROM ProWarns WHERE id = '" + id + "'"); //and error

        statement.executeUpdate(
            "UPDATE ProWarnsPlayers SET num = '" + NewV + "'" WHERE Pl = '"+ tar + "'");
    }
}

Error: java.sql.SQLException: Operation not allowed after ResultSet closed. Where is the bug and how can I fix it?

PS:

I am including a Pastebin of my DB class, in case its helpful.

Upvotes: 0

Views: 653

Answers (1)

Perception
Perception

Reputation: 80603

A Statement object caches its result set, so when you execute the additional operations in your for loop the original result set gets 'reset'. Which leads to the error that is happening, when you call dane.next. From the Javadoc:

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.

Options? Use another Statement object to execute the inner queries.

Upvotes: 1

Related Questions