Java Reference
In-Depth Information
Specifying that a result set be updatable does not guarantee that the
result set you get will actually be updatable. Drivers that do not support
updatable result sets will return one that is read-only. In addition, to get
an updatable result set, the query must generally specify the primary
key as one of the columns selected, and it should select columns from
only one table.
Caution
Since requesting an UpdatableResultSet does not guarantee that you will actually get
one, depending on the driver in use, you should check whether the ResultSet is
updatable using ResultSet.getConcurrency(). Listing 4-8 illustrates opening a
scrollable updatable ResultSet and using getConcurrency to ensure that it is
updatable.
Listing 4-8: Opening an updatable ResultSet
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection ("jdbc:odbc: Contacts");
Statement stmt = con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData md = rs.getMetaData();
if(rs.getConcurrency()==ResultSet.CONCUR_UPDATABLE)
System.out.println("UPDATABLE");
else
System.out.println("READ_ONLY");
int nColumns = md.getColumnCount();
for(int i=1;i<=nColumns;i++){
System.out.print(md.getColumnLabel(i)+((i==nColumns)?"\n":"\t"));
}
while (rs.next()) {
rs.updateString("Street", "123 Main");
rs.updateRow();
for(int i=1;i<=nColumns;i++){
System.out.print(rs.getString(i)+((i==nColumns)?"\n":"\t"));
}
}
If the driver does not support the definition of UpdatableResultSet, the Statement
object may throw a SQL "Optional feature not implemented" exception.
Updating a ResultSet
Search WWH ::




Custom Search