【Java开源代码栏目提醒】:网学会员为广大网友收集整理了,BookManagerEJB.java,希望对大家有所帮助!
import javax.ejb.*;
import javax.resource.cci.*;
import javax.resource.ResourceException;
import javax.naming.*;
import com.sun.connector.cciblackbox.*;
public class BookManagerEJB 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 {
// Establish a JNDI intial context
Context ic = new InitialContext();
// Lookup the username and password
user = (String) ic.lookup("java:comp/env/user");
pass = (String) ic.lookup("java:comp/env/password");
// Lookup the connection factory
cf = (ConnectionFactory) ic.lookup("java:comp/env/CCIEIS");
}
catch (NamingException ne) {
System.err.println(ne.getMessage());
}
}
// The business method
public void insertBook(String name, double price) {
try {
// Create a ConnectionSpec object that holds username and password
ConnectionSpec cs = new CciConnectionSpec(user, pass);
// Get a connection to the EIS from the ConnectionFactory
Connection c = cf.getConnection(cs);
// Create an interaction, to invoke stored procedures
Interaction i = c.createInteraction();
/**
* Create an InteractionSpec,
* so as to pass properties to the interaction object
*/
CciInteractionSpec iSpec = new CciInteractionSpec();
// Set the fields for this instance
iSpec.setFunctionName("INSERTBOOK");
iSpec.setSchema(user);
iSpec.setCatalog(null);
// Create a new record factory from the connection factory
RecordFactory rf = cf.getRecordFactory();
// Cloudscape only supports indexed records, so create one of these
IndexedRecord iRec = rf.createIndexedRecord("InputRecord");
// Add the name and price parameters or the record
iRec.add(name);
iRec.add(new Double(price));
// Execute the stored procedure
i.execute(iSpec, iRec);
// Close the connection
c.close();
}
catch(ResourceException re) {
System.err.println(re.getMessage());
}
}
}
上一篇:
BookManagerClient2.java
下一篇:
法律专业开题报告范文