这次是用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就比本上写完了。
首先是编写远程的接口调用函数:
- <span style="font-size:16px;">import java.rmi.Remote;
- import java.rmi.RemoteException;
- public interface DataServer extends Remote {
- public void CreateTable() throws RemoteException;
- public void insert(int id ,String name,double Score) throws RemoteException;
- public double select(int id)throws RemoteException;
- public double select (String name)throws RemoteException;
- }
- </span>
由于代码中使用到了与数据库的连接,所以写了一个数据库的管理类,代码如下:
- <span style="font-size:16px;">import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class DBManager {
- private static String user = "root";
- private static String pass = "123456";
- private static String className ="com.mysql.jdbc.Driver";
- private static String url = "jdbc:mysql://localhost:3306/students";
- private static Connection conn;
- private static java.sql.Statement state;
- public static void init()
- {
- try {
- Class.forName(className);
- conn = DriverManager.getConnection(url,user,pass);
- state =conn.createStatement();
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void CreateTable(){
- String sql = "create table student (id int Primary key, name char (20),score double);";
- try {
- state.execute(sql);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void insert(int id,String name,double score)
- {
- String sql = "insert into student values("+id+",'"+name+"',"+score+");";
- try {
- state.execute(sql);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static double select(int id)
- {
- String sql = "select score from student where id = "+id+";";
- double result= 0;
- try {
- ResultSet rs = state.executeQuery(sql);
- while(rs.next())
- {
- result = rs.getDouble("score");
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return result;
- }
- public static double select(String name)
- {
- String sql = "select score from student where name = '"+name+"';";
- double result= 0;
- try {
- ResultSet rs = state.executeQuery(sql);
- while(rs.next())
- {
- result = rs.getDouble("score");
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block