4 DB2 Driver : Returning and Inserting/Updating XML Data

Returning and Inserting/Updating XML Data
For DB2 V9.1 and higher for Linux/UNIX/Windows, the DB2 driver supports the XML data type. Which JDBC data type the XML data type is mapped to depends on whether the JDBCBehavior and XMLDescribeType properties are set:
■
If XMLDescribeType=clob or XMLDescribeType=blob, the driver maps the XML data type to the JDBC CLOB or BLOB data type, respectively, regardless of the setting of the JDBCBehavior property.
■
■
If JDBCBehavior=0 and the XMLDescribeType property is not set, XML data is mapped to SQLXML or CLOB, depending on which JVM your application is using. The driver maps the XML data type to the JDBC SQLXML data type if your application is using Java SE 6. If your application is using an earlier JVM, the driver maps the XML data type to the JDBC CLOB data type.
Returning XML Data
You can specify whether XML data is returned as character or binary data by setting the XMLDescribeType property. For example, consider a database table defined as:
CREATE TABLE xmlTable (id int, xmlCol xml NOT NULL)
and the following code:
String sql="SELECT xmlCol FROM xmlTable";
ResultSet rs=stmt.executeQuery(sql);
If your application uses the following connection URL, which specifies that the XML data type be mapped to the BLOB data type, the driver would return XML data as binary data:
jdbc:datadirect:db2://server1:50000;DatabaseName=jdbc;User=test;
Password=secret;XMLDescribeType=blob
Character Data
When XMLDescribeType=clob, XML data is returned as character data. The result set column is described with a column type of CLOB and the column type name is xml.
When XMLDescribeType=clob, your application can use the following methods to return data stored in XML columns as character data:
■
■
■
■
■
The driver converts the XML data returned from the database server from the UTF-8 encoding used by the database server to the UTF-16 Java String encoding.
Your application can use the following method to return data stored in XML columns as ASCII data:
■
The driver converts the XML data returned from the database server from the UTF-8 encoding to the ISO-8859-1 (latin1) encoding.
NOTE: The conversion caused by using the getAsciiStream() method may create XML that is not well-formed because the content encoding is not the default encoding and does not contain an XML declaration specifying the content encoding. Do not use the getAsciiStream() method if your application requires well-formed XML.
When XMLDescribeType=blob, your application should not use any of the methods for returning character data described in this section. In this case, the driver applies the standard JDBC character-to-binary conversion to the data, which returns the hexadecimal representation of the character data.
Binary Data
When XMLDescribeType=blob, the driver returns XML data as binary data. The result set column is described with a column type of BLOB and the column type name is xml.
When XMLDescribeType=blob, your application can use the following methods to return XML data as binary data:
■
■
■
■
■
■
■
The driver does not apply any data conversions to the XML data returned from the database server. These methods return a byte array or binary stream that contains the XML data encoded as UTF-8.
When XMLDescribeType=clob, your application should not use any of the methods for returning binary data described in this section. In this case, the driver applies the standard JDBC binary-to-character conversion to the data, which returns the hexadecimal representation of the binary data.
Inserting/Updating XML Data
The driver can insert or update XML data as character or binary data regardless of the setting of the XMLDescribeType connection property.
Character Data
Your application can use the following methods to insert or update XML data as character data:
■
■
■
■
■
■
■
■
The driver converts the character representation of the data to the XML character set used by the database server and sends the converted XML data to the server. The driver does not parse or remove any XML processing instructions.
Your application can update XML data as ASCII data using the following methods:
■
■
The driver interprets the data supplied to these methods using the ISO-8859-1 (latin 1) encoding. The driver converts the data from ISO-8859-1 to the XML character set used by the database server and sends the converted XML data to the server.
Binary Data
Your application can use the following methods to insert or update XML data as binary data:
■
■
■
■
■
■
■
■
The driver does not apply any data conversions when sending XML data to the database server.