O'Reilly    
 Published on O'Reilly (http://oreilly.com/)
 See this if you're having trouble printing code examples


SQLJ, the Oracle JVM, and Enterprise JavaBeans: Example

[ Back to Article ]
/*
   The program Client.java illustrates how to use the
   Customer bean.
*/

// import the Customer bean classes
import customer.Customer;
import customer.CustomerHome;
import customer.CustomerRecord;

// import the Java Naming and Directory Interface classes
import oracle.aurora.jndi.sess_iiop.ServiceCtx;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Hashtable;

// import java.sql.Date (required for the dob attribute)
import java.sql.Date;

public class Client {

  public static void main(String [] args) throws Exception {

    // get the arguments from the command line
    if (args.length != 4) {
      System.out.println("usage: Client service_URL bean_name " +
        "username password");
      System.exit(1);
    }
    String service_URL = args[0];
    String bean_name = args[1];
    String username = args[2];
    String password = args[3];

    // initialize the Java Naming and Directory Interface (JNDI)
    // environment so that the Client program can run the Customer bean
    Hashtable env = new Hashtable();
    env.put(Context.URL_PKG_PREFIXES, "oracle.aurora.jndi");
    env.put(Context.SECURITY_PRINCIPAL, username);
    env.put(Context.SECURITY_CREDENTIALS, password);
    env.put(Context.SECURITY_AUTHENTICATION, ServiceCtx.NON_SSL_LOGIN);
    Context ic = new InitialContext(env);

    // create a CustomerHome object and use the lookup() method to
    // locate the Customer bean
    CustomerHome customer_home = (CustomerHome) ic.lookup(service_URL +
      bean_name);

    // create a Customer object and call the create() method to create
    // an instance of the Customer bean
    Customer customer_bean = customer_home.create();

    // create a CustomerRecord object and set it to the row returned
    // from the getRow() method contained in the Customer bean
    CustomerRecord customer_record = customer_bean.getRow(1);

    // display the CustomerRecord attributes
    System.out.println("Customer Information:");
    System.out.println("id = " + customer_record.id);
    System.out.println("first_name = " + customer_record.first_name);
    System.out.println("last_name = " + customer_record.last_name);
    System.out.println("dob = " + customer_record.dob);
    System.out.println("phone = " + customer_record.phone);
  }
}

[ Back to Article ]

Copyright © 2009 O'Reilly Media, Inc.