⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mycarttestclient2.java

📁 1.DATABASE目录下存放的是网上购书系统的数据库脚本文件以及相应的jdatastore数据库文件。 2.bookstoreejb目录下存放的工程对应于网上购书系统的ejb部分。 3.shop
💻 JAVA
字号:
package bookstoreejb;import javax.naming.*;import java.util.Properties;import javax.rmi.PortableRemoteObject;public class mycartTestClient2 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 mycartHome mycartHomeObject = null;  private mycart mycartObject = null;  //Construct the EJB test client  public mycartTestClient2() {    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("mycart");      //look up jndi name and cast to Home interface      mycartHomeObject = (mycartHome) PortableRemoteObject.narrow(ref, mycartHome.class);      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded initializing local bean access through Local 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://zhaoqiang: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 mycart create(String _custName) {    long startTime = 0;    if (logging) {      log("Calling create(" + _custName + ")");      startTime = System.currentTimeMillis();    }    try {      mycartObject = mycartHomeObject.create(_custName);      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: create(" + _custName + ")");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: create(" + _custName + ")");      }      e.printStackTrace();    }    if (logging) {      log("Return value from create(" + _custName + "): " + mycartObject + ".");    }    return mycartObject;  }  //----------------------------------------------------------------------------  // Methods that use Remote interface methods to access data through the bean  //----------------------------------------------------------------------------  public void addBook(Book book) {    if (mycartObject == null) {      System.out.println("Error in addBook(): " + ERROR_NULL_REMOTE);      return ;    }    long startTime = 0;    if (logging) {      log("Calling addBook(" + book + ")");      startTime = System.currentTimeMillis();    }    try {      mycartObject.addBook(book);      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: addBook(" + book + ")");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: addBook(" + book + ")");      }      e.printStackTrace();    }  }  public void deleteBook(Book book) {    if (mycartObject == null) {      System.out.println("Error in deleteBook(): " + ERROR_NULL_REMOTE);      return ;    }    long startTime = 0;    if (logging) {      log("Calling deleteBook(" + book + ")");      startTime = System.currentTimeMillis();    }    try {      mycartObject.deleteBook(book);      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: deleteBook(" + book + ")");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: deleteBook(" + book + ")");      }      e.printStackTrace();    }  }  public float getTotalPrice() {    float returnValue = 0f;    if (mycartObject == null) {      System.out.println("Error in getTotalPrice(): " + ERROR_NULL_REMOTE);      return returnValue;    }    long startTime = 0;    if (logging) {      log("Calling getTotalPrice()");      startTime = System.currentTimeMillis();    }    try {      returnValue = mycartObject.getTotalPrice();      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: getTotalPrice()");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: getTotalPrice()");      }      e.printStackTrace();    }    if (logging) {      log("Return value from getTotalPrice(): " + returnValue + ".");    }    return returnValue;  }  public void executeRemoteCallsWithDefaultArguments() {    if (mycartObject == null) {      System.out.println("Error in executeRemoteCallsWithDefaultArguments(): " + ERROR_NULL_REMOTE);      return ;    }    addBook(null);    deleteBook(null);    getTotalPrice();  }  //----------------------------------------------------------------------------  // 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) {    mycartTestClient2 client = new mycartTestClient2();    Book book1 =new Book("b1","z",12);    client.create("z");    client.addBook(book1);    System.out.println(client.getTotalPrice());    client.deleteBook(book1);    System.out.println(client.getTotalPrice());// 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -