execute() vs executeQuery() vs executeUpdate() in JDBC
Fast recall
Quick revision
Core answer
ResultSet. Use `executeUpdate()` for INSERT/UPDATE/DELETE/DDL — it returns an int row count. Use `execute()` when you don't know the type ahead of time — it returns a boolean (true = a ResultSet is available), and you fetch results accordingly.Whether you can match each execute method to the right SQL and return type.
Using executeQuery for an UPDATE or vice versa.
Note execute() is for dynamic/unknown SQL or statements returning multiple results.
Interview-ready guide
Interview answer
JDBC gives you three execute methods, and you pick based on what the SQL does and what you want back.
executeQuery is for SELECT statements; it returns a ResultSet that you iterate over to read rows. executeUpdate is for data-changing statements, INSERT, UPDATE, and DELETE, as well as DDL like CREATE; it returns an int, the number of rows affected, which is zero for DDL.
And execute is the general-purpose one, used when you don't know the statement type ahead of time or it might return multiple results; it returns a boolean, where true means the first result is a ResultSet, and then you call getResultSet or getUpdateCount to retrieve the actual results.
The mistakes to avoid are mixing them up: calling executeQuery on an update, or executeUpdate on a select, is wrong because the return types don't match the operation. In day-to-day code I'm almost always using executeQuery for reads and executeUpdate for writes, and I rarely need the generic execute.
The execute method really comes into play in things like database tools, dynamically generated SQL where you genuinely don't know if it's a query or an update, or stored procedures that can return a ResultSet or an update count, sometimes several in sequence.
execute() vs executeQuery() vs executeUpdate() in JDBC
Direct Answer
Use executeQuery() for SELECT — it returns a ResultSet. Use executeUpdate() for INSERT/UPDATE/DELETE/DDL — it returns an int row count. Use execute() when you don't know the type ahead of time — it returns a boolean (true = a ResultSet is available), and you fetch results accordingly.
Deep Dive & Architecture
The three JDBC methods differ mainly in what the caller expects back. executeQuery() is for a statement that produces one ResultSet, normally a query. executeUpdate() is for a statement whose useful result is an update count, such as INSERT, UPDATE, DELETE, or DDL; a DDL operation commonly reports zero. execute() handles SQL that may return either kind or several results.
For example, after boolean hasRows = statement.execute(sql), true means the first result is available through getResultSet(), while false means an update count can be read with getUpdateCount(). Advanced callers can move through later results with getMoreResults(). When the SQL type is already known, the specific method makes the code clearer and catches a mismatched expectation earlier.
Common Mistakes & Pitfalls
Using executeQuery for an UPDATE or vice versa.
Check your understanding
Answer these without looking back at the article.
- What are the three types of statements in JDBC?
- What is the performance benefit of PreparedStatement over Statement?
- How does PreparedStatement's precompilation improve performance?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.