⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.java

📁 精通Java核心技术源代码
💻 JAVA
字号:
import java.util.*;
import javax.naming.*;
public class Client
{
	InitialContext init;
	TeacherHome teacherHome;
	Teacher teacher;
	Student student;
	StudentHome studentHome;
	public Client()
	{
		init=this.getInitialContext();
	}
	public static void main(String[] args)throws Exception
	{
		Client client=new Client();
	    //client.testTeacher();
	    //client.testStudent();
	   // client.testRelationShip();
	    client.findRelationship2();
	   System.out.println("testEjbQl_findAll");
	   client.testEjbQl_findAll();
	   System.out.println("testEjbQl_ByDegree");
	   client.testEjbQl_ByDegree("dr.");
	   System.out.println("testEjbQl_findStudentsByTeacher");
	   client.testEjbQl_findStudentsByTeacher("002");   
	   
		
	
	}
	
	public void testEjbQl_findAll()throws Exception
	{
		
		 Object objref = init.lookup("ejb/teacher");		
        teacherHome = (TeacherHome)javax.rmi.PortableRemoteObject.narrow(objref, TeacherHome.class);
		Collection teachers=teacherHome.findAll();
		Iterator it=teachers.iterator();
        while(it.hasNext())
        {
        	Teacher teacher=(Teacher)it.next();
        	System.out.println(teacher.getId());
        	System.out.println(teacher.getDegree());
        }
    }
    public void testEjbQl_ByDegree(String degree)throws Exception
    {
    	Object objref = init.lookup("ejb/teacher");		
        teacherHome = (TeacherHome)javax.rmi.PortableRemoteObject.narrow(objref, TeacherHome.class);
		Collection teachers=teacherHome.findByDegree(degree);
		Iterator it=teachers.iterator();
        while(it.hasNext())
        {
        	Teacher teacher=(Teacher)it.next();
        	System.out.println(teacher.getId());
        	System.out.println(teacher.getDegree());
        }
    }
    
    public void testEjbQl_findStudentsByTeacher(String id)throws Exception
    {
    	Object objref = init.lookup("ejb/teacher");		
        teacherHome = (TeacherHome)javax.rmi.PortableRemoteObject.narrow(objref, TeacherHome.class);
		Collection stuents=teacherHome.findStudentsByTeacher(id);
		Iterator it=stuents.iterator();
        while(it.hasNext())
        {
        	Student st=(Student)it.next();
        	System.out.println(st.getId());
        	System.out.println(st.getName());
        }
    }
    
    
		
	public void findRelationship()throws Exception
	{
		Object objref = init.lookup("ejb/student");
		 studentHome = (StudentHome)javax.rmi.PortableRemoteObject.narrow(objref, StudentHome.class); 
        student=studentHome.findByPrimaryKey("002");
        Collection teachers=student.getTeachers();
        Iterator it=teachers.iterator();
        while(it.hasNext())
        {
        	Teacher teacher=(Teacher)it.next();
        	System.out.println(teacher.getId());
        	System.out.println(teacher.getDegree());
        }
    }
		
    public void findRelationship2()throws Exception
	{
		Object objref = init.lookup("ejb/teacher");
		 teacherHome = (TeacherHome)javax.rmi.PortableRemoteObject.narrow(objref, TeacherHome.class); 
        teacher=teacherHome.findByPrimaryKey("007");
        Collection students=teacher.getStudents();
        Iterator it=students.iterator();
        System.out.println(students.size());
        while(it.hasNext())
        {
        	Student st=(Student)it.next();
        	System.out.println(st.getName());
        	System.out.println(st.getId());
        	
        }
    }
    
    
	public void testTeacher()
	{
		try
		{
			
	     Object objref = init.lookup("ejb/teacher");
		
        teacherHome = (TeacherHome)javax.rmi.PortableRemoteObject.narrow(objref, TeacherHome.class); 
        teacher=teacherHome.create("001");
        teacher.setDegree("dr.");
        System.out.println(teacher.getDegree());
        }
	    catch(Exception e)
	    {
	    	e.printStackTrace();
	    }
    }
    
    public void testStudent()
	{
		try
		{
			
	     Object objref = init.lookup("ejb/student");
		
        studentHome = (StudentHome)javax.rmi.PortableRemoteObject.narrow(objref, StudentHome.class); 
        student=studentHome.create("001");
        student.setName("jack");
        System.out.println(student.getName());
        }
	    catch(Exception e)
	    {
	    	e.printStackTrace();
	    }
    }


   public void testRelationShip()throws Exception
   {
   	    Object objref = init.lookup("ejb/student");		
        studentHome = (StudentHome)javax.rmi.PortableRemoteObject.narrow(objref, StudentHome.class); 
        student=studentHome.create("009");
        student.setName("jhon");
        
         Object objref2 = init.lookup("ejb/teacher");
		
        teacherHome = (TeacherHome)javax.rmi.PortableRemoteObject.narrow(objref2, TeacherHome.class); 
        teacher=teacherHome.create("009");
        teacher.setDegree("dr.");
        Collection c2=new ArrayList();
        c2.add(student);
        teacher.setStudents(c2);
        
        Collection c=new ArrayList();
        c.add(teacher);
        student.setTeachers(c);
      
    }
        
        

	public static InitialContext getInitialContext()    
   {
         	Properties p = new Properties();
            p.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
             p.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
             p.put("java.naming.provider.url","localhost");            	

       
       try
       {
       	 return new javax.naming.InitialContext(p);
      }
      catch(Exception e)
      {
      	e.printStackTrace();
      	return null;
    }
  }	
}	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -