studenttestclient1.java

来自「100多M的J2EE培训内容」· Java 代码 · 共 379 行

JAVA
379
字号
package dempcmp;import com.cwj.entitybeandempcmp.*;import javax.naming.*;import java.util.Properties;import javax.rmi.PortableRemoteObject;import java.util.Collection;public class StudentTestClient1 extends Object {  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 static final int MAX_OUTPUT_LINE_LENGTH = 100;  private boolean logging = true;  private StudentRemoteHome studentRemoteHome = null;  private StudentRemote studentRemote = null;  //Construct the EJB test client  public StudentTestClient1() {    initialize();  }  public void initialize() {    long startTime = 0;    if (logging) {      log("Initializing bean access.");      startTime = System.currentTimeMillis();    }    try {      //get naming context      Context context = getInitialContext();      //look up jndi name      Object ref = context.lookup("StudentRemote");      //look up jndi name and cast to Home interface      studentRemoteHome = (StudentRemoteHome) PortableRemoteObject.narrow(ref, StudentRemoteHome.class);      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded initializing bean access through Home interface.");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed initializing bean access.");      }      e.printStackTrace();    }  }  private Context getInitialContext() throws Exception {    String url = "t3://wg_office: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) {      log("Unable to connect to WebLogic server at " + url);      log("Please make sure that the server is running.");      throw e;    }  }  //----------------------------------------------------------------------------  // Methods that use Home interface methods to generate a Remote interface reference  //----------------------------------------------------------------------------  public StudentRemote create(String studentID) {    long startTime = 0;    if (logging) {      log("Calling create(" + studentID + ")");      startTime = System.currentTimeMillis();    }    try {      studentRemote = studentRemoteHome.create(studentID);      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: create(" + studentID + ")");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: create(" + studentID + ")");      }      e.printStackTrace();    }    if (logging) {      log("Return value from create(" + studentID + "): " + studentRemote + ".");    }    return studentRemote;  }  public StudentRemote create(String studentID, String firstname, String lastname) {    long startTime = 0;    if (logging) {      log("Calling create(" + studentID + ", " + firstname + ", " + lastname + ")");      startTime = System.currentTimeMillis();    }    try {      studentRemote = studentRemoteHome.create(studentID, firstname, lastname);      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: create(" + studentID + ", " + firstname + ", " + lastname + ")");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: create(" + studentID + ", " + firstname + ", " + lastname + ")");      }      e.printStackTrace();    }    if (logging) {      log("Return value from create(" + studentID + ", " + firstname + ", " + lastname + "): " + studentRemote + ".");    }    return studentRemote;  }  public Collection findLastName(String lastname) {    Collection returnValue = null;    long startTime = 0;    if (logging) {      log("Calling findLastName(" + lastname + ")");      startTime = System.currentTimeMillis();    }    try {      returnValue = studentRemoteHome.findLastName(lastname);      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: findLastName(" + lastname + ")");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: findLastName(" + lastname + ")");      }      e.printStackTrace();    }    if (logging) {      log("Return value from findLastName(" + lastname + "): " + returnValue + ".");    }    return returnValue;  }  public StudentRemote findByPrimaryKey(String studentID) {    long startTime = 0;    if (logging) {      log("Calling findByPrimaryKey(" + studentID + ")");      startTime = System.currentTimeMillis();    }    try {      studentRemote = studentRemoteHome.findByPrimaryKey(studentID);      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: findByPrimaryKey(" + studentID + ")");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: findByPrimaryKey(" + studentID + ")");      }      e.printStackTrace();    }    if (logging) {      log("Return value from findByPrimaryKey(" + studentID + "): " + studentRemote + ".");    }    return studentRemote;  }  //----------------------------------------------------------------------------  // Methods that use Remote interface methods to access data through the bean  //----------------------------------------------------------------------------  public String getStudentID() {    String returnValue = "";    if (studentRemote == null) {      System.out.println("Error in getStudentID(): " + ERROR_NULL_REMOTE);      return returnValue;    }    long startTime = 0;    if (logging) {      log("Calling getStudentID()");      startTime = System.currentTimeMillis();    }    try {      returnValue = studentRemote.getStudentID();      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: getStudentID()");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: getStudentID()");      }      e.printStackTrace();    }    if (logging) {      log("Return value from getStudentID(): " + returnValue + ".");    }    return returnValue;  }  public void setFirstname(String firstname) {    if (studentRemote == null) {      System.out.println("Error in setFirstname(): " + ERROR_NULL_REMOTE);      return ;    }    long startTime = 0;    if (logging) {      log("Calling setFirstname(" + firstname + ")");      startTime = System.currentTimeMillis();    }    try {      studentRemote.setFirstname(firstname);      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: setFirstname(" + firstname + ")");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: setFirstname(" + firstname + ")");      }      e.printStackTrace();    }  }  public String getFirstname() {    String returnValue = "";    if (studentRemote == null) {      System.out.println("Error in getFirstname(): " + ERROR_NULL_REMOTE);      return returnValue;    }    long startTime = 0;    if (logging) {      log("Calling getFirstname()");      startTime = System.currentTimeMillis();    }    try {      returnValue = studentRemote.getFirstname();      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: getFirstname()");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: getFirstname()");      }      e.printStackTrace();    }    if (logging) {      log("Return value from getFirstname(): " + returnValue + ".");    }    return returnValue;  }  public void setLastname(String lastname) {    if (studentRemote == null) {      System.out.println("Error in setLastname(): " + ERROR_NULL_REMOTE);      return ;    }    long startTime = 0;    if (logging) {      log("Calling setLastname(" + lastname + ")");      startTime = System.currentTimeMillis();    }    try {      studentRemote.setLastname(lastname);      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: setLastname(" + lastname + ")");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: setLastname(" + lastname + ")");      }      e.printStackTrace();    }  }  public String getLastname() {    String returnValue = "";    if (studentRemote == null) {      System.out.println("Error in getLastname(): " + ERROR_NULL_REMOTE);      return returnValue;    }    long startTime = 0;    if (logging) {      log("Calling getLastname()");      startTime = System.currentTimeMillis();    }    try {      returnValue = studentRemote.getLastname();      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: getLastname()");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: getLastname()");      }      e.printStackTrace();    }    if (logging) {      log("Return value from getLastname(): " + returnValue + ".");    }    return returnValue;  }  //----------------------------------------------------------------------------  // Utility Methods  //----------------------------------------------------------------------------  private void log(String message) {    if (message == null) {      System.out.println("-- null");      return ;    }    if (message.length() > MAX_OUTPUT_LINE_LENGTH) {      System.out.println("-- " + message.substring(0, MAX_OUTPUT_LINE_LENGTH) + " ...");    }    else {      System.out.println("-- " + message);    }  }  //Main method  public static void main(String[] args) {    StudentTestClient1 client = new StudentTestClient1();    // Use the client object to call one of the Home interface wrappers    // above, to create a Remote interface reference to the bean.    // If the return value is of the Remote interface type, you can use it    // to access the remote interface methods.  You can also just use the    // client object to call the Remote interface wrappers.  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?