RMI实现一个学生成绩或教师信息查询的程序
2011-08-28 21:53
666 查看
分布式实验报告
一、 实验目的及内容
在Java语言环境下,通过RMI实现一个学生成绩或教师信息查询的程序。
二、实验原理和步骤
1. 定义学生成绩查询或教师信息查询的远程接口
2. 实现服务器端软件(程序):设计远程接口的实现类和服务器对象类,在服务器上启动目录服务,并注册远程对象,供客户端访问。远程接口的实现类要从本地读取数据信息(成绩或教师信息),数据信息可以存储在文件或数据库中。
3. 实现客户端软件(程序):实现访问远程对象的客户程序。
本次实验主要实现一个学生的信息查询系统。包括服务端和客户端。类文件有:
其中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、程序运行截图:
一、 实验目的及内容
在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、程序运行截图:
相关文章推荐
- 笔试题:创建一个单链表,结点包含学生的学号,姓名,性别,年龄信息.写几个程序,实现按学生学号插入,查询,删除等操作.
- 3、写一个程序,定义一个学生结构体,至少要含4个数据项,其中有一项用来保存学生成绩。用此结构体存储班级上的所有学生信息
- 实现一个学生的成绩的录入与查询成绩(不算原创,也不算转载,是根据他人代码改动而来)
- 编写一个程序,将存放学生各种信息的文件中的学生信息读出,重新组成一个存放所有学生的前3门成绩的文件
- 写一个程序接收从键盘输入学生的信息,输入格式为:name,30,30,30(姓名,三门课成绩)
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 问题:设计一个大学教师和学生管理程序, 教师包括 编号、姓名、职称和教研室 数据的输入输出; 大学生包括编号、姓名、性别、班号、英语、高等数学和数据结构三门课程成绩的输入输出和计算平均分; 研究生包
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 编写一个学生和教师数据输入和显示程序,学生数据有编号,姓名,班号,和成绩,教师数据有编号,姓名,职称和部门。要求将编号,姓名输入显示设计成一个类person,并作为学生数据类t和教师数据操作类的基类
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 有五个学生,每个学生有3门课(语文、数学、英语)的成绩, * 写一个程序接收从键盘输入学生的信息,输入格式为:name,30,30,30(姓名,三门课成绩)
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 编写程序,查询所有学生选修课程的期末成绩和对应等级,如学生未选修任何课程 --则输出提示信息。用case选择结果
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 数学老师小y想写一个成绩查询系统,包含如下指令:1.insert [name] [score],向系统中插入一条信息,表示名字为name的学生的数学成绩为score.2.find [name],表示查
- 简单实现了一个学生成绩信息管理系统
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。