JDBC Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 2.11

"UPDATE" Statements

This section describes how to update existing data rows with UPDATE statements.

UPDATE statements are also commonly used by database applications when using users making changes to field values on the UI. An UPDATE statements should have a SET clause to provide a list of columns with new values and a WHERE clause to identify the rows to be updated.

UPDATE statements should be executed with the executeUpdate() method. The Java tutorial program below shows you how to update

/**
 * UpdateRows.java
 * Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
 */
import java.sql.*;
public class UpdateRows {
  public static void main(String [] args) {
    Connection con = null;
    try {
      Class.forName(
        "com.microsoft.sqlserver.jdbc.SQLServerDriver");
      con = DriverManager.getConnection(
          "jdbc:sqlserver://localhost:1269;"
        + "user=sa;password=HerongYang;"
        + "database=AdventureWorksLT");
      Statement sta = con.createStatement(); 

// updating multiple rows
      int count = sta.executeUpdate(
        "UPDATE Herong.Profile SET LastAccessDate = '2007-04-01'"
        + " WHERE UserID > 6");
      System.out.println("Number of rows updated: "+count);

// getting the data back
      ResultSet res = sta.executeQuery(
        "SELECT * FROM Herong.Profile");
      System.out.println("List of Profiles: "); 
      while (res.next()) {
         System.out.println(
           "  "+res.getInt("UserID")
           + ", "+res.getString("FirstName")
           + ", "+res.getString("LastName")
           + ", "+res.getDate("LastAccessDate"));
      }
      res.close();

      sta.close();
      con.close();        
    } catch (java.lang.ClassNotFoundException e) {
      System.err.println("ClassNotFoundException: "
        +e.getMessage());
    } catch (SQLException e) {
      System.err.println("SQLException: "
        +e.getMessage());
    }
  }
}

The output confirms that 5 rows were updated with a new date in the LastAccessDate column:

Number of rows updated: 5
List of Profiles:
  13, Herong, Yang, 2007-04-01
  1, Orlando, Gee, 2004-10-13
  2, Keith, Harris, 2004-10-13
  3, Donna, Carreras, 2004-10-13
  4, Janet, Gates, 2004-10-13
  5, Lucy, Harrington, 2004-10-13
  6, Rosmarie, Carroll, 2004-10-13
  7, Dominic, Gash, 2007-04-01
  10, Kathleen, Garza, 2007-04-01
  11, Katherine, Harding, 2007-04-01
  12, Johnny, Caprio, 2007-04-01

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 Downloading and Installing JDK - Java SE

 Installing and Running Java DB - Derby

 Derby (Java DB) JDBC Driver

 Derby (Java DB) JDBC DataSource Objects

 Java DB (Derby) - DML Statements

 Java DB (Derby) - ResultSet Objects of Queries

 Java DB (Derby) - PreparedStatement

 MySQL Installation on Windows

 MySQL JDBC Driver (MySQL Connector/J)

 MySQL - PreparedStatement

 MySQL - Reference Implementation of JdbcRowSet

 MySQL - JBDC CallableStatement

 MySQL CLOB (Character Large Object) - TEXT

 MySQL BLOB (Binary Large Object) - BLOB

 Oracle Express Edition Installation on Windows

 Oracle JDBC Drivers

 Oracle - Reference Implementation of JdbcRowSet

 Oracle - PreparedStatement

 Oracle - JBDC CallableStatement

 Oracle CLOB (Character Large Object) - TEXT

 Oracle BLOB (Binary Large Object) - BLOB

 Microsoft SQL Server 2005 Express Edition

 Microsoft JDBC Driver for SQL Server - sqljdbc.jar

 Microsoft JDBC Driver - Query Statements and Result Sets

 Microsoft JDBC Driver - DatabaseMetaData Object

 Microsoft JDBC Driver - DDL Statements

Microsoft JDBC Driver - DML Statements

 "SELECT ... INTO" Statements

 "INSERT INTO" Statements

 "INSERT INTO" Statements with INDENTITY Columns

"UPDATE" Statements

 "DELETE FROM" Statements

 SQL Server - PreparedStatement

 SQL Server CLOB (Character Large Object) - TEXT

 SQL Server BLOB (Binary Large Object) - BLOB

 JDBC-ODBC Bridge Driver - sun.jdbc.odbc.JdbcOdbcDriver

 JDBC-ODBC Bridge Driver - Flat Text Files

 JDBC-ODBC Bridge Driver - MS Access

 JDBC-ODBC Bridge Driver - MS SQL Server

 Summary of JDBC Drivers and Database Servers

 Additional Tutorial Notes to Be Added

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2007
"UPDATE" Statements