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

Java Generics

Java ForkJoinPool

P2P Networks Introduction



















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next