Java 集成开发实例精解

源代码在线查看: contacttestclient1.java~2~

软件大小: 3787 K
上传用户: shanon
关键词: Java 集成 开发实例
下载地址: 免注册下载 普通下载 VIP

相关代码

				package cmp1bean;								import javax.naming.*;				import java.util.Properties;				import javax.rmi.PortableRemoteObject;								public class ContactTestClient1 extends Object {				  private ContactHome contactHome = null;				  private Contact contact = 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				      contactHome = (ContactHome) PortableRemoteObject.narrow(ref, ContactHome.class);				      System.out.println("find a record whose key is:"+"Zhang");				                          contact=contactHome.findByPrimaryKey("Zhang");				                  System.out.println("getFirst(): "+contact.getFirst()+				                                             " getLast(): "+contact.getLast()+				                                             " getEmail(): "+contact.getEmail());								    }				    catch(Exception e) {				      e.printStackTrace();				    }				  }								  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;				    }				  }								  //----------------------------------------------------------------------------				  // Utility Methods				  //----------------------------------------------------------------------------								  public ContactHome getHome() {				    return contactHome;				  }				  //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.				  }				}			

相关资源