In a JDBC program, you can create a distinct type using the executeUpdate method
to execute the CREATE DISTINCT TYPE statement. You can also use
executeUpdate to create a table that includes a column of that type. When you
retrieve data from a column of that type, or update a column of that type, you use
Java identifiers with data types that correspond to the built-in types on which the
distinct types are based.
The following example creates a distinct type that is based on an INTEGER type,
creates a table with a column of that type, inserts a row into the table, and
retrieves the row from the table:
Related reference
“Data types that map to database data types in Java applications” on page 193
″CREATE TYPE″ (DB2 SQL Reference)
Savepoints in JDBC applications
An SQL savepoint represents the state of data and schemas at a particular point in
time within a unit of work. There are SQL statements to set a savepoint, release a
savepoint, and restore data and schemas to the state that the savepoint represents.
The IBM Data Server Driver for JDBC and SQLJ supports the following methods
for using savepoints:
Connection.setSavepoint() or Connection.setSavepoint(String name)
Sets a savepoint. These methods return a Savepoint object that is used in later
releaseSavepoint or rollback operations.
When you execute either of these methods, DB2 executes the form of the
SAVEPOINT statement that includes ON ROLLBACK RETAIN CURSORS.
Connection con;
Statement stmt;
ResultSet rs;
String empNumVar;
int shoeSizeVar;
...
stmt = con.createStatement(); // Create a Statement object
stmt.executeUpdate(
"CREATE DISTINCT TYPE SHOESIZE AS INTEGER");
// Create distinct type
stmt.executeUpdate(
"CREATE TABLE EMP_SHOE (EMPNO CHAR(6), EMP_SHOE_SIZE SHOESIZE)");
// Create table with distinct type
stmt.executeUpdate("INSERT INTO EMP_SHOE " +
"VALUES ('000010', 6)"); // Insert a row
rs=stmt.executeQuery("SELECT EMPNO, EMP_SHOE_SIZE FROM EMP_SHOE);
// Create ResultSet for query
while (rs.next()) {
empNumVar = rs.getString(1); // Get employee number
shoeSizeVar = rs.getInt(2); // Get shoe size (use int
// because underlying type
// of SHOESIZE is INTEGER)
System.out.println("Employee number="+empNumVar +
" Shoe size="+shoeSizeVar);
}
rs.close(); // Close ResultSet
stmt.close(); // Close Statement
Figure 16. Creating and using a distinct type
Chapter 3. JDBC application programming 57