JDBC executeQuery() on an INSERT, UPDATE or DELETE throws SQLException
java.sql.SQLException thrown by Statement.executeQuery() when the SQL is an INSERT, UPDATE or DELETEShort answer
executeQuery() is only for statements that produce one ResultSet. An INSERT, UPDATE or DELETE produces an update count, so use executeUpdate(), or execute() when you do not know in advance. The reverse also fails: executeUpdate() rejects a statement that returns a ResultSet.
Fix it fast
1Find out which cause you have
Find the call and read the SQL it passes. If the statement starts with INSERT, UPDATE or DELETE, the method is the wrong one.
grep -rn "executeQuery(" src/ | head -202Match what you see, then jump to its fix
- 1
The failing SQL is a data-changing statementexecuteQuery() used for INSERT, UPDATE or DELETEFix - 2
SQL comes from a file, a console or user inputThe statement type is not known when the code is writtenFix - 3
The failing SQL is a SELECT, or a call that returns a result setexecuteUpdate() used for a statement that returns rowsFix
- 1
What does the error look like?
java.sql.SQLException thrown by Statement.executeQuery() when the SQL is an INSERT, UPDATE or DELETEjava.sql.SQLException thrown by Statement.executeUpdate() when the SQL is a SELECT
Cause 1 of 3
executeQuery() used for INSERT, UPDATE or DELETE
1Check
The JDK documentation says executeQuery cannot be used for statements that do not produce a ResultSet, and throws SQLException when the statement produces anything other than a single ResultSet.
ResultSet rs = stmt.executeQuery("UPDATE users SET active = 1 WHERE id = 5"); // wrong2Fix
Use executeUpdate(). It returns the row count for DML statements, or 0 for statements that return nothing.
int rows = stmt.executeUpdate("UPDATE users SET active = 1 WHERE id = 5");3Verify
The call returns the number of changed rows without an exception.
System.out.println(rows);Confirmed by docs.oracle.com
Cause 2 of 3
The statement type is not known when the code is written
1Check
execute() accepts any SQL. It returns true when the first result is a ResultSet and false for an update count, and it does not reject statements that produce a ResultSet.
boolean isResultSet = stmt.execute(sql);2Fix
Branch on the return value, and use getResultSet() or getUpdateCount() as documented.
boolean isResultSet = stmt.execute(sql);
if (isResultSet) {
try (ResultSet rs = stmt.getResultSet()) { /* read rows */ }
} else {
int count = stmt.getUpdateCount(); // -1 means no more results
}3Verify
Run one SELECT and one UPDATE through the same code path.
// both run without SQLExceptionConfirmed by docs.oracle.com
Cause 3 of 3
executeUpdate() used for a statement that returns rows
1Check
executeUpdate() throws SQLException when the given statement produces a ResultSet object.
int n = stmt.executeUpdate("SELECT * FROM users"); // wrong2Fix
Use executeQuery() for queries that return rows.
try (ResultSet rs = stmt.executeQuery("SELECT * FROM users")) {
while (rs.next()) { /* read */ }
}3Verify
The rows print.
// iterate the ResultSetConfirmed by docs.oracle.com
How do I stop it coming back?
- Pick the method from the SQL verb: executeQuery for SELECT, executeUpdate for DML, execute when unsure.
- On a PreparedStatement, call the no-argument executeQuery() or executeUpdate(); passing a SQL string to those methods on a PreparedStatement is an error per the JDK docs.
- Close statements and result sets with try-with-resources.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Back end
The JDBC driver warns that com.mysql.jdbc.Driver is deprecated
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'.
The warning means something still names the Connector/J 5.1 class.
2 causes, each with a fixBack end
Spring Boot cannot load the MySQL JDBC driver class
Cannot load driver class: com.mysql.jdbc.Driver
Spring Boot checks that the driver class you name can be loaded before it builds the connection pool.
3 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
The AWS SDK for Java cannot find credentials, and AwsCredentials.properties does nothing
software.amazon.awssdk.core.exception.SdkClientException: Unable to load credentials from any of the providers in the chain
AwsCredentials.properties on the classpath is an old convention: the default provider chain of the AWS SDK for Java does not read it.
4 causes, each with a fixBack end
CodeIgniter 4: try/catch does not catch a failed insert in the model
DatabaseException
CodeIgniter throws a DatabaseException on a failed query only when DBDebug is true, and inside a transaction it does not throw unless you ask.
3 causes, each with a fixBack end
CodeIgniter 4 where() builds a different condition than you meant
A SQL syntax error that appears only when a value contains a quote character
The query builder joins every where() with AND and every orWhere() with OR, with no brackets.
3 causes, each with a fix