JAVAWarningDatabase ErrorJuly 2, 2026

Database Error

You cannot use a scrollable result set with a Statement object that has been set to be closed

What This Error Means

This error occurs when you attempt to use a Statement object to scroll through a result set after it has been set to be closed. This is a common mistake that can happen when using JDBC to interact with a database.

Why It Happens

This error happens when you call the close() method on a Statement object, which marks it as closed, and then try to use it to scroll through a result set. JDBC does not allow this because once a Statement object is closed, it can no longer be used to execute queries or access a result set.

How to Fix It

  1. 1To fix this error, you need to create a new Statement object after calling close() on the old one. Alternatively, you can use a Prepared Statement or a Connection's executeQuery() method, which will not close the result set. Here is an example of how to fix the code:
  2. 2// Before (broken code)
  3. 3Statement stmt = conn.createStatement();
  4. 4stmt.execute("SELECT * FROM table");
  5. 5stmt.close();
  6. 6while (stmt.getResultSet().next()) {
  7. 7 // Code here
  8. 8}
  9. 9// After (fixed code)
  10. 10Statement stmt = conn.createStatement();
  11. 11ResultSet rs = stmt.execute("SELECT * FROM table");
  12. 12while (rs.next()) {
  13. 13 // Code here
  14. 14}
  15. 15rs.close();
  16. 16stmt.close();

Example Code Solution

❌ Before (problematic code)
Java
Statement stmt = conn.createStatement();
stmt.execute("SELECT * FROM table");
stmt.close();
while (stmt.getResultSet().next()) {
    // Code here
}
✅ After (fixed code)
Java
Statement stmt = conn.createStatement();
ResultSet rs = stmt.execute("SELECT * FROM table");
while (rs.next()) {
    // Code here
}
rs.close();
stmt.close();

Fix for You cannot use a scrollable result set with a Statement object that has been set to be closed

Related JAVA Errors

Related JAVA Blog Articles

Have a different error? Get an instant explanation.

Explain Another Error