IBM DB2 Manual page 59

Table of Contents

Advertisement

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Testing whether the current row of a ResultSet is a delete or update hole in a
JDBC application:
If a ResultSet has the TYPE_SCROLL_SENSITIVE attribute, and the underlying
cursor is SENSITIVE STATIC, you need to test for delete or update holes before
you attempt to retrieve rows of the ResultSet.
After a SENSITIVE STATIC ResultSet is opened, it does not change size. This
means that deleted rows are replaced by placeholders, which are also called holes.
If updated rows no longer fit the criteria for the ResultSet, those rows also become
holes. You cannot retrieve rows that are holes.
To test whether the current row in a ResultSet is a delete hole or update hole,
follow these steps:
1. Call the DatabaseMetaData.deletesAreDetected or
DatabaseMetaData.updatesAreDetected method with the
TYPE_SCROLL_SENSITIVE argument to determine whether the data source
creates holes for a TYPE_SCROLL_SENSITIVE ResultSet.
2. If DatabaseMetaData.deletesAreDetected or
DatabaseMetaData.updatesAreDetected returns true, which means that the data
source can create holes, call the ResultSet.rowDeleted or ResultSet.rowUpdated
method to determine whether the current row is a delete or update hole. If the
method returns true, the current row is a hole.
The following code tests whether the current row is a delete hole.
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs =
stmt.executeQuery("SELECT EMPNO FROM EMPLOYEE FOR UPDATE OF PHONENO");
DatabaseMetaData dbmd = con.getMetaData();
boolean dbSeesDeletes =
dbmd.deletesAreDetected(ResultSet.TYPESCROLL_SENSITIVE);
rs.afterLast();
while (rs.previous()) {
if (dbSeesDeletes) {
if (!(rs.rowDeleted()))
{
s = rs.getString("EMPNO");
System.out.println("Employee number = " + s);
}
}
}
rs.close();
stmt.close();
Inserting a row into a ResultSet in a JDBC application:
If a ResultSet has a resultSetConcurrency attribute of CONCUR_UPDATABLE, you
can insert rows into the ResultSet.
To insert a row into a ResultSet, follow these steps:
// Create a Statement object
// for a scrollable, updatable
// ResultSet
// Create the ResultSet
// Create the DatabaseMetaData object
// Can the database see delete holes?
// Position the cursor at the end of
// the ResultSet
// Position the cursor backward
// If delete holes can be detected
// If this row is not a delete hole
// Retrieve the employee number
// Print the column value
// Close the ResultSet
// Close the Statement
Chapter 3. JDBC application programming
43

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents