import java.math.BigDecimal; import java.util.*; import javax.ejb.*; import javax.resource.cci.*; import javax.resource.ResourceException; import javax.naming.*; import com.sun.connector.cciblackbox.*; public class AcctManageEJB implements SessionBean { private SessionContext sc; private String user; private String password; private ConnectionFactory cf; public void ejbCreate() throws CreateException { } public void setSessionContext(SessionContext sc) { try { this.sc = sc; Context ic = new InitialContext(); user = (String) ic.lookup("java:comp/env/user"); password = (String) ic.lookup("java:comp/env/password"); cf = (ConnectionFactory) ic.lookup("java:comp/env/CCIEIS"); } catch (NamingException ex) { ex.printStackTrace(); } } public int getAcctAmt() { int amount = -1; try { Connection con = getCCIConnection(); Interaction ix = con.createInteraction(); CciInteractionSpec iSpec = new CciInteractionSpec(); iSpec.setSchema(user); iSpec.setCatalog(null); iSpec.setFunctionName("GETAMT"); RecordFactory rf = cf.getRecordFactory(); IndexedRecord iRec = rf.createIndexedRecord("InputRecord"); Record oRec = ix.execute(iSpec, iRec); Iterator iterator = ((IndexedRecord)oRec).iterator(); while(iterator.hasNext()) { Object obj = iterator.next(); if(obj instanceof Integer) { amount = ((Integer)obj).intValue(); } else if(obj instanceof BigDecimal) { amount = ((BigDecimal)obj).intValue(); } } closeCCIConnection(con); }catch(ResourceException ex) { ex.printStackTrace(); } return amount; } public void insertAcct(String acctName, int amt) { try { Connection con = getCCIConnection(); Interaction ix = con.createInteraction(); CciInteractionSpec iSpec = new CciInteractionSpec(); iSpec.setFunctionName("ADDACCT"); iSpec.setSchema(user); iSpec.setCatalog(null); RecordFactory rf = cf.getRecordFactory(); IndexedRecord iRec = rf.createIndexedRecord("InputRecord"); boolean flag = iRec.add(acctName); flag = iRec.add(new Integer(amt)); ix.execute(iSpec, iRec); closeCCIConnection(con); }catch(ResourceException ex) { ex.printStackTrace(); } } private Connection getCCIConnection() { Connection con = null; try { ConnectionSpec spec = new CciConnectionSpec(user, password); con = cf.getConnection(spec); } catch (ResourceException ex) { ex.printStackTrace(); } return con; } private void closeCCIConnection(Connection con) { try { con.close(); } catch (ResourceException ex) { ex.printStackTrace(); } } public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} }