JDBC: Update the Database

Jakob Jenkov
Last update: 2014-06-23

In order to update the database you need to use a Statement. But, instead of calling the executeQuery() method, you call the executeUpdate() method.

There are two types of updates you can perform on a database:

  1. Update record values
  2. 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.

Jakob Jenkov

Featured Videos















Core Software Performance Optimization Principles




Advertisements

High-Performance
Java Persistence
Close TOC

All Trails

Trail TOC

Page TOC

Previous

Next