Ability to analyze results and knowledge of problem determination

[Note]

XML data retrieval in JDBC applications

The In JDBC applications, you use ResultSet.getXXX or ResultSet.getObject methods to retrieve data from XML columns.

When you retrieve data from XML columns of a DB2 table, the output data is in the serialized string format. This is true whether you retrieve the entire contents of an XML column or a sequence from the column.

You can use one of the following techniques to retrieve XML data:

The table below lists the ResultSet methods and corresponding output data types for retrieving XML data.

Table 7.1.  ResultSet methods and data types for retrieving XML data

MethodOutput data type

ResultSet.getAsciiStream

InputStream

ResultSet.getBinaryStream

InputStream

ResultSet.getBytes

byte[]

ResultSet.getCharacterStream

Reader

ResultSet.getObject

DB2Xml

ResultSet.getString

String

The table below lists the methods and corresponding output data types for retrieving data from a DB2Xml object, as well as the type of encoding in the XML declaration that the driver adds to the output data:

Table 7.2.  DB2Xml methods, data types, and added encoding specifications

MethodOutput data typeType of XML internal encoding declaration added

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 DB2Xml.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

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()
  {
    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("fetchToObject: 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("fetchToString: SQL Exception: " +
        sqle.getMessage());
      System.out.println("fetchToString: SQL State: " +
        sqle.getSQLState());
      System.out.println("fetchToString: SQL Error Code: " +
        sqle.getErrorCode());
    }
  }
					

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     SCDJWS 5.0 Quiz