Wednesday, 6 May 2009

3-How to Develop Container-Managed-Persistence Entity Beans

Client

FILE Client.java
import cmpex.*;
import java.util.*;
import javax.naming.*;
import javax.ejb.*;

public class Client {
public static void main(String[] args) {
print("Starting Client . . .\n");
Context initialContext = null;
OrderARSHome orderHome = null;
OrderARS order = null;
try {
print("Looking up the order home via JNDI.\n");
initialContext = new InitialContext();
Object object = initialContext.lookup("cmpex/OrderARS");
orderHome =
(OrderARSHome)javax.rmi.PortableRemoteObject.narrow(object,
OrderARSHome.class);
order = (OrderARS)orderHome.create("1", "Submitted", 100);
String orderId = order.getOrderId();
print("Created a new order:" + orderId + " .\n");
print("Locating the order " + orderId +
" using findByPrimaryKey method.\n");

order = orderHome.findByPrimaryKey(orderId);
print("Locating all orders with status Submitted " +
" using findByStatus method.\n");
Collection col = orderHome.findByStatus("Submitted");
Enumeration orders = Collections.enumeration(col);

while (orders.hasMoreElements()) {
order = (OrderARS)orders.nextElement();
print("Order id:" + order.getOrderId());
}

print("Finding the total amount of all orders using \n" +
" getTotalAmountOfAllOrders home method.\n");
double totalAmount = orderHome.getTotalAmountOfAllOrders();
print("Total amount:" + totalAmount);
} catch (Exception e) {
e.printStackTrace();
}
}

static void print(String s) {
System.out.println(s);
}
}