Java Reference
In-Depth Information
con.setAutoCommit(false);
Statement stmt = con.createStatement();
stmt.execute(SQLCommand);
//con.commit();
con.rollback();
con.close();
}
You can check to see if the
UPDATE
has been executed by inserting a
SELECT
statement to read the
updated value of the Street field after the update command is executed but before it is rolled back. The
try block now looks like this:
try {
Connection con = DriverManager.getConnection(url);
con.setAutoCommit(false);
Statement stmt = con.createStatement();
stmt.execute(SQLCommand);
String query = "SELECT Street FROM Contact_Info "+
"WHERE First_Name = 'Michael' AND Last_Name ='Corleone';";
ResultSet rs = stmt.executeQuery(query);
rs.next();
System.out.println("Street = "+rs.getString(1));
con.rollback();
con.close();
}
When you run this version, it shows that the new value of Street matches the update, but when you
look in the database, the previous value is still there because the change has been rolled back.
Cross-Reference
RecordSets
and the
SELECT
are discussed in detail in
Chapter 7
.
The DELETE Statement
The last data-manipulation command is
DELETE
, which is used for deleting entire records or groups of
records. Again, when using the
DELETE
command, you use a
WHERE
clause to identify the records to
be deleted.