- Back to Home »
- JDBC Update the Database
In order to update the database you need to use a
Statement
. But, instead of calling theexecuteQuery()
method, you call the executeUpdate()
method.
There are two types of updates you can perform on a database:
- Update record values
- Delete records
The
executeUpdate()
method is used for both of these types of updates.Updating Records
Here is an update record value example:
Statement statement = connection.createStatement(); String sql = "update people set name='John' where id=123"; int rowsAffected = statement.executeUpdate(sql);
The
rowsAffected
returned by the statement.executeUpdate(sql)
call, tells how many records in the database were affected by the SQL statement.Deleting Records
Here is a delete record example:
Statement statement = connection.createStatement(); String sql = "delete from people where id=123"; int rowsAffected = statement.executeUpdate(sql);
Again, the
rowsAffected
returned by the statement.executeUpdate(sql)
call, tells how many records in the database were affected by the SQL statement.