executeQuery on an INSERT or UPDATE throws in PostgreSQL JDBC
org.postgresql.util.PSQLException: No results were returned by the query.Short answer
executeQuery is for statements that return rows. An INSERT, UPDATE, DELETE or DDL statement returns none, so pgJDBC throws. Call executeUpdate for INSERT, UPDATE and DELETE, or execute for DDL. If you do want rows back, the statement itself has to return them.
Fix it fast
1Find out which cause you have
Find the line in the stack trace that calls executeQuery, then read the SQL it runs. The word at the start of that SQL tells you which cause applies.
grep -rn "executeQuery" src/main/java/2Match what you see, then jump to its fix
What does the error look like?
org.postgresql.util.PSQLException: No results were returned by the query.No results returned by the query
Cause 1 of 2
executeQuery used for INSERT, UPDATE or DELETE
1Check
The JDK documents executeQuery as throwing when the SQL does not produce a single ResultSet, and executeUpdate as returning the row count for data statements.
grep -rn -B2 "INSERT INTO\|UPDATE \|DELETE FROM" src/main/java/ | grep executeQuery2Fix
Use executeUpdate and read the returned row count.
try (PreparedStatement st = conn.prepareStatement(
"INSERT INTO mytable (columnfoo) VALUES (?)")) {
st.setInt(1, 500);
int rows = st.executeUpdate();
}3Verify
The call returns the number of rows affected and no exception.
SELECT COUNT(*) FROM mytable;Confirmed by jdbc.postgresql.org
Cause 2 of 2
A DDL or utility statement sent through executeQuery
1Check
pgJDBC documents execute() for creating or changing objects, a call that does not return a result.
grep -rn -B2 "CREATE TABLE\|ALTER TABLE\|DROP " src/main/java/ | grep executeQuery2Fix
Use Statement.execute, or executeUpdate which returns 0 for statements that return nothing.
try (Statement st = conn.createStatement()) {
st.execute("CREATE INDEX idx_foo ON mytable (columnfoo)");
}3Verify
The statement completes and the object exists.
SELECT indexname FROM pg_indexes WHERE tablename = 'mytable';Confirmed by docs.oracle.com
How do I stop it coming back?
- Pick the JDBC method from the first word of the SQL: SELECT uses executeQuery, data changes use executeUpdate, DDL uses execute.
- Let your data layer (JdbcTemplate, JPA) choose the call so you do not write it by hand.
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?
Related fixes
Databases
H2 in PostgreSQL mode still behaves differently from PostgreSQL
Names or null ordering differ from PostgreSQL in test results
H2 compatibility modes are switched on in the JDBC URL and cover only a small part of the differences between databases.
4 causes, each with a fixDatabases
Java cannot load org.postgresql.Driver
Class not found loading JDBC org.postgresql.Driver
The PostgreSQL JDBC jar is not on the classpath of the process that runs your code.
3 causes, each with a fixDatabases
TypedQuery rejects a query that selects several columns
The select list of the query must contain only a single item
The Jakarta Persistence rule is that the select list of a typed query holds a single item.
4 causes, each with a fixDatabases
PostgreSQL JDBC: an I/O error occurred while sending to the backend
org.postgresql.util.PSQLException: An I/O error occurred while sending to the backend.
This error means the connection the driver was about to use was already closed.
3 causes, each with a fixDatabases
Saving a UUID through JDBC fails with a type mismatch
Hint: You will need to rewrite or cast the expression.
pgJDBC sends setString parameters as varchar, and PostgreSQL will not store varchar in a uuid column without a conversion.
3 causes, each with a fixDatabases
Hibernate insert fails: column is of type bit but expression is of type boolean
ERROR: column "active" is of type bit but expression is of type boolean
PostgreSQL has a real boolean type and a separate bit type, and it will not convert one to the other on insert.
3 causes, each with a fix