?? testclient.java~41~
字號:
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++) { LoginBean anOrder = (LoginBean) allEmployee.elementAt(ww); System.out.println(anOrder.getType());}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 + "."); } return ejbAccounts; } //---------------------------------------------------------------------------- // Methods that use Remote interface methods to access data through the bean //---------------------------------------------------------------------------- public void setId(String id) { if (ejbAccounts == null) { System.out.println("Error in setId(): " + ERROR_NULL_REMOTE); return ; } long startTime = 0; if (logging) { log("Calling setId(" + id + ")"); startTime = System.currentTimeMillis(); } try { ejbAccounts.setId(id); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: setId(" + id + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: setId(" + id + ")"); } e.printStackTrace(); } } public String getId() { String returnValue = ""; if (ejbAccounts == null) { System.out.println("Error in getId(): " + ERROR_NULL_REMOTE); return returnValue; } long startTime = 0; if (logging) { log("Calling getId()"); startTime = System.currentTimeMillis(); } try { returnValue = ejbAccounts.getId(); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: getId()"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: getId()"); } e.printStackTrace(); } if (logging) { log("Return value from getId(): " + returnValue + "."); } return returnValue; } public void setBal(double bal) { if (ejbAccounts == null) { System.out.println("Error in setBal(): " + ERROR_NULL_REMOTE); return ; } long startTime = 0; if (logging) { log("Calling setBal(" + bal + ")"); startTime = System.currentTimeMillis(); } try { ejbAccounts.setBal(bal); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: setBal(" + bal + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: setBal(" + bal + ")"); } e.printStackTrace(); } } public double getBal() { double returnValue = 0f; if (ejbAccounts == null) { System.out.println("Error in getBal(): " + ERROR_NULL_REMOTE); return returnValue; } long startTime = 0; if (logging) { log("Calling getBal()"); startTime = System.currentTimeMillis(); } try { returnValue = ejbAccounts.getBal(); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: getBal()"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: getBal()"); } e.printStackTrace(); } if (logging) { log("Return value from getBal(): " + returnValue + "."); } return returnValue; } public void setType(String type) { if (ejbAccounts == null) { System.out.println("Error in setType(): " + ERROR_NULL_REMOTE); return ; } long startTime = 0; if (logging) { log("Calling setType(" + type + ")"); startTime = System.currentTimeMillis(); } try { ejbAccounts.setType(type); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: setType(" + type + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: setType(" + type + ")"); } e.printStackTrace(); } } public String getType() { String returnValue = ""; if (ejbAccounts == null) { System.out.println("Error in getType(): " + ERROR_NULL_REMOTE); return returnValue; } long startTime = 0; if (logging) { log("Calling getType()"); startTime = System.currentTimeMillis(); } try { returnValue = ejbAccounts.getType(); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: getType()"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: getType()"); } e.printStackTrace(); } if (logging) { log("Return value from getType(): " + returnValue + "."); } return returnValue; } public double deposit(double bal) { double returnValue = 0f; if (ejbAccounts == null) { System.out.println("Error in deposit(): " + ERROR_NULL_REMOTE); return returnValue; } long startTime = 0; if (logging) { log("Calling deposit(" + bal + ")"); startTime = System.currentTimeMillis(); } try { returnValue = ejbAccounts.deposit(bal); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: deposit(" + bal + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: deposit(" + bal + ")"); } e.printStackTrace(); } if (logging) { log("Return value from deposit(" + bal + "): " + returnValue + "."); } return returnValue; } public void testRemoteCallsWithDefaultArguments() { if (ejbAccounts == null) { System.out.println("Error in testRemoteCallsWithDefaultArguments(): " + ERROR_NULL_REMOTE); return ; } setId(""); getId(); setBal(0f); getBal(); setType(""); getType(); deposit(0f); } //---------------------------------------------------------------------------- // 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); } } private EjbAccounts findOrCreateAccount(String id, String accountType,double balance) throws CreateException, FinderException, RemoteException { EjbAccounts remote = null; try { remote = (EjbAccounts) PortableRemoteObject.narrow(ejbAccountsHome.findByPrimaryKey(id), EjbAccounts.class); } catch (ObjectNotFoundException onfe) { // the account id does not yet exist so create it. remote = create(id, accountType,balance); } return remote; } //Main method public static void main(String[] args) { TestClient client = new TestClient(); // 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 + -