Xml Data Retrieval In Jdbc Applications - IBM DB2 Manual

Table of Contents

Advertisement

Example: 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());
}
}
Related concepts
"XML data in JDBC applications" on page 65
Related reference
"Data types that map to database data types in Java applications" on page 193
"DB2Xml interface" on page 368

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:
v Use the ResultSet.getSQLXML method to retrieve the data. Then use a
SQLXML.getXXX method to retrieve the data into a compatible output data type.
SQLXML.getXXX methods add XML declarations with encoding specifications to
the output data.
v Use a ResultSet.getXXX method other than ResultSet.getObject to retrieve the
data into a compatible data type.
Chapter 3. JDBC application programming
67

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents