The following example shows how a change can affect a where clause of an SQL statement based on the sensitivity of the ResultSet.
Some of the formatting in this example may be incorrect in order to fit this example on a printed page.
import java.sql.*;
public class Sensitive2 {
public Connection connection = null;
public static void main(java.lang.String[] args) {
Sensitive2 test = new Sensitive2();
test.setup();
test.run("sensitive");
test.cleanup();
test.setup();
test.run("insensitive");
test.cleanup();
}
public void setup() {
try {
System.out.println("Native JDBC used");
Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
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)");
try {
s.executeUpdate("drop table cujosql.sensitive2");
} catch (SQLException e) {
// Ignored.
}
s.executeUpdate("create table cujosql.sensitive2(col2 int)");
s.executeUpdate("insert into cujosql.sensitive2 values(1)");
s.executeUpdate("insert into cujosql.sensitive2 values(2)");
s.executeUpdate("insert into cujosql.sensitive2 values(3)");
s.executeUpdate("insert into cujosql.sensitive2 values(4)");
s.executeUpdate("insert into cujosql.sensitive2 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 col1, col2 From cujosql.sensitive,
cujosql.sensitive2 where col1 = col2");
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 four rows...");
// Another statement creates a value that does not fit the where clause.
Statement s2 =
connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATEABLE);
ResultSet rs2 = s2.executeQuery("select *
from cujosql.sensitive where col1 = 5 FOR UPDATE");
rs2.next();
rs2.updateInt(1, -1);
rs2.updateRow();
s2.close();
if (rs.next()) {
System.out.println("There is still a row: " + 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();
}
}
}