This section describes how to update existing data rows with UPDATE statements.
UPDATE statements are also commonly used by database applications when users making changes to field values on the UI.
An UPDATE statement 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
/**
* DerbyUpdateRows.java
* Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
*/
import java.sql.*;
public class DerbyUpdateRows {
public static void main(String [] args) {
Connection con = null;
try {
con = DriverManager.getConnection(
"jdbc:derby://localhost/TestDB");
Statement sta = con.createStatement();
// updating multiple rows
int count = sta.executeUpdate(
"UPDATE Profile SET ModTime = '2007-04-01 23:59:59.999'"
+ " WHERE ID > 6");
System.out.println("Number of rows updated: "+count);
// Checking updated rows
ResultSet res = sta.executeQuery(
"SELECT * FROM Profile");
System.out.println("List of Profiles: ");
while (res.next()) {
System.out.println(
" "+res.getInt("ID")
+ ", "+res.getString("FirstName")
+ ", "+res.getString("LastName")
+ ", "+res.getFloat("Point")
+ ", "+res.getDate("BirthDate")
+ ", "+res.getTimestamp("ModTime"));
}
res.close();
sta.close();
con.close();
} catch (Exception e) {
System.err.println("Exception: "+e.getMessage());
}
}
}
The output confirms that 7 rows were updated with a new timestamp in the ModTime column: