您的位置 首页 java

RMI远程调用

曾经做过一个简单的RMI远程调用实验,原理就不多说了,现把代码贴出来,也算是自己的学习笔记吧,点滴的积累才能让自己比昨天更进步一点,努力吧,程序员。

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;

}

}

文章来源:智云一二三科技

文章标题:RMI远程调用

文章地址:https://www.zhihuclub.com/195576.shtml

关于作者: 智云科技

热门文章

网站地图