The Sample Client Application

To begin, generate a sample client application using JDeveloper: Right-click on the UserInfo bean and select Create Sample Java Client. Select either Connect to OC4J embedded in JDeveloper or Connect to Remote App Server, depending on where you've deployed the EJB.

The first part of the file is standard, setting up the parameters (which would be in a properties file in production application) to obtain the initial JNDI context, then calling JNDI to obtain the home interface.

 package Samplemypackage4; import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; import mypackage8.UserInfo; import mypackage8.UserInfoHome; import javax.ejb.FinderException; import javax.ejb.CreateException; import java.rmi.RemoteException; public class UserInfoClient2 {   public static void main(String [] args)   {     UserInfoClient2 userInfoClient2 = new UserInfoClient2();     try     {       Hashtable env = new Hashtable();       env.put(Context.INITIAL_CONTEXT_FACTORY,           "com.evermind.server.rmi.RMIInitialContextFactory");       env.put(Context.SECURITY_PRINCIPAL, "admin");       env.put(Context.SECURITY_CREDENTIALS, "adminpwd");       env.put(Context.PROVIDER_URL, "ormi://noizmaker/UserInfo");       Context ctx = new InitialContext(env);       UserInfoHome userInfoHome =           (UserInfoHome)ctx.lookup("UserInfo"); 

We'll first try to lookup an email address to see whether an EJB with that key already exists. If that fails, we'll create a new EJB for that email address.

 // Locate or create EJB String email="me@example.com"; UserInfo userInfo = null; try {   userInfo = userInfoHome.findByPrimaryKey(email); } catch (FinderException e) {   System.out.println("Bean not found: " + e); } if(userInfo==null) {   try   {     userInfo = userInfoHome.create(email, "me", "kitty" );   }   catch (Exception e)   {       System.out.println("Caught: " +e);       System.exit(1);   }   System.out.println("Created new bean:"); } else {   System.out.println("Bean already exists:"); } 

This roughly corresponds to how, in a real application, you might ask users for an email address to see whether they have an account in your system. If the email address is not found, you would then ask the user to sign up for an account by providing additional information.

The next section is intended to exercise all the methods in our home interface to get an EJB and change the data in it.

       // Access EJB attributes       String username;       System.out.println(" username: " +             (username = userInfo.getUsername()));       System.out.println(" email: " + userInfo.getEmail(  ));       System.out.println(" password: " +             userInfo.getPassword(  ));       System.out.println(" member since: " +             userInfo.getMemberSince(  ));       if(username.equals("delete me"))       {           // Remove EJB           userInfo.remove();           System.out.println("Bean removed");       }       else       {           // Change EJB attributes           userInfo.setUsername("delete me");           userInfo.setPassword("newpassword");           System.out.println("Bean altered");        }     }     catch(Throwable ex)     {       ex.printStackTrace();     }   } } 

The first time you run this application it displays all the attributes of the bean that we just created. It then changes the username and password. At this point, you can start SQL*Plus and verify that the EJB container has saved the bean's data in the USER_INFO table.

The second time you run the application the bean will already exist. Once again, the attributes will be displayed, but this time, at the end, the bean will be destroyed by calling the remote interface's remove() method. At the end, you can verify the deletion of the record in the USER_INFO table by using SQL*Plus.



Java Oracle Database Development
Java Oracle Database Development
ISBN: 0130462187
EAN: 2147483647
Year: 2002
Pages: 71

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net