我正在使用Java设置新的客户端-服务器应用程序,并且必须使用RMI创建服务器。范围太大,我需要数据库。我正在使用基本方法来创建服务器。
这是我的远程接口类
package Connectivity;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.sql.Connection;
public interface ConnectInterface extends Remote{
String welcomeBanner() throws RemoteException;
Connection getDBConnection() throws RemoteException;
//Just a comment
}
实施类
package Connectivity;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ServerConnection extends UnicastRemoteObject implements ConnectInterface{
private static ServerConnection SCInstance;
private static Connection connection;
String welcomeBannerText;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/xam_test1";
String username = "root";
String password = "";
protected ServerConnection() throws RemoteException {
super();
// TODO Auto-generated constructor stub
}
public static ServerConnection getInstance() throws RemoteException {
if(SCInstance == null) {
SCInstance = new ServerConnection();
}
return SCInstance;
}
public Connection getDBConnection() {
try {
if (connection == null || connection.isClosed()) {
Class.forName(driver);
connection = DriverManager.getConnection(url, username, password);
}
} catch (ClassNotFoundException | SQLException e) {
System.out.println("Error in ServerConnection: " + e);
}
return connection;
}
@Override
public String welcomeBanner() throws RemoteException {
// TODO Auto-generated method stub
return welcomeBannerText;
}
}
服务器程序
package Connectivity;
import java.sql.Statement;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.sql.Connection;
import java.sql.ResultSet;
public class StartServer {
public static void main(String[] args) {
try {
int PORT = 1318;
Registry registry = LocateRegistry.createRegistry(PORT);
LocateRegistry.getRegistry();
ServerConnection stub = new ServerConnection();
stub.welcomeBannerText = "Welcome to X-AM";
System.out.println("Server is starting");
registry.rebind("jdbc:mysql://localhost:3306/xam_test1", stub);
System.out.println("Server is ready");
} catch (Exception e) {
System.out.println("Error: " + e.toString());
}
}
}
客户端程序(这在另一个项目中,但是具有相同的包名称)
package Connectivity;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class ClientX {
ConnectInterface temp;
public ClientX() {
try {
System.out.println("Searching for Server");
String url = "jdbc:mysql://localhost/127.0.0.1:3306/xam_test1";
Registry registry = LocateRegistry.getRegistry(1318);
System.out.println("Registry located");
this.temp = (ConnectInterface) registry.lookup(url);
System.out.println("Server found");
this.temp.getDBConnection();
//this.welcomeBanner();
} catch (Exception e) {
System.out.println("Client Exception: " + e.toString());
}
}
public String welcomeBanner() throws RemoteException {
return temp.welcomeBanner();
}
public static void main(String[] args) throws RemoteException {
ClientX clientX = new ClientX();
System.out.println(clientX.welcomeBanner());
}
}
服务器正在运行。我也对查询做了一些实验,它们给了我输出。但是当我运行客户端程序时,输出是这样的
Searching for Server
Registry located
Client Exception: java.rmi.NotBoundException: jdbc:mysql://localhost/127.0.0.1:3306/xam_test1
我想在整个会话期间保持客户端与服务器之间的不间断连接。请说明如何解决此问题。谢谢