问题:
RMI+JDBC远端数据库的访问。实现简单的成绩查询系统(创建表,录入成绩,查询成绩等)。在服务器端,通过JDBC访问数据库。客户端调用服务端提供的各种数据库操作。
环境准备:
(1).确保JDK已经安装,并将其bin路径配置到环境变量path中
(2).创建MySQL数据库,数据库名为rmidb
步骤:
(1)使用mysql-connector-java-5[1].0.8.zip驱动包,编写DBManager类,访问mysql数据库,实现创建表,录入成绩,查询成绩等操作。
package com.rmi; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; import com.rmi.entity.Student; //编写DBmanager类,访问mysql数据库,实现创建表,录入成绩,查询成绩等操作。
public class DBManager {
private final static String userName = "root";
private final static String passWord = "root";
private final static String dburl = "jdbc:mysql://localhost:3306/rmiDB?useUnicode=true&characterEncoding=utf8";
private final static String driverClassName = "com.mysql.jdbc.Driver";
private static Connection conn = null;
private static ResultSet rs = null;
private static Statement stmt = null; public static Connection getConn() {
try {
Class.forName(driverClassName);
conn = DriverManager.getConnection(dburl, userName, passWord);
} catch (ClassNotFoundException e) {
System.out.println("没有找到数据库驱动程序");
} catch (SQLException e) {
System.out.println("数据库连接时出现异常,可能由于数据库服务未启动造成,请先启动数据库服务");
}
return conn;
} public static void createTable() {
String sql = "create table stu(stuNo varchar(10),stuName varchar(10),grade float);";
conn = getConn();
try {
stmt = conn.createStatement();
stmt.execute(sql);
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
} public static void insert(Student stu) {
String sql = "insert into stu values('" + stu.getStuNo() + "','" + stu.getStuName() + "'," + stu.getGrade()
+ ")";
conn = getConn();
try {
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
} public static Student getStu(String stuNo) {
String sql = "select * from stu where stuNo = " + stuNo;
conn = getConn();
Student stu = new Student();
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
stu.setStuNo(rs.getString(1));
stu.setStuName(rs.getString(2));
stu.setGrade(rs.getFloat(3));
}
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return stu;
} public static void main(String[] args) {
} }
(2)定义服务接口DBServer,参考实例DBServer.java。提供创建表,录入成绩,查询成绩等操作(尽量与DBmanager类中的操作定义成同样的形式)。
package com.rmi; import java.rmi.Remote;
import java.rmi.RemoteException; import com.rmi.entity.Student; //提供创建表,录入成绩,查询成绩等操作(尽量与DBmanager类中的操作定义成同样的形式)。
public interface DBServer extends Remote {
public void createTable() throws RemoteException; public Student getStu(String stuNo) throws RemoteException; public void insert(Student stu) throws RemoteException;
}
(3)实现服务,请参考RMI实例DBServerImpl。对实现创建表,录入成绩,查询成绩等操作。实现过程中可直接使用DBmanager类。
package com.rmi; import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject; import com.rmi.entity.Student; public class DBServerImpl extends UnicastRemoteObject implements DBServer { public DBServerImpl() throws RemoteException {
super();
} @Override
public void createTable() throws RemoteException {
DBManager.createTable();
} @Override
public Student getStu(String stuNo) throws RemoteException {
return DBManager.getStu(stuNo);
} @Override
public void insert(Student stu) throws RemoteException {
DBManager.insert(stu);
}
}
(4)编写部署类DeployServer,请参考RMI实例DeployServer.java。
package com.rmi; import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry; //部署程序
public class DeployServer {
public DeployServer() {
} public static void main(String[] args) {
try {
DBServer ds=new DBServerImpl();
//LocateRegistry 用于获取特定主机(包括本地主机)上的远程对象注册表的引用,或用于创建一个接受对特定端口调用的远程对象注册表。
LocateRegistry.createRegistry(1111);// 创建并导出接受指定 port 请求的本地主机上的 Registry 实例。
//Naming 可以将某个名称作为一个参数,名称=url格式的字符串
Naming.rebind("//localhost:1111/ds", ds);// 将指定名称重新绑定到一个新的远程对象。
System.out.println("RMI服务器正在启动...");
} catch (RemoteException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} } }
(5)使用DataService接口,实现客户端,请参考RMI实例RmiHelloClient.java。编写bat文件启动服务器和客户端,进行测试。
package com.client; import java.rmi.Naming; import com.rmi.DBServer;
import com.rmi.entity.Student; public class RmiHelloClient {
public static void main(String[] args) {
try {
DBServer ds = (DBServer) Naming.lookup("//127.0.0.1:1111/ds");
ds.createTable();
Student stu = new Student();
stu.setStuNo("2014012771");
stu.setStuName("海哥哥");
stu.setGrade(99.9f);
ds.insert(stu);
Student stu2 = ds.getStu("2014012771");
System.out.println("学号: " + stu2.getStuNo());
System.out.println("姓名: " + stu2.getStuName());
System.out.println("成绩: " + stu2.getGrade());
} catch (Exception e) {
e.printStackTrace();
}
}
}