Java RMI遠(yuǎn)程調(diào)用
這次是用java實(shí)現(xiàn)RMI 的遠(yuǎn)程調(diào)用:
編寫(xiě)的過(guò)程大致為:
- 首先我們的啟動(dòng)Mysqlserver ,然后再里面建立一個(gè)數(shù)據(jù)庫(kù),以便我們?cè)跀?shù)據(jù)庫(kù)中插入存放學(xué)生信息等。
- 先編寫(xiě)一個(gè)接口DataServer,并且繼承Remote類,然后再接口里面寫(xiě)上服務(wù)器端能實(shí)現(xiàn)的方法,然后再定義一個(gè)類DataServerImpl 繼承UnicastRemoteObject 在實(shí)現(xiàn)接口DataServer,并且一一實(shí)現(xiàn)它定義在借口里面得到方法,這里我們重新寫(xiě)了一個(gè)類DBManager,用來(lái)實(shí)現(xiàn)與數(shù)據(jù)庫(kù)的連接,包括插入數(shù)據(jù),根據(jù)學(xué)號(hào)、姓名等進(jìn)行查詢等操作,其源代碼見(jiàn)附件。
- 下面就是需要使用rmic命令進(jìn)行編譯DataServerImpl文件,并且產(chǎn)生兩個(gè)文件,產(chǎn)生這兩個(gè)文件后就可以編寫(xiě)服務(wù)器端的代碼了,主要是在主函數(shù)中生成一個(gè)DataServerImpl對(duì)象,然后在綁定一個(gè)端口在程序中,在綁定一個(gè)url地址,來(lái)綁定服務(wù)的對(duì)象,這樣服務(wù)器端的程序就寫(xiě)好了。
- 下面就是寫(xiě)客戶端的代碼了。
- 首先是通過(guò)Naming.lookup(url),(url)就是服務(wù)器端指定的url地址這樣就可以得到一個(gè)DataServer的對(duì)象,然后得到這樣一個(gè)對(duì)象后就可以調(diào)用它的方法了。這樣也就實(shí)現(xiàn)了調(diào)用遠(yuǎn)程服務(wù)器端的代碼了,所以說(shuō)這樣RMI就比本上寫(xiě)完了。
首先是編寫(xiě)遠(yuǎn)程的接口調(diào)用函數(shù):
- <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>
由于代碼中使用到了與數(shù)據(jù)庫(kù)的連接,所以寫(xiě)了一個(gè)數(shù)據(jù)庫(kù)的管理類,代碼如下:
- <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
- e.printStackTrace();
- }
- return result;
- }
- }
- </span>
下面就是編寫(xiě)實(shí)現(xiàn)類,要繼承UnicastRemoteObject,并且實(shí)現(xiàn)上面定義的接口。
- <span style="font-size:16px;">import java.rmi.RemoteException;
- import java.rmi.server.UnicastRemoteObject;
- public class DataServerImpl extends UnicastRemoteObject implements DataServer {
- static {
- DBManager.init();
- }
- public DataServerImpl() throws RemoteException {
- super();
- // TODO Auto-generated constructor stub
- }
- @Override
- public void CreateTable() throws RemoteException {
- DBManager.CreateTable();
- }
- @Override
- public void insert(int id, String name, double score)
- throws RemoteException {
- DBManager.insert(id, name, score);
- }
- @Override
- public double select(int id) throws RemoteException {
- // TODO Auto-generated method stub
- double score = DBManager.select(id);
- return score;
- }
- @Override
- public double select(String name) throws RemoteException {
- double score = DBManager.select(name);
- return score;
- }
- }
- </span>
這樣就基本上完成了,然后就是編寫(xiě)服務(wù)端的代碼:
- <span style="font-size:16px;">import java.net.MalformedURLException;
- import java.rmi.Naming;
- import java.rmi.RemoteException;
- import java.rmi.registry.LocateRegistry;
- public class RMIServer {
- public static void main(String[] args) {
- try {
- DataServerImpl dataServer = new DataServerImpl();
- LocateRegistry.createRegistry(1111); //這里,服務(wù)端口號(hào)可任意指定
- Naming.rebind("//localhost:1111/showScore", dataServer);
- System.out.println("服務(wù)已經(jīng)啟動(dòng)。。");
- } catch (RemoteException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- </span>
使用命令空間綁定服務(wù)的地址,以及服務(wù)的名稱,以便客戶端實(shí)現(xiàn)遠(yuǎn)程調(diào)用??蛻舳说拇a如下:
- <span style="font-size:16px;">import java.net.MalformedURLException;
- import java.rmi.Naming;
- import java.rmi.NotBoundException;
- import java.rmi.RemoteException;
- import java.util.Scanner;
- public class RMIClient {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- try {
- DataServer dataServer = (DataServer)Naming.lookup("//localhost:1111/showScore");
- System.out.println("首先創(chuàng)建一個(gè)數(shù)據(jù)表,student");
- dataServer.CreateTable();
- System.out.println("創(chuàng)建成功。。");
- boolean isrunning = true;
- while(isrunning)
- {
- System.out.println("請(qǐng)您按照數(shù)字選擇操作。\n1.插入數(shù)據(jù) 2.按學(xué)號(hào)查找 3.按姓名查找 0.退出");
- int select = sc.nextInt();
- if(select == 1)
- {
- System.out.println("請(qǐng)您依次輸入學(xué)生的學(xué)號(hào)、姓名、成績(jī):");
- int num = sc.nextInt();
- String name = sc.next();
- double score = sc.nextDouble();
- dataServer.insert(num, name, score);
- }
- else if(select == 2)
- {
- System.out.println("請(qǐng)您輸入學(xué)生的學(xué)號(hào):");
- int num = sc.nextInt();
- double score = dataServer.select(num);
- System.out.println("學(xué)號(hào): "+num +" 成績(jī)?yōu)椋?nbsp;"+ score);
- }
- else if(select == 3)
- {
- System.out.println("請(qǐng)您輸入學(xué)生的姓名:");
- String name = sc.next();
- double score = dataServer.select(name);
- System.out.println("姓名: "+name +" 成績(jī)?yōu)椋?nbsp;"+ score);
- }
- else if(select == 0)
- {
- isrunning = false;
- }
- else
- {
- System.out.println("輸入有誤,請(qǐng)您重新輸入!");
- }
- }
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (RemoteException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (NotBoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- </span>
這樣就基本上的完成了所有的工作。。。
原文鏈接:http://blog.csdn.net/wx_962464/article/details/7443231
【編輯推薦】