Accessing IMS data with the IMS Universal JCA/JDBC driver
Use the IMS Universal JCA/JDBC driver if you require full use of the IMS Universal JDBC driver within a Java™ EE runtime environment.
Before your Java EE application component
can retrieve, insert, update, or delete data from an IMS
database, you need to obtain a java.sql.Connection instance for the physical
connection to the database.
Example EJB application using the IMS Universal JCA/JDBC driver
The following code sample shows a bean-managed EJB application that 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.SQLException;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import javax.transaction.UserTransaction;
import com.ibm.ims.db.cci.SQLInteractionSpec;
/**
* Bean implementation class for Enterprise Bean: StatefulBeanManaged
*/
public class JDBCBeanManagedSampleSQLBean {
private javax.ejb.SessionContext mySessionCtx;
public void execute() throws Exception {
InitialContext ic = new InitialContext();
DataSource ds =
(DataSource) ic.lookup("java:comp/env/MyMCF");
Connection conn = null;
UserTransaction ut = null;
try {
ut = this.mySessionCtx.getUserTransaction();
ut.begin();
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)");
rs.close();
ut.commit();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
ut.rollback();
conn.close();
}
}
/**
* getSessionContext
*/
public javax.ejb.SessionContext getSessionContext() {
return mySessionCtx;
}
/**
* setSessionContext
*/
public void setSessionContext(javax.ejb.SessionContext ctx) {
mySessionCtx = ctx;
}
/**
* ejbCreate
*/
public void ejbCreate() throws javax.ejb.CreateException {
}
/**
* ejbActivate
*/
public void ejbActivate() {
}
/**
* ejbPassivate
*/
public void ejbPassivate() {
}
/**
* ejbRemove
*/
public void ejbRemove() {
}
}