Monday, 4 May 2009

8-How to Develop Bean-Managed-Persistence Entity Beans

FILE: Client.java

import bmpex.Student;
import bmpex.StudentHome;

import java.util.*;
import javax.naming.*;
import javax.ejb.*;


public class Client {
public static void main(String[] args) {
print("Starting Client . . .");
Context initialContext = null;
StudentHome studentHome = null;
Student student = null;
try {
print("Looking up the student home via JNDI.");
initialContext = new InitialContext();
Object object = initialContext.lookup("bmpex/Student");

studentHome =
(StudentHome)javax.rmi.PortableRemoteObject.narrow(object,
StudentHome.class);

String studentId = "raghava";
print("Creating a new student id:" + studentId + ".");
object =
(Student)studentHome.create(studentId, "Raghava", "Kothapalli",
"1234, People Dr. Pleasonton, CA");
//-------------------------------------------------------
student =
(Student)javax.rmi.PortableRemoteObject.narrow(object, Student.class);

print("Locating the student id " + studentId + " using findByPrimaryKey method");
student = studentHome.findByPrimaryKey(studentId);
print("Locating all students with last name Kothapalli " +
" using findByLastName method.");
//---------------------------------------------------------
Collection col = studentHome.findByLastName("Kothapalli");
Enumeration students = Collections.enumeration(col);

while (students.hasMoreElements()) {
student =
(Student)javax.rmi.PortableRemoteObject.narrow(students.nextElement(),
Student.class);
print("id:" + student.getStudentId() + " First name:" +
student.getFirstName());
}
//---------------------------------------------------------
print("Finding the total number of students using " +
" getTotalNumberOfStudents method");
int count = studentHome.getTotalNumberOfStudents();
print("Count:" + count);
//---------------------------------------------------------
} catch (Exception ex) {
ex.printStackTrace();
}
}

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