我正在使用MySQL数据库在RMI中开展一个小项目。 当我尝试连接到数据库时,一切正常,但是当我将RMI服务器的代码和jdbc的代码组合起来时,它不起作用。
这是我的数据库连接的代码 BaseDonnees.java :
package Serveur ;
import java.sql.*;
public class BaseDonnees {
private Connection conn ;
private Statement stat ;
public BaseDonnees() throws InstantiationException, IllegalAccessException {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance() ;
} catch (ClassNotFoundException ex) {
System.out.println("Pas de Driver MySQL") ;
}
}
public void OpenConnection() throws SQLException {
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/distributeur", "root", "") ;
} catch (SQLException ex) {
System.out.println("Erreur de connexion à la base de données");
}
if(conn == null) {
System.out.println("Nop :-(") ;
}
else {
System.out.println("Ok :-)") ;
}
stat = conn.createStatement();
}
public void Update(String req) throws SQLException {
stat.executeUpdate(req);
}
public ResultSet Select(String req) throws SQLException {
ResultSet rs = null;
rs = stat.executeQuery(req) ;
return rs ;
}
public void CloseConnection() throws Exception {
conn.close();
}
}
这是接口实现的代码,它包含所有远程方法(只有构造函数,没有Abstract方法的实现) DistributeurImplement.java :
package Serveur ;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DistributeurImplement extends UnicastRemoteObject implements DistributeurInterface {
private double solde ;
private boolean statut ;
private String login ;
private String password ;
private ResultSet resultat ;
private BaseDonnees bd ;
public DistributeurImplement() throws RemoteException, InstantiationException, SQLException, IllegalAccessException {
this.login = null ;
this.password = null ;
this.solde = 0 ;
this.statut = false ;
this.resultat = null ;
bd = new BaseDonnees() ;
bd.OpenConnection() ;
}
}
最后主要的代码(服务器类) DistributeurServeur.java :
package Serveur ;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
public class DistributeurServeur {
public static void main(String[] args) throws RemoteException {
//System.setSecurityManager (new RMISecurityManager()) ;
try {
LocateRegistry.createRegistry(1099); //just added
Naming.rebind ("objet_distributeur", new DistributeurImplement()) ;
System.out.println (" Serveur prêt.") ;
}
catch (Exception e) {
System.out.println(" Erreur serveur : " + e) ;
}
}
}
当我运行主要时,我收到了这条消息:
Erreur deconnexionàlabasededonnées com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败
成功发送到服务器的最后一个数据包是0毫秒前。驱动程序未收到来自服务器的任何数据包。
和
Nop: - ( 在Serveur.DistributeurServeur.main(DistributeurServeur.java:12)
Erreur serveur :java.lang.NullPointerException 引起:java.net.SocketException:java.security.AccessControlException:access denied(java.net.SocketPermission [0:0:0:0:0:0:0:1]:3306 connect,resolve)
和
显示java.lang.NullPointerException 在Serveur.BaseDonnees.OpenConnection(BaseDonnees.java:33) 在Serveur.DistributeurImplement。(DistributeurImplement.java:35) 在Serveur.DistributeurServeur.main(DistributeurServeur.java:12)
(由数据库类中的两个例外生成)
我不知道为什么我在没有RMI的情况下尝试连接时会收到这些消息及其工作的一切。所有它现在有效,我删除了以下行:
System.setSecurityManager (new RMISecurityManager()) ;
因为如果我让它,我将需要在项目中包含java.policy。 我添加了这一行:
LocateRegistry.createRegistry(1099);
之前的行
Naming.rebind ("objet_distributeur", new DistributeurImplement()) ;
因为在缺席时我需要在编译中创建rmiregistry(使用rmiregistry命令)。