XML data retrieval in JDBC applications
In JDBC applications, you use ResultSet.getXXX or ResultSet.getObject methods to retrieve data from XML columns.
In a JDBC application, you can retrieve data from XML columns in a Db2® table as XML textual data. You can retrieve data from XML columns in a table as binary XML data (data that is in the Extensible Dynamic Binary XML Db2 Client/Server Binary XML Format), if the data server supports binary XML data.
- Use the ResultSet.getSQLXML method to retrieve
the data. Then use a SQLXML.getXXX method to retrieve
the data into a compatible output data type. This technique requires
JDBC 4.0 or later.
For example, you can retrieve data by using the SQLXML.getBinaryStream method or the SQLXML.getSource method.
- Use a ResultSet.getXXX method other than ResultSet.getObject to retrieve the data into a compatible data type.
- Use the ResultSet.getObject method to retrieve
the data, and then cast it to the DB2Xml type and
assign it to a DB2Xml object. Then use a DB2Xml.getDB2XXX or DB2Xml.getDB2XmlXXX method
to retrieve the data into a compatible output data type.
You need to use this technique if you are not using a version of the IBM® Data Server Driver for JDBC and SQLJ that supports JDBC 4.0.
The following table lists the ResultSet methods and corresponding output data types for retrieving XML data.
Method | Output data type |
---|---|
ResultSet.getAsciiStream | InputStream |
ResultSet.getBinaryStream | InputStream |
ResultSet.getBytes | byte[] |
ResultSet.getCharacterStream | Reader |
ResultSet.getObject | Object |
ResultSet.getSQLXML | SQLXML |
ResultSet.getString | String |
Method | Output data type | Type of XML internal encoding declaration added |
---|---|---|
SQLXML.getBinaryStream | InputStream | None |
SQLXML.getCharacterStream | Reader | None |
SQLXML.getSource | Source1 | None |
SQLXML.getString | String | None |
DB2Xml.getDB2AsciiStream | InputStream | None |
DB2Xml.getDB2BinaryStream | InputStream | None |
DB2Xml.getDB2Bytes | byte[] | None |
DB2Xml.getDB2CharacterStream | Reader | None |
DB2Xml.getDB2String | String | None |
DB2Xml.getDB2XmlAsciiStream | InputStream | US-ASCII |
DB2Xml.getDB2XmlBinaryStream | InputStream | Specified by getDB2XmlBinaryStream targetEncoding parameter |
DB2Xml.getDB2XmlBytes | byte[] | Specified by DB2Xml.getDB2XmlBytes targetEncoding parameter |
DB2Xml.getDB2XmlCharacterStream | Reader | ISO-10646-UCS-2 |
DB2Xml.getDB2XmlString | String | ISO-10646-UCS-2 |
Note:
|
If the application executes the XMLSERIALIZE function on the data that is to be returned, after execution of the function, the data has the data type that is specified in the XMLSERIALIZE function, not the XML data type. Therefore, the driver handles the data as the specified type and ignores any internal encoding declarations.
public void fetchToSQLXML(long cid, java.sql.Connection conn)
{
System.out.println(">> fetchToSQLXML: Get XML data as an SQLXML object " +
"using getSQLXML");
PreparedStatement selectStmt = null;
String sqls = null, stringDoc = null;
ResultSet rs = null;
try{
sqls = "SELECT info FROM customer WHERE cid = " + cid;
selectStmt = conn.prepareStatement(sqls);
rs = selectStmt.executeQuery();
// Get metadata
// Column type for XML column is the integer java.sql.Types.OTHER
ResultSetMetaData meta = rs.getMetaData();
int colType = meta.getColumnType(1);
System.out.println("fetchToSQLXML: Column type = " + colType);
while (rs.next()) {
// Retrieve the XML data with getSQLXML.
// Then write it to a string with
// explicit internal ISO-10646-UCS-2 encoding.
java.sql.SQLXML xml = rs.getSQLXML(1);
System.out.println (xml.getString());
}
rs.close();
}
catch (SQLException sqle) {
System.out.println("fetchToSQLXML: SQL Exception: " +
sqle.getMessage());
System.out.println("fetchToSQLXML: SQL State: " +
sqle.getSQLState());
System.out.println("fetchToSQLXML: SQL Error Code: " +
sqle.getErrorCode());
}
}
String sql = "SELECT INFO FROM Customer WHERE Cid='1000'";
PreparedStatement pstmt = con.prepareStatement(sql);
ResultSet resultSet = pstmt.executeQuery();
// Get the result XML as a binary stream
SQLXML sqlxml = resultSet.getSQLXML(1);
InputStream binaryStream = sqlxml.getBinaryStream();
public void fetchToString(long cid, java.sql.Connection conn)
{
System.out.println(">> fetchToString: Get XML data " +
"using getString");
PreparedStatement selectStmt = null;
String sqls = null, stringDoc = null;
ResultSet rs = null;
try{
sqls = "SELECT info FROM customer WHERE cid = " + cid;
selectStmt = conn.prepareStatement(sqls);
rs = selectStmt.executeQuery();
// Get metadata
// Column type for XML column is the integer java.sql.Types.OTHER
ResultSetMetaData meta = rs.getMetaData();
int colType = meta.getColumnType(1);
System.out.println("fetchToString: Column type = " + colType);
while (rs.next()) {
stringDoc = rs.getString(1);
System.out.println("Document contents:");
System.out.println(stringDoc);
}
catch (SQLException sqle) {
System.out.println("fetchToString: SQL Exception: " +
sqle.getMessage());
System.out.println("fetchToString: SQL State: " +
sqle.getSQLState());
System.out.println("fetchToString: SQL Error Code: " +
sqle.getErrorCode());
}
}
public void fetchToDB2Xml(long cid, java.sql.Connection conn)
{
System.out.println(">> fetchToDB2Xml: Get XML data as a DB2XML object " +
"using getObject");
PreparedStatement selectStmt = null;
String sqls = null, stringDoc = null;
ResultSet rs = null;
try{
sqls = "SELECT info FROM customer WHERE cid = " + cid;
selectStmt = conn.prepareStatement(sqls);
rs = selectStmt.executeQuery();
// Get metadata
// Column type for XML column is the integer java.sql.Types.OTHER
ResultSetMetaData meta = rs.getMetaData();
int colType = meta.getColumnType(1);
System.out.println("fetchToDB2Xml: Column type = " + colType);
while (rs.next()) {
// Retrieve the XML data with getObject, and cast the object
// as a DB2Xml object. Then write it to a string with
// explicit internal ISO-10646-UCS-2 encoding.
com.ibm.db2.jcc.DB2Xml xml =
(com.ibm.db2.jcc.DB2Xml) rs.getObject(1);
System.out.println (xml.getDB2XmlString());
}
rs.close();
}
catch (SQLException sqle) {
System.out.println("fetchToDB2Xml: SQL Exception: " +
sqle.getMessage());
System.out.println("fetchToDB2Xml: SQL State: " +
sqle.getSQLState());
System.out.println("fetchToDB2Xml: SQL Error Code: " +
sqle.getErrorCode());
}
}