Retrieving XML data by using the IMS Universal JDBC driver

You can use the IMS Universal JDBC driver to retrieve XML data from an IMS database as a character large object (CLOB) through an SQL SELECT statement.

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

Procedure

  1. Specify the file path that contains the XML schema file (.xsd) describing 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 and execute an SQL SELECT statement to retrieve the XML data.
    The database table in your SQL SELECT statement must include the XML column for retrieving the XML data. If you specify the column name explicitly in the SQL SELECT statement, the column name must match the name defined in the Java™ metadata class.
    The following example shows how to obtain a java.sql.resultSet object from an SQL SELECT call to retrieve the hospxml column in the HOSPITAL segment. In the example, st is a java.sql.Statement instance.
    ResultSet rs = st.executeQuery("SELECT hospxml FROM PCB01.HOSPITAL");
  3. Read the XML data from the resultSet object after the retrieve call is made.
    The XML data is stored in a java.sql.Clob object in the resultSet.

The following code sample shows how to retrieve XML data from 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 com.ibm.ims.jdbc.IMSDataSource;

public class RetrieveXMLSamples{

    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();

			     ResultSet rs = st.executeQuery("SELECT hospxml FROM PCB01.HOSPITAL");

			     StringWriter sw = new StringWriter();
           
            while (rs.next()) {
        
                Clob clob = rs.getClob(1);
                Reader reader = clob.getCharacterStream();
                char[] buffer = new char[1000];
                int read = reader.read(buffer);
                while (read != -1) {
                	sw.write(buffer,0,read);
                    read = reader.read(buffer);
                }
            }
            
           String result = sw.toString();
          	System.out.println(result);
           System.out.println();

           conn.commit();
           conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
            if (!conn.isClosed()) {
                conn.rollback();
                conn.close();
            }
        }
    }   
}