Java Reference
In-Depth Information
To appreciate the simplicity of using UpdatableResultSet instead of SQL UPDATES,
it is worth looking first at what is involved in using Statement.executeUpdate() to
change a customer address. The code to make this change looks like this:
stmt.executeUpdate(
"UPDATE Customers SET Street = '123 Main Street' +
"WHERE First_Name = 'Vito' AND Last_Name = 'Corleone'");
This is simple enough when you know how to identify the record to be updated, but
consider how much more complicated it would be if your application were displaying
the ResultSet in a JTable. Unless you go to considerable trouble to keep track of the
current record, it is quite difficult to identify to the RDBMS which record to update.
Using an UpdatableResultSet simplifies the situation considerably. All you need to do
is set the cursor to the desired row and change the column value using a
data-type-specific update method. Here's an example:
rs.updateString("Street", "123 Main");
Since updates made to an UpdatableResultSet always affect the current row, you
must make sure you have moved the cursor to the correct row prior to making an
update.
Most of the ResultSet.update methods take two parameters: the column to update
and the new value to put in that column. As with the getter methods, the column may
be specified using either the column name or the column number.
Table 4 -4 summarizes the update methods for the UpdatableResultSet, showing only
the variant using column name as the specifier for reasons of space.
Note A special update method, updateNull(), is used for setting column values
to NULL.
Table 4-4: ResultSet Update Methods
Data Type
Method
BigDecimal
updateBigDecimal(String columnName, BigDecimal x)
boolean
updateBoolean(String columnName, boolean x)
byte
updateByte(String columnName, byte x)
byte[]
updateBytes(String columnName, byte[] x)
double
updateDouble(String columnName, double x)
float
updateFloat(String columnName, float x)
int
updateInt(String columnName, int x)
Search WWH ::




Custom Search