XML column updates in SQLJ applications
In an SQLJ application, you can update or insert data into XML columns of a table at a Db2 data server using XML textual data. You can update or insert data into XML columns of a table using binary XML data (data that is in the Extensible Dynamic Binary XML DB2® Client/Server Binary XML Format), if the data server supports binary XML data.
- java.sql.SQLXML (requires an SDK for Java Version 6 or later, and the IBM® Data Server Driver for JDBC and SQLJ version 4.0 or later)
- com.ibm.db2.jcc.DB2Xml (deprecated)
- String
- byte
- Blob
- Clob
- sqlj.runtime.AsciiStream
- sqlj.runtime.BinaryStream
- sqlj.runtime.CharacterStream
The encoding of XML data can be derived from the data itself, which is known as internally encoded data, or from external sources, which is known as externally encoded data. XML data that is sent to the database server as binary data is treated as internally encoded data. XML data that is sent to the data source as character data is treated as externally encoded data. The external encoding is the default encoding for the JVM.
External encoding for Java applications is always Unicode encoding.
- If the data source is Db2 on Linux®, UNIX, and Windows systems, the data source generates an error if the external and internal encoding are incompatible, unless the external and internal encoding are Unicode. If the external and internal encoding are Unicode, the data source ignores the internal encoding.
- If the data source is Db2 for z/OS®, the data source ignores internal encoding.
Character data in XML columns is stored in UTF-8 encoding.
#sql [ctx] {INSERT INTO CUSTACC VALUES (1, :xmlString)};
byte[] xmlBytes = xmlString.getBytes("CP500");
#sql[ctx] {INSERT INTO CUSTACC VALUES (4, :xmlBytes)};
A byte string is considered to be internally encoded data.
The data is converted from its internal encoding scheme to UTF-8,
if necessary, and stored in its hierarchical format on the data source.byte[] b = xmlString.getBytes("US-ASCII");
java.io.ByteArrayInputStream xmlAsciiInputStream =
new java.io.ByteArrayInputStream(b);
sqlj.runtime.AsciiStream sqljXmlAsciiStream =
new sqlj.runtime.AsciiStream(xmlAsciiInputStream, b.length);
#sql[ctx] {INSERT INTO CUSTACC VALUES (4, :sqljXmlAsciiStream)};
sqljXmlAsciiStream is a stream type, so its internal encoding
is used. The data is converted from its internal encoding to UTF-8
encoding and stored in its hierarchical form on the data source.java.io.StringReader xmlReader =
new java.io.StringReader(xmlString);
sqlj.runtime.CharacterStream sqljXmlCharacterStream =
new sqlj.runtime.CharacterStream(xmlReader, xmlString.length());
#sql [ctx] {INSERT INTO CUSTACC VALUES (4, :sqljXmlCharacterStream)};
sqljXmlCharacterStream is a character type, so its external
encoding is used, whether or not it has an internal encoding specification. java.sql.ResultSet rs = s.executeQuery ("SELECT * FROM CUSTACC");
rs.next();
java.sql.SQLXML xmlObject = (java.sql.SQLXML)rs.getObject(2);
#sql [ctx] {INSERT INTO CUSTACC VALUES (6, :xmlObject)};
After you retrieve the data it is still in UTF-8 encoding,
so when you insert the data into another XML column, no conversion
occurs.java.sql.ResultSet rs = s.executeQuery ("SELECT * FROM CUSTACC");
rs.next();
com.ibm.db2.jcc.DB2Xml xmlObject = (com.ibm.db2.jcc.DB2Xml)rs.getObject(2);
#sql [ctx] {INSERT INTO CUSTACC VALUES (6, :xmlObject)};
After you retrieve the data it is still in UTF-8 encoding,
so when you insert the data into another XML column, no conversion
occurs.