?? testclient.java~47~
字號:
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); //String []a=new String; String []anOrder =( String[])allEmployee.elementAt(ww); for(int mm=0;mm<anOrder.length;mm++) { System.out.println("個數 "+ mm +": "+ anOrder[mm]); } /*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(); }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -