【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了BookManagerEJB2.java提供大家参考,希望对大家有所帮助!
import javax.ejb.*;
import javax.resource.cci.*;
import javax.resource.ResourceException;
import javax.naming.*;
import com.sun.connector.cciblackbox.*;
import java.util.*;
public class BookManagerEJB2 implements SessionBean {
private SessionContext sc;
private String user;
private String pass;
private ConnectionFactory cf;
// Session Bean Methods
public void ejbPassivate() {;}
public void ejbActivate() {;}
public void ejbCreate() {;}
public void ejbRemove() {;}
public void setSessionContext(SessionContext sc) {
this.sc=sc;
try {
Context ic = new InitialContext();
user = (String) ic.lookup("java:comp/env/user");
pass = (String) ic.lookup("java:comp/env/pass
word");
cf = (ConnectionFactory) ic.lookup("java:comp/env/CCIEIS");
}
catch (NamingException ne) {
System.err.println(ne.getMessage());
}
}
// The business method
public void listTitles(String name) {
try {
ConnectionSpec cs = new CciConnectionSpec(user, pass);
Connection c = cf.getConnection(cs);
Interaction i = c.createInteraction();
CciInteractionSpec iSpec = new CciInteractionSpec();
iSpec.setFunctionName("LISTTITLES");
iSpec.setSchema(user);
iSpec.setCatalog(null);
RecordFactory rf = cf.getRecordFactory();
IndexedRecord iRec = rf.createIndexedRecord("InputRecord");
// Add the name parameter to the record
iRec.add(name);
// Obtain a reference to a transaction context
LocalTransaction lTrans = c.getLocalTransaction();
// Start the transaction
lTrans.begin();
/**
* Execute the stored procedure,
* passing the InteractionSpec and the record
*/
try {
Record oRec = i.execute(iSpec, iRec);
// Get an iterator. Cast the Record to an IndexedRecord.
Iterator iterator = ((IndexedRecord)oRec).iterator();
// Iterate through the records and print names
while (iterator.hasNext()) {
String title = (String)iterator.next();
System.out.println(title);
}
}
catch (ResourceException e) {
// Rollback in the event of an error
lTrans.rollback();
}
c.close();
}
catch(ResourceException re) {
System.err.println(re.getMessage());
}
}
}