📄 studentcmpclient.java
字号:
package cmp;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
public final class StudentCMPClient extends BaseClient {
private StudentHome studentHome;
private BookHome bookHome;
private TeacherHome teacherHome;
private FamilyHome familyHome;
private Context ctx;
public StudentCMPClient(String [] argv) throws NamingException{
super(argv);
ctx = getInitialContext();
studentHome =
(StudentHome) narrow(ctx.lookup("StudentCMPEJB"), StudentHome.class);
bookHome = (BookHome) narrow(ctx.lookup("BookCMPEJB"), BookHome.class);
teacherHome =
(TeacherHome)narrow(ctx.lookup("TeacherCMPEJB"), TeacherHome.class);
familyHome = (FamilyHome)narrow(ctx.lookup("FamilyCMPEJB"),
FamilyHome.class);
}
public static void main(String[] argv) throws Exception
{
StudentCMPClient sc = new StudentCMPClient(argv);
sc.runClient();
}
private void runClient() throws Exception{
System.out.println("Running CMP Mmnager Relation Example.\n");
Student [] students = null;
Family [] familys = null;
Book [] books = null;
Teacher [] teachers = null;
try {
students = createStudents();
// create 1 family for each student
familys = createFamilys(students.length);
// create 1 book for each student;
books = createBooks(students.length);
teachers = createTeachers();
} catch (Exception e) {
System.err.println("Unable to create the CMP Example.");
}
System.out.println("Updating relationships....\n");
// Assign each Student a Family
System.err.println("Assigning each Student to a Family.");
for (int i=0; i<students.length; i++) {
students[i].assignFamily(familys[i]);
}
// Assign the books out to the students
System.err.println("Assigning books to each Student.");
for (int i=0; i<books.length; i++) {
students[i % students.length].assignBook(books[i]);
}
// Assign students to teachers
// Student 0 gets teacher 0
// Student 1 gets teacher 0,1 etc.
System.err.println("Assigning Students to Teachers.\n");
for (int i=0; i<teachers.length; i++) {
for (int j=0; j<i; j++) {
students[j].assignTeacher(teachers[i]);
}
}
System.out.println("Finding All the Students in grade 5:");
try {
Collection c = studentHome.findStudentsInGrade(5);
if (c.isEmpty()) {
System.err.println("No Students were found in grade 5.");
} else {
Iterator it = c.iterator();
while (it.hasNext()) {
Student s = (Student) narrow(it.next(), Student.class);
System.err.println("Found student named: "+s.getName()+
" in grade 5.");
}
System.out.println("");
}
} catch (Exception e) {
System.err.println("Unable to find students in grade 5.");
System.err.println("The error was: ");
//e.printStackTrace();
//throw e;
}
System.out.println("Finding the Students with a Family:");
try {
Collection c = studentHome.findStudentsWithAFamily(2);
if (c.isEmpty()) {
System.out.println("No Students were found with Familys.");
} else {
Iterator it = c.iterator();
while (it.hasNext()) {
Student s = (Student) narrow(it.next(), Student.class);
System.err.println("Found student named: "+s.getName()+
" with a Family");
}
System.out.println("");
}
} catch (Exception e) {
System.err.println("Unable to find students with Familys.\n");
System.err.println("The error was: ");
}
System.out.println("Finding All the Students with a Book:");
try {
Collection c = studentHome.findStudentsWithBooksTitled("C/C++ program");
if (c.isEmpty()) {
System.err.println("No Students were found with books named Hamlet.");
} else {
Iterator it = c.iterator();
while (it.hasNext()) {
Student s = (Student) narrow(it.next(), Student.class);
System.err.println("Found student named: "+s.getName()+
" with a book named C/C++ program.");
}
System.err.println("");
}
} catch (Exception e) {
System.err.println("Unable to find students WITH books named Hamlet.");
System.err.println("The error was: ");
}
}
//Create Beans For CMP Examples
private Student[] createStudents() throws CreateException,RemoteException
{
System.out.println("Creating Student CMP Beans...");
String [] names ={"LiMing","WangHong","YangYang","Mike","Peter" };
deleteOldStudents(names.length);
Student [] students = new Student[names.length];
for (int i=0; i<names.length; i++) {
//create 5 students in grade 2 or 5
if(i%2!=0) //奇数学生的班级设为2
students[i] = studentHome.create(names[i], i, 2);
else
students[i] = studentHome.create(names[i], i, 5);
}
return students;
}
private Family[] createFamilys(int N) throws CreateException,RemoteException
{
System.out.println("Creating Family CMP Beans...");
deleteOldFamilys(N);
Family [] familys = new Family[N];
for (int i=0; i<N; i++){
//reate students in 5 family
familys[i] = familyHome.create(new Integer(i),"YangGao Road No "+i);
}
return familys;
}
private Teacher [] createTeachers()
throws CreateException, RemoteException
{
System.err.println("Creating Teacher CMP Beans...");
String [] names = { "Jack", "Jim", "HaLi Han", "TinXianShen"};
deleteOldTeachers(names.length);
Teacher [] teachers = new Teacher[names.length];
for (int i=0; i<names.length; i++){
// create our teachers
teachers[i] = teacherHome.create(names[i], new Integer(i));
}
return teachers;
}
private Book[] createBooks(int numCopies) throws CreateException,RemoteException
{
System.out.println("Creating Book CMP beans...");
String [] titles = {"C/C++ program", "Java Think", "Data Structure"};
String [] authors = {"ZhanJunGu", "ZhangHZ", "SuHong"};
deleteOldBooks(titles.length, numCopies);
Book [] books = new Book[titles.length * numCopies];
for (int i=0; i<titles.length; i++) {
for (int j=0; j<numCopies; j++) {
books[i*numCopies+j] =
bookHome.create(authors[i],titles[i],String.valueOf(i),j);
}
}
return books;
}
// Utility code
private void deleteOldFamilys(int N) {
for (int i=0; i<N; i++) {
try {
familyHome.remove(new Integer(i));
} catch (Exception ignore) {}
}
}
private void deleteOldStudents(int N) {
for (int i=0; i<N; i++) {
try {
studentHome.remove(new Integer(i));
} catch (Exception ignore) {}
}
}
private void deleteOldTeachers(int N) {
for (int i=0; i<N; i++) {
try {
teacherHome.remove(new Integer(i));
} catch (Exception ignore) {}
}
}
private void deleteOldBooks(int N, int numCopies) {
BookPK pk;
for (int i=0; i<N; i++) {
for (int j=0; j<numCopies; j++) {
try {
pk = new BookPK(String.valueOf(i), j);
bookHome.remove(pk);
} catch (Exception ignore) {}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -