做完实验,来个小结。
【实验原理和步骤】
1. 定义学生成绩查询或教师信息查询的远程接口
2.
实现服务器端软件(程序):设计远程接口的实现类和服务器对象类,在服务器
上启动目录服务,并注册远程对象,供客户端访问。远程接口的实现类要从本地读取
数据信息(成绩或教师信息),数据信息可以存储在文件或数据库中。
3.
实现客户端软件(程序):实现访问远程对象的客户程序。
【方案设计】
1)编写 RMI 中的接口 public interface GetScoreInterface extends
Remote;
2)编写接口实现方案 public class GetScoreImpl extends UnicastRemoteObject
implements
GetScoreInterface,在此类中,完成对数据库的访问,获得需要的信息,并返回给客户端;
3)编写服务器端程序
public class rmi_server;
4)编写客户端程序 public class rmi_client;
5)用 rmic 命令生成
stub 文件后,对程序进行运行调试。
【实验环境】
ubuntu 12.04
java version "1.7.0_17"
mysql Ver 14.14 Distrib
5.5.37, for debian-linux-gnu (i686) using readline 6.2
Eclipse Platform
Version: 4.2.1
上面是从实验报告中直接复制粘贴过来的,下面说一些小细节:
1)RMI网络编程提供了各种接口,调用一套函数完后,就算写完了,真是清晰明了啊~
2)程序运行有一套安全机制,具体的我没深究=。=还有一堆实验要写。。。女神原谅我现在犯个懒先吧。。。
3)没了=。=下面给源码
interface GetScoreInterface extends Remote:
import java.rmi.*; import java.sql.SQLException; /** * This remote method return the students score * @author Alex <alexande2008@gmail.com> * */ public interface GetScoreInterface extends Remote{ public String getScore(String sno)throws RemoteException,SQLException; }
class GetScoreImpl extends UnicastRemoteObject implements GetScoreInterface:
import java.rmi.*; import java.rmi.server.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * This class implement the interface GetScoreInterface * @author Alex <alexande2008@gmail.com> * */ public class GetScoreImpl extends UnicastRemoteObject implements GetScoreInterface{ String url="jdbc:mysql://localhost:3306/db_exam?&setUnicode=true&characterEncoding=utf-8"; String driver="com.mysql.jdbc.Driver"; Connection con=null; private String usr="exam_admin"; private String psw="qaz123"; public GetScoreImpl() throws RemoteException{ super(); } /** * Implement the interface abstract method getScore */ public String getScore(String sno)throws RemoteException,SQLException{ String scores=""; String sql=new String("select exam_name,mark from designs natural join marks where sno="+sno+";"); ResultSet rs=null; try{ Class.forName(driver); con=DriverManager.getConnection(url , usr, psw); Statement st=con.createStatement(); rs=st.executeQuery(sql); while(rs.next()){ //System.out.print(rs.getString(1)+"\t"+rs.getString(2)); scores+=rs.getString(1)+"\t"+rs.getString(2)+"\n"; } }catch(SQLException e){ System.out.print("Exception in GetScoreImpl"+e.getMessage()); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return scores; }/* public static void main(String[] a) throws RemoteException, SQLException{ GetScoreImpl aaa=new GetScoreImpl(); String kkk=aaa.getScore("20110012"); System.out.print(kkk); }*/ }
rmi_client:
import java.io.*; import java.rmi.*; /** * License: GPL * Description: RMI client which request the scores of the student whose number was given * @author Alex <alexande2008@gmail.com> * */ public class rmi_client { public static void main(String[] args){ try{ int RMIPort; //String hostname; InputStreamReader is=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(is); //System.out.println("Enter the host name"); //hostname=br.readLine(); System.out.println("Enter the port number"); RMIPort=Integer.parseInt(br.readLine().trim()); String registryURL="rmi://localhost:"+RMIPort+"/GetScore"; GetScoreInterface kkk=(GetScoreInterface)Naming.lookup(registryURL); System.out.println("Look up completed"); System.out.println("Enter the student number of which you want to check marks"); String sno=br.readLine().trim(); String score=kkk.getScore(sno); System.out.println(score); }catch(Exception e){ System.out.println("Exception in rmi_client main: "+e.getMessage()); } } }
rmi_server:
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; /** * License: GPL * Description: RMI server which returns the scores of the student whose number was given * @author Alex <alexande2008@gmail.com> * */ public class rmi_server { public static void main(String[] args){ InputStreamReader is=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(is); String registryURL; int portNum; try{ System.out.println("Enter RMIregistry porn number:"); portNum=Integer.parseInt((br.readLine()).trim()); registryURL="rmi://localhost:"+new Integer(portNum).toString()+"/GetScore"; register(registryURL,portNum); GetScoreImpl exportedObj=new GetScoreImpl(); Naming.rebind(registryURL, exportedObj); System.out.println("GetSocre server ready"); }catch(Exception e){ System.out.println("Exception in rmi_server main:"+e.getMessage()); } } /** * This method register a RMI local host */ public static void register(String registryURL,int RMIPortNum)throws RemoteException, MalformedURLException{ try{ Registry registry=LocateRegistry.getRegistry(RMIPortNum); registry.list(); }catch(RemoteException e){ System.out.println("RMI registry cannot be located port "+RMIPortNum); LocateRegistry.createRegistry(RMIPortNum); System.out.println("RMI registry created at port "+RMIPortNum); } try{ System.out.println("Registry"+registryURL+" contains:"); for(String name:Naming.list(registryURL)) System.out.println(name); } catch(MalformedURLException e){ System.out.println("Exception in rmi_server register,MalformedURLException"+e.getMessage()); } } }
p个小s: 用rmic生成stub文件,在这里是rmic GetScoreImpl (后面不加后缀了)
打完收工=。=接着写下一个实验。。。
原文:http://www.cnblogs.com/alex-wood/p/3746423.html