Java Reference
In-Depth Information
// check whether the RowSet can be updated
if(rowSet.getConcurrency()==ResultSet.CONCUR_UPDATABLE)
System.out.println("Rowset is UPDATABLE");
else
System.out.println("Rowset is READ_ONLY");
// update the record and output it to the console
while (rowSet.next()){
rowSet.updateString("FName", "Igor");
rowSet.updateRow();
for(int j=1; j<=rowSet.getMetaData().getColumnCount(); j++){
System.out.print( rowSet.getObject(j)+"\t");
}
System.out.println();
}
rowSet.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
Updating a RowSet
As you can see from the code in
Listing 18-3
, the use of an updatable
RowSet
is considerably simpler
than using the SQL UPDATE command with a conventional
Statement.executeUpdate()
.This is
particularly true when you consider that updates made to an updatable
RowSet
always affect the
current row, so there is no need to find the row to update. Of course, this does mean that you must
make sure you have moved the cursor to the correct row prior to making an update.
RowSet
updates use the update methods inherited from the
ResultSet
. Most of the
ResultSet.update
methods take two parameters: the column to update and the new value to put in
that column. The column may be specified using either the column name or the column number.
Table 18-2
summarizes the update methods for the
ResultSet
, showing only the variant using column
name as the specifier for reasons of space.
Table 18-2: 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)