package examples.ejb.sequence.jdbc; import javax.naming.*; import javax.ejb.*; import java.rmi.RemoteException; import java.util.Hashtable; /** * This class demonstrates calling an entity EJBean, * followed by creating two new accounts with an opening balance * and an automatically generated primary key. *

* This class also illustrates how to search the JNDI tree * for an appropriate container. * * @author Copyright (c) 1998 by WebLogic, Inc. All Rights Reserved. * @author Copyright (c) 1998-1999 by BEA WebXpress. All Rights Reserved. */ public class Client { static String url = "t3://localhost:7001"; static String user = null; static String password = null; /** * Runs this example from the command line. Example: *

* java examples.ejb.sequence.jdbc.Client "t3://localhost:7001" scott tiger *

* The parameters are optional, but if any are supplied, * they are interpreted in this order: *

* @param url URL such as "t3://localhost:7001" of Server * @param user User name, default null * @param password User password, default null */ public static void main(String[] args) { System.out.println("\nBeginning sequence.jdbc.Client...\n"); // Parse the argument list if ((args == null) || (args.length == 0)) {} else for (int i = 0; i < args.length; i++) { switch(i) { case 0: url = args[i]; break; case 1: user = args[i]; break; case 2: password = args[i]; break; default: } } double balance = 3000; try { // Contact the AutoAccountBean container (the "AutoAccountHome") through JNDI. Context ctx = getInitialContext(); AutoAccountHome home = (AutoAccountHome) ctx.lookup("jdbc.AutoAccountHome"); // Create a new account with an opening balance // Display the account id that was assigned and the opening balance AutoAccount ac1 = home.create(balance); System.out.println("AutoAccount " + ac1.accountId() + " created with an opening balance of $" + ac1.balance()); // Create another new account with twice the opening balance // Display the account id that was assigned and the opening balance AutoAccount ac2 = home.create(balance*2); System.out.println("AutoAccount " + ac2.accountId() + " created with an opening balance of $" + ac2.balance()); System.out.println("\nRemoving AutoAccounts created..."); ac1.remove(); ac2.remove(); // Catch any exceptions } catch (ProcessingErrorException pe) { System.out.println("Unexpected Processing Error: " + pe); } catch (Exception e) { System.out.println(":::::::::::::: Unexpected Error :::::::::::::::::"); e.printStackTrace(); } finally { System.out.println("\nEnd sequence.jdbc.Client..."); } } /** * Gets an initial context for the current user, password and url. * * @return Context * @exception java.lang.Exception if there is * an error in getting the Context */ static public Context getInitialContext() throws Exception { Hashtable h = new Hashtable(); h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); h.put(Context.PROVIDER_URL, url); if (user != null) { System.out.println ("user: " + user); h.put(Context.SECURITY_PRINCIPAL, user); if (password == null) password = ""; h.put(Context.SECURITY_CREDENTIALS, password); } return new InitialContext(h); } }