skip to main content
Using the driver : Returning and inserting/updating XML data : Inserting/updating XML data
  

Try DataDirect Drivers Now

Inserting/updating XML data

When inserting to or updating XMLType columns, the data to be inserted or updated must be the XMLType data type. Oracle provides the xmltype() function to construct an XMLType data object. The xmlData argument of the xmltype() function can be specified as a string literal or a parameter marker. If specified as a parameter marker, the parameter value can be set using the following methods:
*PreparedStatement.setString()
*PreparedStatement.setCharacterStream()
*PreparedStatement.setClob()
*PreparedStatement.setAsciiStream()
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.
The following code inserts data into an XMLType column using a statement with a string literal as the xmlData argument of the xmltype() function:

// Insert xml data as a literal
String sql = "INSERT INTO XMLTable VALUES (1, xmltype('" +
   "<emp><empNo>123</empNo><empName>Mark</empName></emp>'))";
Statement stmt = con.createStatement();
stmt.executeUpdate(sql);
The following code inserts data into an XMLType column using a prepared statement:

// Insert xml data as a String parameter
String xmlStr = "<emp><empNo>234</empNo><empName>Trish</empName></emp>";
String sql = "INSERT INTO XMLTable VALUES (?, xmltype(?))";
PreparedStatement prepStmt = con.prepareStatement(sql);
prepStmt.setInt(1, 2);
prepStmt.setString(2, xmlStr);
prepStmt.executeUpdate();