JAVAWarningApril 15, 2026
Database Error
java.sql.SQLException: Cannot execute query while there is an open result set
What This Error Means
This error occurs when you try to execute a new query while a previous query's result set is still open, preventing the database from processing the next query.
Why It Happens
This error happens when a database connection is not properly closed or when a result set is not closed after use, causing the database to freeze and prevent new queries.
How to Fix It
- 1To fix this error, ensure that you close any open result sets before executing new queries. Use methods like 'ResultSet.close()' and 'Statement.close()' to properly close resources. Use try-with-resources statements or ensure to close resources in a finally block to avoid resource leaks.
Example Code Solution
❌ Before (problematic code)
Java
try {
// Execute first query
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM table1");
// Execute new query without closing previous result set
stmt = conn.createStatement();
stmt.executeQuery("SELECT * FROM table2");
} catch (SQLException e) {
System.out.println(e.getMessage());
}✅ After (fixed code)
Java
try (Statement stmt = conn.createStatement()) {
// Execute first query
ResultSet rs = stmt.executeQuery("SELECT * FROM table1");
// Close the result set
rs.close();
// Now it's safe to execute the new query
stmt = conn.createStatement();
stmt.executeQuery("SELECT * FROM table2");
} catch (SQLException e) {
System.out.println(e.getMessage());
}Fix for java.sql.SQLException: Cannot execute query while there is an open result set
Browse Related Clusters
Related JAVA Errors
Related JAVA Blog Articles
Have a different error? Get an instant explanation.
Explain Another Error