Nearby lessons
10 of 37JDBC - Statement, executeQuery and executeUpdate
📌 What You Will Learn
- The difference between Select and Non-Select operations
- executeQuery vs executeUpdate — when to use which
- Reading results with the ResultSet methods
- Complete CRUD programs: Insert, Select, Update, Delete
- The ResultSet object and how it works
Statement, executeQuery and executeUpdate is a fundamental part of database programming with JDBC. This lesson explains Select vs Non-Select Operations, ecuteQuery vs executeUpdate and Statement vs PreparedStatement vs CallableStatement with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
Select vs Non-Select Operations
In JDBC, every SQL operation is one of two types:
| Type | SQL examples | Which method |
|---|---|---|
| Select operation | SELECT (returns rows of data) | executeQuery() → returns ResultSet |
| Non-Select operation | INSERT, UPDATE, DELETE (change data) | executeUpdate() → returns row count |
Simple memory: if the query gives you data back → executeQuery. If it changes data (or the table structure) → executeUpdate.
executeQuery vs executeUpdate
| Point | executeQuery() | executeUpdate() |
|---|---|---|
| Used for | SELECT | INSERT / UPDATE / DELETE, DDL |
| Returns | A ResultSet (the rows) | An int (how many rows changed) |
| For DDL (CREATE/DROP) | No | Yes (returns 0) |
| Can you read data? | Yes | No |
In simple words: The method you pick depends on what the query returns: `executeQuery()` gives back a ResultSet, `executeUpdate()` gives back a row count. SELECT is a select operation; INSERT, UPDATE and DELETE are non-select operations.
Example02
Statement vs PreparedStatement vs CallableStatement
| Point | Statement | PreparedStatement | CallableStatement |
|---|---|---|---|
| SQL with values | Joined with + (unsafe) | ? placeholders (safe) | ? placeholders |
| Compiled | Every time | Once (reusable) | Once (procedure call) |
| SQL injection risk | High | None | None |
| Used for | DDL, one-time queries | Repeated value queries (INSERT/UPDATE) | Stored procedures |
| Performance | Slowest for repeats | Fast | Fast |
Statement vs PreparedStatement vs CallableStatement
| Point | Statement | PreparedStatement | CallableStatement |
|---|---|---|---|
| Used for | Simple SQL | SQL with values (repeated) | Stored procedures |
| Create by | con.createStatement() | con.prepareStatement(sql) | con.prepareCall("{call ...}") |
| Placeholders | No | Yes (? with setXxx) | Yes (? with setXxx + registerOut) |
| SQL injection safe? | No | Yes | Yes |
| Best performance when | One-time queries | Same query, many values | Procedure calls |
Real-Time Coding Standards
The classic material teaches the habits professional developers follow. These are must-know for placements:
| Standard | Why |
|---|---|
| Always close resources (ResultSet, Statement, Connection) | Otherwise connections leak and the database runs out of connections |
| Use try-with-resources (Java 7+) | Resources close automatically even if an error happens |
| Use PreparedStatement instead of Statement for values | Protects against SQL injection (Chapter 7) |
| Handle exceptions with try-catch (or throws) | A crash must never leave connections open |
| Put the driver JAR in classpath (or use a build tool) | The driver must be findable at run time |
| Use a connection pool in real applications | Creating a connection is expensive; reuse them |
In simple words: A professional never lets a connection leak. Every Connection, Statement and ResultSet you forget to close eats up a database connection, until the database refuses new ones.
The Professional Way — try-with-resources
Trainer's Note: This is the recommended modern style: put Connection, Statement and ResultSet inside the try brackets. When the try ends — normally or with an error — all three close by themselves. No con.close() needed, no forgotten resources.
Example06
📝 Key Takeaways
- Select ops (SELECT) use executeQuery() → ResultSet.
- Non-select ops (INSERT/UPDATE/DELETE) use executeUpdate() → row count.
- rs.next() moves the cursor; getInt/getString/getDouble read values.
- CRUD = Create(INSERT), Retrieve(SELECT), Update, Delete.
- Always close the connection after work.
- Use PreparedStatement for real applications (SQL injection safety).
🧠 Test Your Knowledge
1 QuestionsProgress: 0 / 1