package examples.ejb.sequence.oracle; 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 employees with a salary * 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.oracle.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.oracle.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 salary = 3000; try { // Contact the OracleBean container (the "OracleHome") through JNDI. Context ctx = getInitialContext(); OracleHome home = (OracleHome) ctx.lookup("oracle.OracleHome"); // Create a new employee with a salary // Display the employee number that was assigned and the salary System.out.println(""); Oracle emp1 = home.create(salary); System.out.println("Employee No. " + emp1.accountId() + " created with a salary of $" + emp1.salary()); // Create another new employee with twice the salary // Display the employee number that was assigned and the salary Oracle emp2 = home.create(salary*2); System.out.println("Employee No. " + emp2.accountId() + " created with a salary of $" + emp2.salary()); System.out.println("\nRemoving employee records created..."); emp1.remove(); emp2.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.oracle.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); } }