最近做了个RMI远程调用实验,现记录一下,实验要求如下:
(1) 两人一组,每人一台PC,一台客户端,一台服务端;
(2) 服务端要求:要求实现如下远程方法,实现对登录提交的用户名密码进行远程校验。
publicBooleanCheckLogin(String username, Stringpassword)
(3) 服务端代码要求连接Mysql数据库进行数据校验;
(4) 客户端要求:用RMI技术进行远程过程调用实现登录过程。根据远程返回值分别进入“登录成功”或“登录失败”提示信息或页面。
原理就不多说了,直接上源码,源码如下。(不在同一网段下的两台电脑就需要花点时间去ping通了)
1、服务端源码 RMIServer
public class RMIServer {
public static void main(String[] args){
try {
System.setProperty("java.rmi.server.hostname", "192.168.1.105");
//注册服务
LocateRegistry.createRegistry(1099);
LoginService loginService = new LoginServiceImpl();
Naming.bind("rmi://192.168.1.105:1099/rmiServer", loginService);
System.out.println(">>>>>> INFO:远程RMIServer绑定【192.168.1.105】成功!");
} catch (RemoteException e) {
System.out.println("创建远程对象发生异常!");
e.printStackTrace();
} catch (MalformedURLException e) {
System.out.println("重复绑定发生异常!");
e.printStackTrace();
} catch (AlreadyBoundException e) {
System.out.println("URL异常!");
e.printStackTrace();
}
}
}
2、客户端源码 RMIClient
public class RMIClient {
public static void main(String[] args) {
try {
LoginService login = (LoginService)Naming.lookup("rmi://192.168.1.105:1099/rmiServer");
boolean isLogin = login.checkLogin("admin", "123");
if(isLogin){
System.out.println("登陆成功!");
}else{
System.out.println("登陆失败!");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
}
}
}
3、远程接口LoginService
public interface LoginService extends Remote{
//声明服务器端必须提供的服务
public boolean checkLogin(String username,String password) throws RemoteException;
}
4、接口实现类LoginServiceImpl
public class LoginServiceImpl extends UnicastRemoteObject implements LoginService{
public LoginServiceImpl() throws RemoteException {
super();
}
public boolean checkLogin(String username, String password) throws RemoteException {
DBUtil db = new DBUtil("select * from user where username = ? and password = ? ");
ResultSet rs = null;
System.out.println(">>>>>>开始查询数据库<<<<<
try {
db.pst.setString(1, username);
db.pst.setString(2, password);
rs = db.pst.executeQuery();
if(rs.next()){
System.out.println("用户名"+rs.getString("username")+"密码"+rs.getString("password"));
return true;
}
System.out.println(">>>>>>用户不存在<<<<<
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
rs.close();
db.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
}
5、DBUtil
public class DBUtil {
private static final String url = "jdbc:mysql://localhost:3306/testrmi";
private static final String user = "root";
private static final String password = "123456";
private static final String name="com.mysql.jdbc.Driver";
public Connection conn = null;
public PreparedStatement pst = null;
public DBUtil(String sql){
try {
Class.forName(name);
conn = DriverManager.getConnection(url, user, password);//获取连接
pst = conn.prepareStatement(sql);//准备执行语句
} catch (Exception e) {
e.printStackTrace();
}
}
public void close(){
try {
this.conn.close();
this.pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
6、实体类User
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}