This is an example of how to use distinct types.
/////////////////////////////////////////
// This example program shows examples of
// various common tasks that can be done
// with distinct types.
/////////////////////////////////////////
import java.sql.*;
public class Distinct {
public static void main(String[] args)
throws SQLException
{
// Register the native JDBC driver.
try {
Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
} catch (Exception e) {
System.exit(1); // Setup error.
}
Connection c = DriverManager.getConnection("jdbc:db2:*local");
Statement s = c.createStatement();
// Clean up any old runs.
try {
s.executeUpdate("DROP TABLE CUJOSQL.SERIALNOS");
} catch (SQLException e) {
// Ignore it and assume the table did not exist.
}
try {
s.executeUpdate("DROP DISTINCT TYPE CUJOSQL.SSN");
} catch (SQLException e) {
// Ignore it and assume the table did not exist.
}
// Create the type, create the table, and insert a value.
s.executeUpdate("CREATE DISTINCT TYPE CUJOSQL.SSN AS CHAR(9)");
s.executeUpdate("CREATE TABLE CUJOSQL.SERIALNOS (COL1 CUJOSQL.SSN)");
PreparedStatement ps = c.prepareStatement("INSERT INTO CUJOSQL.SERIALNOS VALUES(?)");
ps.setString(1, "399924563");
ps.executeUpdate();
ps.close();
// You can obtain details about the types available with new metadata in
// JDBC 2.0
DatabaseMetaData dmd = c.getMetaData();
int types[] = new int[1];
types[0] = java.sql.Types.DISTINCT;
ResultSet rs = dmd.getUDTs(null, "CUJOSQL", "SSN", types);
rs.next();
System.out.println("Type name " + rs.getString(3) +
" has type " + rs.getString(4));
// Access the data you have inserted.
rs = s.executeQuery("SELECT COL1 FROM CUJOSQL.SERIALNOS");
rs.next();
System.out.println("The SSN is " + rs.getString(1));
c.close(); // Connection close also closes stmt and rs.
}
}