Example: Sensitive and insensitive ResultSets
The following example shows the difference between sensitive and insensitive ResultSets when rows are inserted into a table.
Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.
import java.sql.*;
public class Sensitive {
public Connection connection = null;
public static void main(java.lang.String[] args) {
Sensitive test = new Sensitive();
test.setup();
test.run("sensitive");
test.cleanup();
test.setup();
test.run("insensitive");
test.cleanup();
}
public void setup() {
try {
connection = DriverManager.getConnection("jdbc:db2:*local");
Statement s = connection.createStatement();
try {
s.executeUpdate("drop table cujosql.sensitive");
} catch (SQLException e) {
// Ignored.
}
s.executeUpdate("create table cujosql.sensitive(col1 int)");
s.executeUpdate("insert into cujosql.sensitive values(1)");
s.executeUpdate("insert into cujosql.sensitive values(2)");
s.executeUpdate("insert into cujosql.sensitive values(3)");
s.executeUpdate("insert into cujosql.sensitive values(4)");
s.executeUpdate("insert into cujosql.sensitive values(5)");
s.close();
} catch (Exception e) {
System.out.println("Caught exception: " + e.getMessage());
if (e instanceof SQLException) {
SQLException another = ((SQLException) e).getNextException();
System.out.println("Another: " + another.getMessage());
}
}
}
public void run(String sensitivity) {
try {
Statement s = null;
if (sensitivity.equalsIgnoreCase("insensitive")) {
System.out.println("creating a TYPE_SCROLL_INSENSITIVE cursor");
s = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
} else {
System.out.println("creating a TYPE_SCROLL_SENSITIVE cursor");
s = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
ResultSet rs = s.executeQuery("select * From cujosql.sensitive");
// Fetch the five values that are there.
rs.next();
System.out.println("value is " + rs.getInt(1));
rs.next();
System.out.println("value is " + rs.getInt(1));
rs.next();
System.out.println("value is " + rs.getInt(1));
rs.next();
System.out.println("value is " + rs.getInt(1));
rs.next();
System.out.println("value is " + rs.getInt(1));
System.out.println("fetched the five rows...");
// Note: If you fetch the last row, the ResultSet looks
// closed and subsequent new rows that are added
// are not be recognized.
// Allow another statement to insert a new value.
Statement s2 = connection.createStatement();
s2.executeUpdate("insert into cujosql.sensitive values(6)");
s2.close();
// Whether a row is recognized is based on the sensitivity setting.
if (rs.next()) {
System.out.println("There is a row now: " + rs.getInt(1));
} else {
System.out.println("No more rows.");
}
} catch (SQLException e) {
System.out.println("SQLException exception: ");
System.out.println("Message:....." + e.getMessage());
System.out.println("SQLState:...." + e.getSQLState());
System.out.println("Vendor Code:." + e.getErrorCode());
System.out.println("-------------------------------------");
e.printStackTrace();
}
catch (Exception ex) {
System.out.println("An exception other than an SQLException was thrown: ");
ex.printStackTrace();
}
}
public void cleanup() {
try {
connection.close();
} catch (Exception e) {
System.out.println("Caught exception: ");
e.printStackTrace();
}
}
}