XML column updates in JDBC applications
When you update or insert data into XML columns of a database table, the input data in your JDBC applications must be in the serialized string format.
The following table lists the methods and corresponding input data types that you can use to put data in XML columns.
| Method | Input data type |
|---|---|
| PreparedStatement.setAsciiStream | InputStream |
| PreparedStatement.setBinaryStream | InputStream |
| PreparedStatement.setBlob | Blob |
| PreparedStatement.setBytes | byte[] |
| PreparedStatement.setCharacterStream | Reader |
| PreparedStatement.setClob | Clob |
| PreparedStatement.setObject | byte[], Blob, Clob, SQLXML, InputStream, Reader, String |
| PreparedStatement.setString | String |
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.
External encoding for Java™ applications is always Unicode encoding.
Externally encoded data can have internal encoding. That is, the data might be sent to the data source as character data, but the data contains encoding information. The data source handles incompatibilities between internal and external encoding by generating an error if the external and internal encoding are incompatible.
Data in XML columns is stored in the XML column CCSID. The database source handles conversion of the data from its internal or external encoding to the XML column CCSID.
The following example demonstrates inserting data from an SQLXML object into an XML column. The data is String data, so the database source treats the data as externally encoded.
public void insertSQLXML()
{
Connection con = DriverManager.getConnection(url);
SQLXML info = con.createSQLXML;
// Create an SQLXML object
PreparedStatement insertStmt = null;
String infoData =
"<customerinfo xmlns=""http://posample.org"" " +
"Cid=""1000"" xmlns=""http://posample.org"">...</customerinfo>";
cid.setString(cidData);
// Populate the SQLXML object
int cid = 1000;
try {
sqls = "INSERT INTO CUSTOMER (CID, INFO) VALUES (?, ?)";
insertStmt = con.prepareStatement(sqls);
insertStmt.setInt(1, cid);
insertStmt.setSQLXML(2, info);
// Assign the SQLXML object value
// to an input parameter
if (insertStmt.executeUpdate() != 1) {
System.out.println("insertSQLXML: No record inserted.");
}
}
catch (IOException ioe) {
ioe.printStackTrace();
}
catch (SQLException sqle) {
System.out.println("insertSQLXML: SQL Exception: " +
sqle.getMessage());
System.out.println("insertSQLXML: SQL State: " +
sqle.getSQLState());
System.out.println("insertSQLXML: SQL Error Code: " +
sqle.getErrorCode());
}
}
The following example demonstrates inserting data from a file into an XML column. The data is inserted as binary data, so the database server honors the internal encoding.
public void insertBinStream()
{
PreparedStatement insertStmt = null;
String sqls = null;
int cid = 0;
ResultSet rs=null;
Statement stmt=null;
try {
sqls = "INSERT INTO CUSTOMER (CID, INFO) VALUES (?, ?)";
insertStmt = conn.prepareStatement(sqls);
insertStmt.setInt(1, cid);
File file = new File(fn);
insertStmt.setBinaryStream(2,
new FileInputStream(file), (int)file.length());
if (insertStmt.executeUpdate() != 1) {
System.out.println("insertBinStream: No record inserted.");
}
}
catch (IOException ioe) {
ioe.printStackTrace();
}
catch (SQLException sqle) {
System.out.println("insertBinStream: SQL Exception: " +
sqle.getMessage());
System.out.println("insertBinStream: SQL State: " +
sqle.getSQLState());
System.out.println("insertBinStream: SQL Error Code: " +
sqle.getErrorCode());
}
}
