现在的位置: 首页 > 综合 > 正文

Java rmi 远程调用

2013年03月23日 ⁄ 综合 ⁄ 共 3641字 ⁄ 字号 评论关闭

这次是用java实现RMI 的远程调用:

编写的过程大致为:

1.      首先我们的启动Mysqlserver ,然后再里面建立一个数据库,以便我们在数据库中插入存放学生信息等。

2.      先编写一个接口DataServer,并且继承Remote类,然后再接口里面写上服务器端能实现的方法,然后再定义一个类DataServerImpl 继承UnicastRemoteObject
在实现接口DataServer,并且一一实现它定义在借口里面得到方法,这里我们重新写了一个类DBManager,用来实现与数据库的连接,包括插入数据,根据学号、姓名等进行查询等操作,其源代码见附件。

3.      下面就是需要使用rmic命令进行编译DataServerImpl文件,并且产生两个文件,产生这两个文件后就可以编写服务器端的代码了,主要是在主函数中生成一个DataServerImpl对象,然后在绑定一个端口在程序中,在绑定一个url地址,来绑定服务的对象,这样服务器端的程序就写好了。

4.      下面就是写客户端的代码了。

5.      首先是通过Naming.lookup(url),(url)就是服务器端指定的url地址这样就可以得到一个DataServer的对象,然后得到这样一个对象后就可以调用它的方法了。这样也就实现了调用远程服务器端的代码了,所以说这样RMI就比本上写完了。


首先是编写远程的接口调用函数:

  1. <span style="font-size:16px;">import java.rmi.Remote;  
  2. import java.rmi.RemoteException;  
  3.   
  4. public interface DataServer extends Remote {  
  5.     public void CreateTable() throws RemoteException;  
  6.     public void insert(int id ,String name,double Score) throws RemoteException;  
  7.     public double select(int id)throws RemoteException;  
  8.     public double select (String name)throws RemoteException;  
  9. }  
  10. </span>  


由于代码中使用到了与数据库的连接,所以写了一个数据库的管理类,代码如下:

  1. <span style="font-size:16px;">import java.sql.Connection;  
  2. import java.sql.DriverManager;  
  3. import java.sql.ResultSet;  
  4. import java.sql.SQLException;  
  5.   
  6. public class DBManager {  
  7.     private static String user = "root";  
  8.     private static String pass = "123456";  
  9.     private static String className ="com.mysql.jdbc.Driver";  
  10.     private static String url = "jdbc:mysql://localhost:3306/students";  
  11.     private static Connection conn;  
  12.     private static java.sql.Statement state;  
  13.     public static void init()  
  14.     {  
  15.         try {  
  16.             Class.forName(className);  
  17.              conn = DriverManager.getConnection(url,user,pass);  
  18.              state =conn.createStatement();  
  19.                
  20.                
  21.         } catch (ClassNotFoundException e) {  
  22.             // TODO Auto-generated catch block  
  23.             e.printStackTrace();  
  24.         } catch (SQLException e) {  
  25.             // TODO Auto-generated catch block  
  26.             e.printStackTrace();  
  27.         }  
  28.     }  
  29.     public static  void CreateTable(){  
  30.         String sql = "create table student (id int Primary key, name char (20),score double);";  
  31.         try {  
  32.             state.execute(sql);  
  33.       
  34.         } catch (SQLException e) {  
  35.             e.printStackTrace();  
  36.         }  
  37.     }  
  38.     public static void insert(int id,String name,double score)  
  39.     {  
  40.         String sql = "insert into student values("+id+",'"+name+"',"+score+");";  
  41.         try {  
  42.             state.execute(sql);  
  43.               
  44.         } catch (SQLException e) {  
  45.             // TODO Auto-generated catch block  
  46.             e.printStackTrace();  
  47.         }  
  48.     }  
  49.     public static double select(int id)  
  50.     {  
  51.         String sql  = "select score from student where id = "+id+";";  
  52.         double result= 0;  
  53.         try {  
  54.             ResultSet rs = state.executeQuery(sql);  
  55.             while(rs.next())  
  56.             {  
  57.                  result = rs.getDouble("score");  
  58.             }  
  59.               
  60.         } catch (SQLException e) {  
  61.             // TODO Auto-generated catch block  
  62.             e.printStackTrace();  
  63.         }  
  64.         return result;  
  65.     }  
  66.       
  67.     public static double select(String name)  
  68.     {  
  69.         String sql  = "select score from student where name = '"+name+"';";  
  70.         double result= 0;  
  71.         try {  
  72.             ResultSet rs = state.executeQuery(sql);  
  73.             while(rs.next())  
  74.             {  
  75.                  result = rs.getDouble("score");  
  76.             }  
  77.           
  78.         } catch (SQLException e) {  
  79.             // TODO Auto-generated catch block

抱歉!评论已关闭.