IBM DB2 Manual page 85

Table of Contents

Advertisement

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.
Example: The following example demonstrates retrieving data from an XML
column into an SQLXML object, and then using the SQLXML.getString method to
retrieve the data into a string.
public void fetchToSQLXML()
{
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();
String 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());
}
}
Example: The following example demonstrates retrieving data from an XML
column into a String variable.
public void fetchToString()
{
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();
Chapter 3. JDBC application programming
69

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents