- Java RMI 有用資源
- Java RMI - 快速指南
- Java RMI - 有用資源
- Java RMI - 討論
Java RMI - 資料庫應用程式
在上一章中,我們建立了一個示例 RMI 應用程式,其中客戶端呼叫一個顯示 GUI 視窗(JavaFX)的方法。
在本章中,我們將透過一個示例來了解客戶端程式如何檢索駐留在伺服器上的 MySQL 資料庫中表的記錄。
假設我們在資料庫details中有一個名為student_data的表,如下所示。
+----+--------+--------+------------+---------------------+ | ID | NAME | BRANCH | PERCENTAGE | EMAIL | +----+--------+--------+------------+---------------------+ | 1 | Ram | IT | 85 | ram123@gmail.com | | 2 | Rahim | EEE | 95 | rahim123@gmail.com | | 3 | Robert | ECE | 90 | robert123@gmail.com | +----+--------+--------+------------+---------------------+
假設使用者名稱為myuser,密碼為password。
建立學生類
建立一個Student類,其中包含setter和getter方法,如下所示。
public class Student implements java.io.Serializable {
private int id, percent;
private String name, branch, email;
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getBranch() {
return branch;
}
public int getPercent() {
return percent;
}
public String getEmail() {
return email;
}
public void setID(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setBranch(String branch) {
this.branch = branch;
}
public void setPercent(int percent) {
this.percent = percent;
}
public void setEmail(String email) {
this.email = email;
}
}
定義遠端介面
定義遠端介面。在這裡,我們定義了一個名為Hello的遠端介面,其中包含一個名為getStudents()的方法。此方法返回一個包含Student類物件的列表。
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.*;
// Creating Remote interface for our application
public interface Hello extends Remote {
public List<Student> getStudents() throws Exception;
}
開發實現類
建立一個類並實現上面建立的介面。
在這裡,我們實現了遠端介面的getStudents()方法。當您呼叫此方法時,它將檢索名為student_data表的記錄。使用其setter方法將這些值設定到Student類,將其新增到列表物件中並返回該列表。
import java.sql.*;
import java.util.*;
// Implementing the remote interface
public class ImplExample implements Hello {
// Implementing the interface method
public List<Student> getStudents() throws Exception {
List<Student> list = new ArrayList<Student>();
// JDBC driver name and database URL
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://:3306/details";
// Database credentials
String USER = "myuser";
String PASS = "password";
Connection conn = null;
Statement stmt = null;
//Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
//Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql = "SELECT * FROM student_data";
ResultSet rs = stmt.executeQuery(sql);
//Extract data from result set
while(rs.next()) {
// Retrieve by column name
int id = rs.getInt("id");
String name = rs.getString("name");
String branch = rs.getString("branch");
int percent = rs.getInt("percentage");
String email = rs.getString("email");
// Setting the values
Student student = new Student();
student.setID(id);
student.setName(name);
student.setBranch(branch);
student.setPercent(percent);
student.setEmail(email);
list.add(student);
}
rs.close();
return list;
}
}
伺服器程式
RMI 伺服器程式應實現遠端介面或擴充套件實現類。在這裡,我們應該建立一個遠端物件並將其繫結到RMI 登錄檔。
以下是此應用程式的伺服器程式。在這裡,我們將擴充套件上面建立的類,建立一個遠端物件並將其註冊到 RMI 登錄檔中,繫結名稱為hello。
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server extends ImplExample {
public Server() {}
public static void main(String args[]) {
try {
// Instantiating the implementation class
ImplExample obj = new ImplExample();
// Exporting the object of implementation class (
here we are exporting the remote object to the stub)
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
// Binding the remote object (stub) in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
客戶端程式
以下是此應用程式的客戶端程式。在這裡,我們正在獲取遠端物件並呼叫名為getStudents()的方法。它從列表物件中檢索表的記錄並顯示它們。
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.*;
public class Client {
private Client() {}
public static void main(String[] args)throws Exception {
try {
// Getting the registry
Registry registry = LocateRegistry.getRegistry(null);
// Looking up the registry for the remote object
Hello stub = (Hello) registry.lookup("Hello");
// Calling the remote method using the obtained object
List<Student> list = (List)stub.getStudents();
for (Student s:list)v {
// System.out.println("bc "+s.getBranch());
System.out.println("ID: " + s.getId());
System.out.println("name: " + s.getName());
System.out.println("branch: " + s.getBranch());
System.out.println("percent: " + s.getPercent());
System.out.println("email: " + s.getEmail());
}
// System.out.println(list);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
執行示例的步驟
以下是執行我們的 RMI 示例的步驟。
步驟 1 - 開啟您已儲存所有程式的資料夾,並編譯所有 Java 檔案,如下所示。
Javac *.java
步驟 2 - 使用以下命令啟動rmi登錄檔。
start rmiregistry
這將在單獨的視窗中啟動一個rmi登錄檔,如下所示。
步驟 3 - 執行伺服器類檔案,如下所示。
Java Server
步驟 4 - 執行客戶端類檔案,如下所示。
java Client