IBM DB2 Manual page 86

Table of Contents

Advertisement

Example: The following example demonstrates retrieving data from an XML
column into a DB2Xml object, and then using the DB2Xml.getDB2XmlString method
to retrieve the data into a string with an added XML declaration with an
ISO-10646-UCS-2 encoding specification.
public void fetchToDB2Xml()
70
Application Programming Guide and Reference for Java
String 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());
}
}
{
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();
String 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());
}
}
Related concepts
"XML data in JDBC applications" on page 65
Related reference
"Data types that map to database data types in Java applications" on page 193

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents