Accessing IMS data with the SQLInteractionSpec class
Use the SQLInteractionSpec class to retrieve, insert, update, and delete data from an IMS database using SQL queries with the IMS Universal Database resource adapter. The IMS Universal Database resource adapter supports the same SQL statement syntax and usage as the IMS Universal JDBC driver and has the same restrictions.
Before your application component can retrieve, insert, update, or delete data
from an IMS database, you need to obtain a
javax.resource.cci.Connection instance for the physical connection to the
database.
Procedure
Example code for IMS data operations using the SQLInteractionSpec class
The following code example shows how to use the SQLInteractionSpec class to retrieve patient names from PATIENT records.
package client;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.resource.ResourceException;
import javax.resource.cci.Connection;
import javax.resource.cci.ConnectionFactory;
import javax.resource.cci.Interaction;
import javax.resource.cci.ResultSet;
import javax.transaction.UserTransaction;
import com.ibm.ims.db.cci.SQLInteractionSpec;
/**
* Bean implementation class for Enterprise Bean: StatefulBeanManaged
*/
public class BeanManagedSampleSQLBean implements javax.ejb.SessionBean {
private javax.ejb.SessionContext mySessionCtx;
public void execute() throws Exception {
InitialContext ic = new InitialContext();
ConnectionFactory cf =
(ConnectionFactory) ic.lookup("java:comp/env/MyMCF");
Connection conn = null;
UserTransaction ut = null;
try {
ut = this.mySessionCtx.getUserTransaction();
ut.begin();
conn = cf.getConnection();
Interaction ix = conn.createInteraction();
SQLInteractionSpec iSpec = new SQLInteractionSpec();
// This query will return the WARDNAME, PATCOUNT, DOCCOUNT,
// and NURCOUNT fields for the WARD with WARDNO = 51
iSpec.setSQL("SELECT WARDNAME, PATCOUNT, DOCCOUNT, " +
"NURCOUNT FROM PCB09.WARD WHERE WARDNO='51'");
ResultSet rs = (ResultSet) ix.execute(iSpec, null);
while (rs.next()) {
System.out.println(rs.getString("WARDNAME"));
System.out.println(rs.getString("PATCOUNT"));
System.out.println(rs.getString("DOCCOUNT"));
System.out.println(rs.getString("NURCOUNT"));
}
rs.close();
ix.close();
ut.commit();
conn.close();
} catch (ResourceException e) {
ut.rollback();
conn.close();
} catch (SQLException e) {
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() {
}
}