Sample application for the IMS Universal JDBC driver

The following sample Java™ application demonstrates the basic programming flow for a JDBC application using the IMS™ Universal JDBC driver.

The following example connects to an IMS database, retrieves a list of patient names using a SQL SELECT query, and modifies the patient information using a SQL UPDATE query.

package client;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.ibm.ims.jdbc.IMSDataSource;

public class JDBCSample {


    public static void main(String[] args) 
       throws SQLException {
        IMSDataSource ds = new IMSDataSource();
        ds.setDatabaseName("MYPSB");
        ds.setDatastoreName("IMS1");
        ds.setDatastoreServer("ec0123.my.host.com");
        ds.setPortNumber(5555);
        ds.setDriverType(IMSDataSource.DRIVER_TYPE_4);
        ds.setUser("myUserId");
        ds.setPassword("myPassword");

        Connection conn = null;

        try {
            conn = ds.getConnection();

            Statement st = conn.createStatement();

            // List all of the patient names in the 
            // SURG ward in the ALEXANDRIA hospital
            ResultSet rs = st.executeQuery("SELECT patname from " +
                    "pcb01.hospital, ward, patient " +
                    "where hospital.hospname = 'ALEXANDRIA' " +
                    "and ward.wardname = 'SURG'");
            while (rs.next()) {
                System.out.println(rs.getString("patname"));
            }

            // Update the name of the patient with patient 
            // number 0222 in ward 04 in the hospital
            // with code R1210010000A 
            int updatedRecords = st.executeUpdate("UPDATE PCB01.PATIENT " +
               "SET PATNAME='UPDATED NAME' WHERE PATNUM='0222' " + 
               "AND HOSPITAL_HOSPCODE='R121001000A' AND WARD_WARDNO='04'");
            System.out.println("Updated " + updatedRecords + " Record(s)");

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