📄 enterprise1bean.java
字号:
package cmp2bean;
import javax.ejb.*;
import javax.naming.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;
public class Enterprise1Bean
implements SessionBean {
SessionContext sessionContext;
private ContactHome contactHome = null;
private Contact contact = null;
public void ejbCreate() throws CreateException {
/**@todo Complete this method*/
}
public void ejbRemove() {
/**@todo Complete this method*/
}
public void ejbActivate() {
/**@todo Complete this method*/
}
public void ejbPassivate() {
/**@todo Complete this method*/
}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
public void displayPerson(String last) {
try {
Context context = getInitialContext();
//look up jndi name
Object ref = context.lookup("Contact2");
//look up jndi name and cast to Home interface
contactHome = (ContactHome) PortableRemoteObject.narrow(ref, ContactHome.class);
System.out.println("find a record whose key is:" + last);
contact = contactHome.findByPrimaryKey(last);
System.out.println("getFirst(): " + contact.getFirst() +
" getLast(): " + contact.getLast() +
" getEmail(): " + contact.getEmail());
}
catch (Exception ex) {
System.out.println("Exception in displayPerson in PersonHandlerBean:" +
ex);
}
}
public void insertPerson(String first, String last, String email) {
try {
Context context = getInitialContext();
//look up jndi name
Object ref = context.lookup("Contact2");
//look up jndi name and cast to Home interface
contactHome = (ContactHome) PortableRemoteObject.narrow(ref, ContactHome.class);
System.out.println("insert a record whose key is:" + last);
contact = contactHome.create(last);
contact.setFirst(first);
contact.setEmail(email);
System.out.println("getFirst(): " + contact.getFirst() +
" getLast(): " + contact.getLast() +
" getEmail(): " + contact.getEmail());
}
catch (Exception ex) {
System.out.println("Exception in displayPerson in PersonHandlerBean:" +
ex);
}
}
private Context getInitialContext() throws Exception {
String url = "t3://localhost:7001";
String user = null;
String password = null;
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS,
password == null ? "" : password);
}
return new InitialContext(properties);
}
catch (Exception e) {
System.out.println("Unable to connect to WebLogic server at " + url);
System.out.println("Please make sure that the server is running.");
throw e;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -