EasyManuals Logo

IBM DB2 User Manual

IBM DB2
585 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #83 background imageLoading...
Page #83 background image
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

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the IBM DB2 and is the answer not in the manual?

IBM DB2 Specifications

General IconGeneral
DeveloperIBM
Initial Release1983
LicenseProprietary
Written inC, C++
CategoryDatabase Management System (RDBMS)
Operating SystemLinux, Windows
Programming LanguagesC, C++
Data ModelsRelational
EditionsCommunity, Standard, Advanced
Key Featureshigh availability, scalability