Learning about parameter and ResultSet columns that are defined with code units
The IBM® Data Server Driver for JDBC and SQLJ provides methods that you can use to determine the number of bits in a code unit for parameters or ResultSet columns that are defined with the OCTETS, CODEUNITS16, or CODEUNITS32 attribute.
About this task
The standard JDBC java.sql.ParameterMetaData and java.sql.ResultSetMetaData methods return information about lengths of the parameters or columns only in code units. That information is summarized in the following table.
Method | Length and code unit information returned |
---|---|
java.sql.ParameterMetaData.getPrecision | Returns the parameter length in code units |
java.sql.ResultSetMetaData.getColumnDisplaySize | Returns the column length in code units |
java.sql.ResultSetMetaData.getPrecision | Returns the column length in code units |
The IBM Data Server Driver for JDBC and SQLJ provides methods that return the number of bits in a code unit for columns or parameters that are defined in code units. Those methods are:
- com.ibm.db2.jcc.DB2ParameterMetaData.getMaxStringUnitBits
- com.ibm.db2.jcc.DB2ResultSetMetaData.getMaxStringUnitBits
Procedure
To determine the number of bits in a parameter or in a ResultSet column that is defined in code units, follow these steps.
- Use one of the standard JDBC methods to retrieve the length in code units.
- Retrieve the number of bits in a code unit with the DB2ParameterMetaData.getCodeUnitScalingFactor or DB2ResultSetMetaData.getCodeUnitScalingFactor method.
- Multiply the two values to get the length of the parameter or ResultSet column in bits.
Examples
Example: Suppose that you want to insert values into a table is defined like this:
CREATE TABLE MYTABLE (
CHARCOL CHAR(9 OCTETS),
VARCHARCOL VARCHAR(9 CODEUNITS32));
The following code demonstrates how to determine the length in bits of the parameters that are defined in code units. The numbers to the right of selected statements correspond to the previously described steps.
…
PreparedStatement pstmt =
con.prepareStatement("INSERT INTO MYTABLE VALUES (?,?)");
ParameterMetaData pmd = pstmt.getParameterMetaData();
int pCount = pmd.getParameterCount();
int pLenInCodeUnits;
int pBitsPerCodeUnit;
for (int j=1;j<=pCount;j++)
{
System.out.println("***********************************************");
System.out.println("Parameter type = " +
pmd.getParameterTypeName(j));
pLenInCodeUnits = pmd.getPrecision(j)); 1
System.out.println("Length in code units = " + pLenInCodeUnits);
pBitsPerCodeUnit =
(com.ibm.db2.jcc.DB2ParameterMetaData(pmd)).getMaxStringUnitBits(); 2
System.out.println("Bits per code unit = " + pBitsPerCodeUnit);
System.out.println("Length in bits = " + pLenInCodeUnits*pBitsPerCodeUnit); 3
}
The following results are returned:
***********************************************
Parameter type = CHAR
Length in code units = 9
Scaling factor = 1
Length in bytes = 9
***********************************************
Parameter type = VARCHAR
Length in code units = 9
Scaling factor = 4
Length in bytes = 36