Storing XML data by using the IMS Universal JDBC driver

You can use the IMS Universal JDBC driver to store XML data into an IMS database through an SQL INSERT statement.

To store XML data in your IMS Universal JDBC driver application:

Procedure

  1. Specify the file path that contains the XML schema file (.xsd) that describes the input XML data structure by setting the http://www.ibm.com/ims/schema-resolver/file/path environment variable.
    The following example shows how to programmatically set the environment variable. In this example, the file path uxml/samples indicates a relative path to the XML schema file. You can also specify an absolute file path.
    System.setProperty("http://www.ibm.com/ims/schema-resolver/file/path", 
       "uxml/samples");
  2. Specify your XML data source.
    If you are reading in XML data from an external source, such as a file, you must create a java.io.Reader object to wrap the input XML data. The following example shows how to create an InputStreamReader object to wrap an external file named hospwashington.xml. The InputStreamReader object converts the bytes that are read from the input file from ASCII encoding to Unicode.
    String doc = "hospwashington.xml";   
    InputStream fileStream = getClass().getResourceAsStream(doc);
    if (fileStream == null) {
        throw new FileNotFoundException("Insert Document: '" + doc + "' was 
        not found in classpath");
    }
    InputStreamReader fileReader = new InputStreamReader(fileStream, "ASCII");
  3. Insert the XML data.
    1. Create a java.sql.preparedStatement object representing the SQL INSERT call.
      In the SQL INSERT statement, you must specify the name of the XML column to store the XML data. The column name must match the name that is defined in the Java™ metadata class.
      The following example shows how to create a preparedStatement object to insert data into the hospxml column in the HOSPITAL segment, using the java.sql.Connection instance conn.
      String s = "INSERT INTO pcb01.HOSPITAL (hospxml) VALUES (?)"
      PreparedStatement ps = conn.prepareStatement(s);
      
    2. Set the value of the XML data to insert into the preparedStatement object.
      The following table describes the methods and corresponding input data types that you can use to insert data in XML columns.
      Table 1. Methods and data types for updating XML columns
      Method Input data type
      PreparedStatement.setCharacterStream Reader
      PreparedStatement.setClob Clob
      In decomposed storage mode, XML data is stored with EBCDIC encoding. In intact storage mode, the default encoding is Unicode.

The following code sample shows how to insert XML data into the Hospital database.

package uxml.samples;

import java.io.*;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import com.ibm.ims.jdbc.IMSDataSource;

public class StoreXMLSamples{

    public static void main(String argv[]) throws SQLException,IOException {
        IMSDataSource ds = new IMSDataSource();
        ds.setDatabaseName("class://uxml.samples.BMP255NewSyntaxDatabaseView");
        ds.setDatastoreName("IMS1");
        ds.setDatastoreServer("yourhost.yourdomain.com");
        ds.setPortNumber(5555);
        ds.setDriverType(IMSDataSource.DRIVER_TYPE_4);
        ds.setUser("myUserID");
        ds.setPassword("myPass");

        // Specify file path of XML schema
        System.setProperty("http://www.ibm.com/ims/schema-resolver/file/path", 
           "uxml/samples");

        Connection conn = null;

        try {
          conn = ds.getConnection();
          Statement st = conn.createStatement();			
	        String doc = "hospwashington.xml";   
	        StoreXMLSamples storeSample = new StoreXMLSamples();
	        InputStream fileStream = 
             storeSample.getClass().getResourceAsStream(doc);
	        if (fileStream == null) {
	            throw new FileNotFoundException("Insert Document: '" + 
                 doc + "' was not found in classpath");
	        }
          
          // Convert XML document from ASCII to Unicode
 	        InputStreamReader fileReader = 
             new InputStreamReader(fileStream, "ASCII");

	        PreparedStatement ps = 
             conn.prepareStatement("INSERT INTO pcb01.HOSPITAL" +
               " (hospxml) VALUES (?)");

            ps.setCharacterStream(1, fileReader, -1);
            int rows = ps.executeUpdate();	        	
            System.out.println("Inserted");
            conn.commit();
           conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
            if (!conn.isClosed()) {
                conn.rollback();
                conn.close();
            }
        }
    }   
}