Junior
What is the difference between the methods execute, executeQuery, and executeUpdate?
sobes.tech AI
Answer from AI
The methods execute, executeQuery, and executeUpdate are used to execute SQL queries via JDBC, but are intended for different types of queries and return different results.
-
executeQuery(String sql):- Designed for executing
SELECTtype SQL queries. - Returns a
ResultSetobject containing the query results. - Throws an
SQLExceptionif the query is not aSELECT.
// Example of using executeQuery String sql = "SELECT COUNT(*) FROM users"; try (ResultSet rs = statement.executeQuery(sql)) { if (rs.next()) { int userCount = rs.getInt(1); System.out.println("Number of users: " + userCount); } } catch (SQLException e) { e.printStackTrace(); } - Designed for executing
-
executeUpdate(String sql):- Designed for executing
INSERT,UPDATE, orDELETESQL queries. - Can also be used for executing DDL commands (e.g.,
CREATE TABLE,ALTER TABLE). - Returns an integer (
int) representing the number of affected rows (forINSERT,UPDATE,DELETE) or 0 (for DDL commands). - Throws an
SQLExceptionif the query returns aResultSet.
// Example of using executeUpdate String sql = "UPDATE products SET price = price * 1.1 WHERE category = 'electronics'"; try { int updatedRows = statement.executeUpdate(sql); System.out.println("Rows updated: " + updatedRows); } catch (SQLException e) { e.printStackTrace(); } - Designed for executing
-
execute(String sql):- A universal method for executing any type of SQL query.
- Returns a
boolean:trueif the result is aResultSet(forSELECT), andfalseif the result is the number of affected rows or nothing (forINSERT,UPDATE,DELETE, DDL). - To get the actual result after
execute:- If
executereturnstrue, usegetResultSet()to obtain theResultSet. - If
executereturnsfalse, usegetUpdateCount()to get the number of affected rows.
- If
// Example of using execute String sql = "CREATE TABLE IF NOT EXISTS logs (id INT PRIMARY KEY, message VARCHAR(255))"; try { boolean isResultSet = statement.execute(sql); if (isResultSet) { // This is a SELECT query try (ResultSet rs = statement.getResultSet()) { // Process results } } else { // This is an INSERT, UPDATE, DELETE, or DDL query int updateCount = statement.getUpdateCount(); System.out.println("Number of affected rows or DDL result: " + updateCount); } } catch (SQLException e) { e.printStackTrace(); }
The table below summarizes the main differences:
| Method | SQL Query Type | Return Value | Example Queries |
|---|---|---|---|
executeQuery |
SELECT |
ResultSet |
SELECT * FROM users |
executeUpdate |
INSERT, UPDATE, DELETE, DDL |
int (number of affected rows or 0) |
INSERT INTO, UPDATE users, DROP TABLE |
execute |
Any | boolean (true if ResultSet; false otherwise) |
Any |
As a rule, it is recommended to use more specific methods (executeQuery, executeUpdate) when the query type is known in advance, as this improves code clarity and allows the compiler or IDE to perform more precise checks. The execute method is used when the query type may be dynamic or unknown beforehand.