Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

executeQuery on an INSERT or UPDATE throws in PostgreSQL JDBC

Errororg.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.

  • Applies to PostgreSQL 16, PostgreSQL 17, pgJDBC 42, Java 21
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/
  2. 2Match what you see, then jump to its fix

    1. 1The data change is applied, then the exception is thrownexecuteQuery used for INSERT, UPDATE or DELETEFix
    2. 2The failing SQL starts with CREATE, ALTER, DROP or similarA DDL or utility statement sent through executeQueryFix

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 executeQuery

2Fix

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

Did this fix it?

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 executeQuery

2Fix

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

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.