RMI远程服务调用
数据库:info.sql
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/* Navicat MySQL Data Transfer Source Server : yuanzhen Source Server Version : 50713 Source Host : 192.168.1.6:3306 Source Database : serialnumber Target Server Type : MYSQL Target Server Version : 50713 File Encoding : 65001 Date: 2016-08-16 09:38:17 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for info -- ---------------------------- DROP TABLE IF EXISTS `info`; CREATE TABLE `info` ( `name` varchar(255) DEFAULT NULL, `email` varchar(255) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of info -- ---------------------------- INSERT INTO `info` VALUES ('Tom', '4564@foxmail.com'); INSERT INTO `info` VALUES ('John', 'You@163.com'); INSERT INTO `info` VALUES ('Harry', 'harry@qq.com');
Bank.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.rmi.*; import java.util.*; interface Bank extends Remote{ public List<Customer> getCustomers()throws RemoteException; }
BankImpl.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.rmi.*; import java.rmi.server.*; import java.sql.*; import java.util.*; class BankImpl extends UnicastRemoteObject implements Bank{ BankImpl()throws RemoteException{} public static void main (String[] args)throws RemoteException{ List<Customer> list=new BankImpl().getCustomers(); for(Customer customer:list){ System.out.println(customer); } return; } public List<Customer> getCustomers(){ List<Customer> list=new ArrayList<Customer>(); try{ Class.forName("org.gjt.mm.mysql.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://192.168.1.6:3306/serialnumber","root","123"); PreparedStatement ps=con.prepareStatement("select * from info"); ResultSet rs=ps.executeQuery(); while(rs.next()){ Customer c=new Customer(); c.setName(rs.getString(1)); c.setEmail(rs.getString(2)); list.add(c); } con.close(); }catch(Exception e){System.out.println(e);} return list; }//end of getCustomers() }
Customer.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Customer implements java.io.Serializable{ private String name; private String email; public void setName(String name){ this.name=name; } public void setEmail(String email){ this.email=email; } public String getName(){ return name; } public String getEmail(){ return email; } public String toString(){ return name+" "+email; } }
MyServer.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.rmi.*; public class MyServer{ public static void main(String args[])throws Exception{ Remote r=new BankImpl(); Naming.rebind("rmi://localhost:6666/mine",r); } }
编译服务器端源代码
注册:注册的过程中会生成BankImpl_Stub.class(用于拷贝至客户端);
启动服务器:
客户端代码:
Customer.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Customer implements java.io.Serializable{ private String name; private String email; public void setName(String name){ this.name=name; } public void setEmail(String email){ this.email=email; } public String getName(){ return name; } public String getEmail(){ return email; } public String toString(){ return name+" "+email; } }
Bank.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.rmi.*; import java.util.*; interface Bank extends Remote{ public List<Customer> getCustomers()throws RemoteException; }
MyCLient.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.util.*; import java.rmi.*; public class MyClient{ public static void main(String args[])throws Exception{ Bank b=(Bank)Naming.lookup("rmi://localhost:6666/mine"); List<Customer> list=b.getCustomers(); for(Customer c:list){ System.out.println(c); } System.out.println("done"); } }
编译:
把服务器端的BankImpl_Stub.class拷贝过来
启动客户端
相关链接: