INSERT, UPDATE, DELETE
SELECT queries read data and hand you back a ResultSet. INSERT, UPDATE, and DELETE are different —
they change data instead of reading it, and JDBC gives you a separate method for that: Statement.executeUpdate(sql).
This page covers all three write operations and how to confirm they actually did something.
executeUpdate() Returns a Row Count, Not a ResultSet
Any SQL statement that modifies data — INSERT, UPDATE, or DELETE — is run with
executeUpdate(sql) instead of executeQuery(sql). It does not return a ResultSet
because there are no rows to read back. Instead, it returns an int: the number of rows that were affected
by the statement.
| Method | Used for | Returns |
|---|---|---|
executeQuery(sql) | SELECT | ResultSet |
executeUpdate(sql) | INSERT, UPDATE, DELETE | int (rows affected) |
INSERT: Adding a New Row
An INSERT adds a brand-new row to a table. The row count returned is almost always 1 for a single-row insert.
import java.sql.*;
public class InsertExample {
public static void main(String[] args) throws Exception {
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/coding_with_sonu", "root", "");
Statement stmt = conn.createStatement();
String sql = "INSERT INTO students (name, course) VALUES ('Amit', 'JDBC')";
int rowsInserted = stmt.executeUpdate(sql);
System.out.println(rowsInserted + " row(s) inserted.");
stmt.close(); conn.close();
}
}
UPDATE: Modifying Existing Rows
An UPDATE changes values in rows that already exist. The row count tells you how many rows matched the
WHERE clause and were changed — this could be 0, 1, or many.
String sql = "UPDATE students SET course = 'Advanced JDBC' WHERE name = 'Amit'";
int rowsUpdated = stmt.executeUpdate(sql);
System.out.println(rowsUpdated + " row(s) updated.");
DELETE: Removing Rows
A DELETE removes rows that match a condition. Just like UPDATE, the row count tells you how many rows were actually removed.
String sql = "DELETE FROM students WHERE name = 'Amit'";
int rowsDeleted = stmt.executeUpdate(sql);
System.out.println(rowsDeleted + " row(s) deleted.");
Always Check the Row Count
The int returned by executeUpdate() is not just informational — it's how you know whether
your statement actually matched anything. An UPDATE or DELETE with a WHERE clause
that matches nothing will not throw an exception; it will simply return 0. If your code assumes the
operation succeeded without checking this, you can silently miss bugs like a typo'd ID or a row that was already deleted.
String sql = "UPDATE students SET course = 'Advanced JDBC' WHERE name = 'Ghost'";
int rowsAffected = stmt.executeUpdate(sql);
if (rowsAffected == 0) {
System.out.println("No matching row found - nothing was updated.");
} else {
System.out.println(rowsAffected + " row(s) updated.");
}
| Row count value | What it tells you |
|---|---|
0 | Nothing matched the WHERE clause — good to treat as "not found" |
1 | Exactly one row was affected — the normal case for a single-record insert/update/delete |
| more than 1 | Your WHERE clause matched multiple rows — make sure that was intentional |