您的位置:首页 > 移动开发 > Objective-C

RMI实现一个学生成绩或教师信息查询的程序

2011-08-28 21:53 666 查看
分布式实验报告

一、 实验目的及内容

在Java语言环境下,通过RMI实现一个学生成绩或教师信息查询的程序。

二、实验原理和步骤

1. 定义学生成绩查询或教师信息查询的远程接口

2. 实现服务器端软件(程序):设计远程接口的实现类和服务器对象类,在服务器上启动目录服务,并注册远程对象,供客户端访问。远程接口的实现类要从本地读取数据信息(成绩或教师信息),数据信息可以存储在文件或数据库中。

3. 实现客户端软件(程序):实现访问远程对象的客户程序。 

三、 实验过程及结果

1、类文件:

本次实验主要实现一个学生的信息查询系统。包括服务端和客户端。类文件有:





其中StudentInterface.java是远程接口类,SqlExcuter.java实现了远程接口并继承了UnicastRemoteObject,DB.java用于连接数据库。MyServer.java用于RMI注册.StudentSystem.java处理界面,StuModel.java用于查找并进行远程调用。

2、关键模块程序说明;

Ø 服务器端

远程接口类:

public interface StudentInterface extends Remote {

public String sayHello(String name)

throws java.rmi.RemoteException;

public Vector queryIfo(String str)

throws java.rmi.RemoteException;

} //end interface

实现远程接口:

public Vector queryIfo(String str) throws RemoteException{

Vector rows = new Vector();

try {

ct = DB.getConnection( );

ps = ct.prepareStatement(str);

rs = ps.executeQuery();

while(rs.next()){

Vector row = new Vector();

row.add(rs.getString(1));

row.add(rs.getString(2));

row.add(rs.getString(3));

row.add(rs.getInt(4));

row.add(rs.getString(5));

row.add(rs.getString(6));

// System.out.print( row.get(0).toString());

rows.add(row);

// System.out.print( rows.get(0).toString());

}

} catch (Exception e1) {

// TODO: handle exception

e1.printStackTrace();

} finally {

   DB.close(ct,rs,  ps);

}

return  rows;

}

RMI在服务器注册:

String portNum, registryURL;

try{    

         portNum = Integer.toString(20081);//(br.readLine()).trim();

int RMIPortNum = Integer.parseInt(portNum);

startRegistry(RMIPortNum);

         SqlExcuter exportedObj = new SqlExcuter();

         registryURL = "rmi://localhost:" + portNum + "/hello";

         Naming.rebind(registryURL, exportedObj);

         System.out.println

("Server registered.  Registry currently contains:");

// list names currently in the registry

listRegistry(registryURL);

         System.out.println("Hello Server ready.");

      }// end try

catch (Exception re) {

         System.out.println("Exception in HelloServer.main: " + re);

      } // end catch

  } // end main

// This method starts a RMI registry on the local host, if it

// does not already exists at the specified port number.

private static void startRegistry(int RMIPortNum)

throws RemoteException{

try {

         Registry registry = LocateRegistry.getRegistry(RMIPortNum);

         registry.list( );  // This call will throw an exception

// if the registry does not already exist

      }

catch (RemoteException e) {

// No valid registry at that port.

  System.out.println

  ("RMI registry cannot be located at port "

        + RMIPortNum);

         Registry registry =

            LocateRegistry.createRegistry(RMIPortNum);

       System.out.println(

"RMI registry created at port " + RMIPortNum);

      }

   } // end startRegistry

// This method lists the names registered with a Registry object

private static void listRegistry(String registryURL)

throws RemoteException, MalformedURLException {

       System.out.println("Registry " + registryURL + " contains: ");

       String [ ] names = Naming.list(registryURL);

for (int i=0; i < names.length; i++)

          System.out.println(names[i]);

  } //end listRegistry

Ø 客户端

远程接口:

public interface StudentInterface extends Remote {

public String sayHello(String name)

throws java.rmi.RemoteException;

public Vector queryIfo(String str)

throws java.rmi.RemoteException;

} //end interface

远程方法调用:

String hostName = "localhost";

         String portNum = "20081";//br.readLine();

         RMIPort = Integer.parseInt(portNum);

         String registryURL =

"rmi://" + hostName+ ":" + portNum + "/hello"; 

// find the remote object and cast it to an interface object

         StudentInterface h =

           (StudentInterface)Naming.lookup(registryURL);

         System.out.println("Lookup completed " );

// invoke the remote method

rows =  h.queryIfo(str);

3、使用介绍:

先启动服务器,则运行MyServer.java.

然后启动StudentSystem.java。输入要查询的名字,确定后进行远程方法调用,查询出学生的相应信息。

4、程序运行截图:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐