【Java开源代码栏目提醒】:网学会员Java开源代码为您提供ContactTestClient1.java参考,解决您在ContactTestClient1.java学习中工作中的难题,参考学习。
package bmpbean;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.lang.String;
import java.util.Properties;
import java.util.Hashtable;
public class ContactTestClient1 {
private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
private ContactRemote contactRemote = null;
private ContactRemoteHome contactRemoteHome = null;
// Construct the EJB test client
public ContactTestClient1() {
initialize();
}
public void initialize() {
try {
// get naming context
Context context = getInitialContext();
// look up jndi name
Object ref = context.lookup("Contact");
// look up jndi name and cast to Home interface
contactRemoteHome = (ContactRemoteHome) PortableRemoteObject
.narrow(ref, ContactRemoteHome.class);
System.out.println("find a record whose key is:" + "Zhang");
contactRemote = contactRemoteHome.findByPrimaryKey("Zhang");
System.out.println("getFirst(): " + contactRemote.getFirst()
+ " getLast():" + contactRemote.getLast() + " getEmail():"
+ contactRemote.getEmail());
} catch (Exception e) {
e.printStackTrace();
}
}
protected Context getInitialContext() throws Exception {
Hashtable props = new Hashtable();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(Context.URL_PKG_PREFIXES,
"org.jboss.naming:org.jnp.interfaces");
props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
Context ctx = new InitialContext(props);
return ctx;
}
// ----------------------------------------------------------------------------
// Methods that use Home interface methods to generate a Remote interface
// reference
// ----------------------------------------------------------------------------
public ContactRemote create(String last) {
try {
contactRemote = contactRemoteHome.create(last);
} catch (Exception e) {
e.printStackTrace();
}
return contactRemote;
}
public ContactRemote findByPrimaryKey(String last) {
try {
contactRemote = contactRemoteHome.findByPrimaryKey(last);
} catch (Exception e) {
e.printStackTrace();
}
return contactRemote;
}
// ----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
// ----------------------------------------------------------------------------
public void setEmail(String email) {
if (contactRemote == null) {
System.out.println("Error in setEmail(): " + ERROR_NULL_REMOTE);
return;
}
try {
contactRemote.setEmail(email);
} catch (Exception e) {
e.printStackTrace();
}
}
public String getEmail() {
String returnValue = "";
if (contactRemote == null) {
System.out.println("Error in getEmail(): " + ERROR_NULL_REMOTE);
return returnValue;
}
try {
returnValue = contactRemote.getEmail();
} catch (Exception e) {
e.printStackTrace();
}
return returnValue;
}
public String getLast() {
String returnValue = "";
if (contactRemote == null) {
System.out.println("Error in getLast(): " + ERROR_NULL_REMOTE);
return returnValue;
}
try {
returnValue = contactRemote.getLast();
} catch (Exception e) {
e.printStackTrace();
}
return returnValue;
}
public void setFirst(String first) {
if (contactRemote == null) {
System.out.println("Error in setFirst(): " + ERROR_NULL_REMOTE);
return;
}
try {
contactRemote.setFirst(first);
} catch (Exception e) {
e.printStackTrace();
}
}
public String getFirst() {
String returnValue = "";
if (contactRemote == null) {
System.out.println("Error in getFirst(): " + ERROR_NULL_REMOTE);
return returnValue;
}
try {
returnValue = contactRemote.getFirst();
} catch (Exception e) {
e.printStackTrace();
}
return returnValue;
}
// ----------------------------------------------------------------------------
// Utility Methods
// ----------------------------------------------------------------------------
public ContactRemoteHome getHome() {
return contactRemoteHome;
}
// Main method
public static void main(String[] args) {
ContactTestClient1 client = new ContactTestClient1();
// Use the getHome() method of the client object to call Home interface
// methods that will return a Remote interface reference. Then
// use that Remote interface reference to access the EJB.
}
}
上一篇:
ContactsDemo.java
下一篇:
致享和威驰哪个好点?冲着安全和空间 推荐致享