1.创建一个学生实体类Student.java:
package com.RMIdemo.entity; @SuppressWarnings("serial")
public class Student implements java.io.Serializable{
private int id;
private String name; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(int id, String name) {
super();
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
public Student() {
super();
} }
2.创建一个远程接口StudentDao.java,由于是远程接口所以需要继承 Remote:
package com.RMIdemo.dao; import java.rmi.Remote;
import java.rmi.RemoteException; //此为远程对象调用的接口,必须继承Remote类
public interface StudentDao extends Remote{ //获取所有学生信息
public String getStudentList()throws RemoteException;
}
3.实现这个接口StudentDaoImpl.java,这个实现类需要继承UnicastRemoteObject:
package com.RMIdemo.daoImpl; import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import com.RMIdemo.dao.DBConnUtil;
import com.RMIdemo.dao.StudentDao;
import com.RMIdemo.entity.Student; @SuppressWarnings("serial")
public class StudentDaoImpl extends UnicastRemoteObject implements StudentDao { public StudentDaoImpl() throws RemoteException {
super();
} public String getStudentList() throws RemoteException { List<Student> list = new ArrayList<Student>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from student";
conn = DBConnUtil.getConn();
int i = 1;
try {
ps = conn.prepareStatement(sql); rs = ps.executeQuery();
while (rs.next()) {
Student stu = new Student();
stu.setId(rs.getInt("id"));
stu.setName(rs.getString("name"));
list.add(stu);
}
} catch (SQLException e) { e.printStackTrace();
} finally {
DBConnUtil.closeAll(rs, ps, conn);
} net.sf.json.JSONArray json = net.sf.json.JSONArray.fromObject(list); return json.toString(); } }
4.连接数据库时用的是JDBC所以需要建立DBConnUtil.java这个连接数据库的工具类:
package com.RMIdemo.daoImpl; import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import com.RMIdemo.dao.DBConnUtil;
import com.RMIdemo.dao.StudentDao;
import com.RMIdemo.entity.Student; @SuppressWarnings("serial")
public class StudentDaoImpl extends UnicastRemoteObject implements StudentDao { public StudentDaoImpl() throws RemoteException {
super();
} public String getStudentList() throws RemoteException { List<Student> list = new ArrayList<Student>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from student";
conn = DBConnUtil.getConn();
int i = 1;
try {
ps = conn.prepareStatement(sql); rs = ps.executeQuery();
while (rs.next()) {
Student stu = new Student();
stu.setId(rs.getInt("id"));
stu.setName(rs.getString("name"));
list.add(stu);
}
} catch (SQLException e) { e.printStackTrace();
} finally {
DBConnUtil.closeAll(rs, ps, conn);
} net.sf.json.JSONArray json = net.sf.json.JSONArray.fromObject(list); return json.toString(); } }
5.这是连接数据库的配置文件database.properties
6.创建服务端StudenServer.java:
package com.RMIdemo.rmi; import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import com.RMIdemo.dao.StudentDao;
import com.RMIdemo.daoImpl.StudentDaoImpl; public class StudentServer {
/**
* 启动 RMI 注册服务并进行对象注册
*/
public static void main(String[] argv) {
try {
// 启动RMI注册服务,指定端口为1099 (1099为默认端口)
// 也可以通过命令 $java_home/bin/rmiregistry 1099启动
// 这里用这种方式避免了再打开一个DOS窗口
// 而且用命令rmiregistry启动注册服务还必须事先用RMIC生成一个stub类为它所用
LocateRegistry.createRegistry(1099); // 创建远程对象的一个或多个实例,下面是stu对象
// 可以用不同名字注册不同的实例 // 把hello注册到RMI注册服务器上,命名为stu;注意:此处如果用的是Spring框架的话我们需要先加载Spring的配置文件
StudentDao stu=new StudentDaoImpl();
Naming.rebind("stu", stu); // 如果要把stu实例注册到另一台启动了RMI注册服务的机器上
// Naming.rebind("//机器IP地址:1099/stu",stu);
System.out.println("服务器已启动!");
} catch (Exception e) {
System.out.println("服务器启动失败: " + e);
}
}
}
7.建立客户端StudentClient.java:
package com.RMIdemo.rmi;
import java.rmi.Naming; import javax.servlet.http.HttpServletRequest; import com.RMIdemo.dao.StudentDao;
import com.RMIdemo.util.jsonpUtil; public class StudentClient
{
/**
* 查找远程对象并调用远程方法
*/
public static void main(String[] argv)
{
try
{
StudentDao stu = (StudentDao) Naming.lookup("stu"); //如果要从另一台启动了RMI注册服务的机器上查找hello实例
//StudentDao stu = (StudentDao) Naming.lookup("//192.168.1.21:1099/stu"); //调用远程方法 JDBC连接数据库方式
jsonpUtil jsonp=new jsonpUtil();
String callBack=jsonp.returnJsonp2(stu.getStudentList());
System.out.println(callBack);
}
catch (Exception e)
{
System.out.println("服务器异常: " + e);
}
}
}
下面是我们先运行服务端代码:
这个时候就证明服务端成功启动!
接着运行客户端代码:
此时如果我们看到控制台有服务器返回的数据就代表我们大功告成了!