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

📄 testclient.java~46~

📁 一个四层结构的j2ee项目实例。对于初学者相当合适。欢迎下载使用。
💻 JAVA~46~
📖 第 1 页 / 共 2 页
字号:
package sqlaccounts;import java.rmi.RemoteException;import java.util.Collection;import java.util.Hashtable;import java.util.Properties;import java.util.Vector;import java.util.Iterator;import javax.ejb.CreateException;import javax.ejb.DuplicateKeyException;import javax.ejb.FinderException;import javax.ejb.ObjectNotFoundException;import javax.ejb.RemoveException;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.rmi.PortableRemoteObject;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: </p> * @author unascribed * @version 1.0 */public class TestClient {  static final private String ERROR_NULL_REMOTE = "Remote interface reference is null.  It must be created by calling one of the Home interface methods first.";  static final private int MAX_OUTPUT_LINE_LENGTH = 100;  private boolean logging = true;  private EjbAccountsHome ejbAccountsHome = null;  private EjbAccounts ejbAccounts = null;  //Construct the EJB test client  public TestClient() {    long startTime = 0;    if (logging) {      log("Initializing bean access.");      startTime = System.currentTimeMillis();    }    try    {  /*    AccountsSessions x=new AccountsSessions();        Vector  allEmployee=x.findByType("zj");        int m=allEmployee.size();        System.out.println(m);        for(int ww=0 ; ww< m; ww++)        {            AccountsSessions anOrder = (AccountsSessions) allEmployee.elementAt(ww);            System.out.println(anOrder.getType());        }*/        Context ctx = getInitialContext();        //调用sessionEjb        Object ref = ctx.lookup("Login");        LoginHome     loginHome = (LoginHome) PortableRemoteObject.narrow(ref, LoginHome.class);        Login x=loginHome.create();        Vector  allEmployee=x.findByType("zj");        int m=allEmployee.size();        for(int ww=0 ; ww< m; ww++)        {            // String anOrder[] = (String) allEmployee.elementAt(ww);            // System.out.println(anOrder[0]);            Collection anOrder = (Collection) allEmployee.elementAt(ww);            Iterator iterator12= anOrder.iterator();            int n=1;            while (iterator12.hasNext()) {                System.out.println("个数 "+ n +": "+ iterator12.next());                n++;            }        }Iterator it = allEmployee.iterator();System.out.println(x);while (it.hasNext()){ Login emp = (Login)it.next();//System.out.println(emp.getType());    System.out.println(it.toString()+it.next());}       //get naming context      /*  Context ctx = getInitialContext();      //look up jndi name        Object ref = ctx.lookup("EjbAccountsRemote");      //cast to Home interface      ejbAccountsHome = (EjbAccountsHome) PortableRemoteObject.narrow(ref, EjbAccountsHome.class);      for(int i=0 ;i<30;i++)     {        findOrCreateAccount("11"+i*100,"张 瑾",i*100);     }     for(int i=0 ;i<30;i++)    {        ejbAccountsHome.create("11"+i*100,"zhangjin",100.0);     }   Collection col = findByType("张 瑾");   if(col.isEmpty()) {     log("No accounts were found!");   }   int j=10;   Iterator it = col.iterator();   while (it.hasNext()) {//     EjbAccounts bigAccount =         ejbAccounts=(EjbAccounts) PortableRemoteObject.narrow(it.next(), EjbAccounts.class);     log("Account " + ejbAccounts.getPrimaryKey() +       "; balance is $" + ejbAccounts.getBal()+" "+ejbAccounts.getType());     // double x=ejbAccounts.deposit(100.0*j);       double x=ejbAccounts.deposit(100.0*j);       x=ejbAccounts.withdraw(5000.0);       System.out.print("当前账户现金为: "+x);       ejbAccounts.remove();      j++;   }      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded initializing bean access.");        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://3as: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 Collection findByType(String Type) {    Collection returnValue = null;    long startTime = 0;    if (logging) {      log("Calling findByType(" + Type + ")");      startTime = System.currentTimeMillis();    }    try {      returnValue = ejbAccountsHome.findByType(Type);      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: findByType(" + Type + ")");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: findByType(" + Type + ")");      }      e.printStackTrace();    }    if (logging) {      log("Return value from findByType(" + Type + "): " + returnValue + ".");    }    return returnValue;  }  public EjbAccounts findByPrimaryKey(String id) {    long startTime = 0;    if (logging) {      log("Calling findByPrimaryKey(" + id + ")");      startTime = System.currentTimeMillis();    }    try {      ejbAccounts = ejbAccountsHome.findByPrimaryKey(id);      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: findByPrimaryKey(" + id + ")");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: findByPrimaryKey(" + id + ")");      }      e.printStackTrace();    }    if (logging) {      log("Return value from findByPrimaryKey(" + id + "): " + ejbAccounts + ".");    }    return ejbAccounts;  }  public EjbAccounts create(String Id) {    long startTime = 0;    if (logging) {      log("Calling create(" + Id + ")");      startTime = System.currentTimeMillis();    }    try {      ejbAccounts = ejbAccountsHome.create(Id);      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: create(" + Id + ")");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: create(" + Id + ")");      }      e.printStackTrace();    }    if (logging) {      log("Return value from create(" + Id + "): " + ejbAccounts + ".");    }    return ejbAccounts;  }  public EjbAccounts create(String id, String Type, double bal) {    long startTime = 0;    if (logging) {      log("Calling create(" + id + ", " + Type + ", " + bal + ")");      startTime = System.currentTimeMillis();    }    try {      ejbAccounts = ejbAccountsHome.create(id, Type, bal);      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: create(" + id + ", " + Type + ", " + bal + ")");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: create(" + id + ", " + Type + ", " + bal + ")");      }      e.printStackTrace();    }    if (logging) {      log("Return value from create(" + id + ", " + Type + ", " + bal + "): " + ejbAccounts + ".");    }

⌨️ 快捷键说明

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