Recuperación de datos XML en aplicaciones JDBC
En las aplicaciones JDBC, se utilizan los métodos ResultSet.getXXX o ResultSet.getObject para recuperar datos de columnas XML.
En una aplicación JDBC, se pueden recuperar datos de columnas XML en una tabla de DB2 como datos textuales XML. Puede recuperar datos de columnas XML en una tabla como datos XML binarios (datos que están en el formato XML binario extensible dinámico XML DB2® cliente/servidor ), si el servidor de datos admite datos XML binarios.
- Utilice el método
ResultSet.getSQLXML para recuperar los datos. Luego utilice un método SQLXML.getXXX para
transformar los datos a un tipo de datos de salida compatible. Esta técnica requiere JDBC 4.0 o posterior.
Por ejemplo, se pueden recuperar datos mediante el método SQLXML.getBinaryStream o el método SQLXML.getSource.
- Utilice un método de acceso a datos ( ResultSet.getXXX ) distinto de ResultSet.getObject para recuperar los datos en un tipo de datos compatible.
- Utilice el método ResultSet.getObject para recuperar los datos y, a continuación, transpórtelos al tipo DB2Xml y asígnelos a un objeto DB2Xml . A continuación, utilice un método de conversión de datos ( DB2Xml.getDB2XXX ) o de transformación de datos ( DB2Xml.getDB2XmlXXX ) para recuperar los datos en un tipo de datos de salida compatible.
Debe utilizar esta técnica si no está utilizando una versión de IBM® Data Server Driver for JDBC and SQLJ que admita JDBC 4.0.
La tabla siguiente muestra los métodos ResultSet y sus correspondientes tipos de datos de salida para recuperar datos XML.
| Método | Tipo de datos de salida |
|---|---|
| ResultSet.getAsciiStream | InputStream |
| ResultSet.getBinaryStream | InputStream |
| ResultSet.getBytes | byte[] |
| ResultSet.getCharacterStream | Lector |
| ResultSet.getObject | Object |
| ResultSet.getSQLXML | SQLXML |
| ResultSet.getString | Serie |
| Método | Tipo de datos de salida | Tipo de declaración de codificación interna XML añadida |
|---|---|---|
| SQLXML.getBinaryStream | InputStream | Ninguno |
| SQLXML.getCharacterStream | Lector | Ninguno |
| SQLXML.getSource | Fuente 1 | Ninguno |
| SQLXML.getString | Serie | Ninguno |
| DB2Xml.getDB2AsciiStream | InputStream | Ninguno |
| DB2Xml.getDB2BinaryStream | InputStream | Ninguno |
| DB2Xml.getDB2Bytes | byte[] | Ninguno |
| DB2Xml.getDB2CharacterStream | Lector | Ninguno |
| DB2Xml.getDB2String | Serie | Ninguno |
| DB2Xml.getDB2XmlAsciiStream | InputStream | US-ASCII |
| DB2Xml.getDB2XmlBinaryStream | InputStream | Especificado por el parámetro getDB2XmlBinaryStream codificaciónDestino |
| DB2Xml.getDB2XmlBytes | byte[] | Especificado por el parámetro DB2Xml.getDB2XmlBytes codificaciónDestino |
| DB2Xml.getDB2XmlCharacterStream | Lector | ISO-10646-UCS-2 |
| DB2Xml.getDB2XmlString | Serie | ISO-10646-UCS-2 |
Nota:
|
||
Si la aplicación ejecuta la función XMLSERIALIZE en los datos que se deben devolver, después de la ejecución de la función, los datos tendrán el tipo especificado en la función XMLSERIALIZE, no el tipo de datos XML. Por consiguiente, el controlador maneja los datos como el tipo especificado y pasa por alto cualquier declaración de codificación interna.
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());
}
}