Invocation of routines with XML parameters in Java applications

Java applications can call stored procedures at Db2 on Linux®, UNIX, and Windows systems or Db2 for z/OS® data sources that have XML parameters.

For native SQL procedures, XML parameters in the stored procedure definition have the XML type. For external stored procedures and user-defined functions on Db2 on Linux, UNIX, and Windows systems data sources, XML parameters in the routine definition have the XML AS CLOB type. When you call a stored procedure or user-defined function that has XML parameters, you need to use a compatible data type in the invoking statement.

To call a routine with XML input parameters from a JDBC program, use parameters of the java.sql.SQLXML or com.ibm.db2.jcc.DB2Xml type. To register XML output parameters, register the parameters as the java.sql.Types.SQLXML or com.ibm.db2.jcc.DB2Types.XML type. (The com.ibm.db2.jcc.DB2Xml and com.ibm.db2.jcc.DB2Types.XML types are deprecated.)

Example: JDBC program that calls a stored procedure that takes three XML parameters: an IN parameter, an OUT parameter, and an INOUT parameter. This example requires JDBC 4.0 or later.

java.sql.SQLXML in_xml = xmlvar; 
java.sql.SQLXML out_xml = null;
java.sql.SQLXML inout_xml = xmlvar;                               
                                  // Declare an input, output, and       
                                  // INOUT XML parameter
Connection con;
CallableStatement cstmt;
ResultSet rs;
…
cstmt = con.prepareCall("CALL SP_xml(?,?,?)");
                                  // Create a CallableStatement object
cstmt.setObject (1, in_xml);      // Set input parameter
cstmt.setObject (3, inout_xml);   // Set inout parameter
cstmt.registerOutParameter (2, java.sql.Types.SQLXML);
                                  // Register out and input parameters
cstmt.registerOutParameter (3, java.sql.Types.SQLXML);
cstmt.executeUpdate();            // Call the stored procedure
out_xml = cstmt.getSQLXML(2);     // Get the OUT parameter value
inout_xml = cstmt.getSQLXML(3);   // Get the INOUT parameter value
System.out.println("Parameter values from SP_xml call: ");
System.out.println("Output parameter value ");
MyUtilities.printString(out_xml.getString());
                          // Use the SQLXML.getString
                          // method to convert the out_xml
                          // value to a string for printing.
                          // Call a user-defined method called
                          // printString (not shown) to print
                          // the value.
System.out.println("INOUT parameter value ");
MyUtilities.printString(inout_xml.getString());
                          // Use the SQLXML.getString
                          // method to convert the inout_xml
                          // value to a string for printing.
                          // Call a user-defined method called
                          // printString (not shown) to print
                          // the value.

To call a routine with XML parameters from an SQLJ program, use parameters of the java.sql.SQLXML or com.ibm.db2.jcc.DB2Xml type.

Example: SQLJ program that calls a stored procedure that takes three XML parameters: an IN parameter, an OUT parameter, and an INOUT parameter. This example requires JDBC 4.0 or later.

java.sql.SQLXML in_xml = xmlvar;
java.sql.SQLXML out_xml = null;
java.sql.SQLXML inout_xml = xmlvar;
                                  // Declare an input, output, and 
                                  // INOUT XML parameter
…
#sql [myConnCtx] {CALL SP_xml(:IN in_xml,
                            :OUT out_xml,
                            :INOUT inout_xml)};
                                  // Call the stored procedure
System.out.println("Parameter values from SP_xml call: ");
System.out.println("Output parameter value ");
MyUtilities.printString(out_xml.getString());
                          // Use the SQLXML.getString 
                          // method toconvert the out_xml value 
                          // to a string for printing.
                          // Call a user-defined method called
                          // printString (not shown) to print
                          // the value.
System.out.println("INOUT parameter value ");
MyUtilities.printString(inout_xml.getString());
                          // Use the SQLXML.getString
                          // method to convert the inout_xml
                          // value to a string for printing.
                          // Call a user-defined method called
                          // printString (not shown) to print
                          // the value.