1)基于CORBA技术开发一个考试成绩查询系统,主要包括:服务器部分:实现查询和录入服务;客户端部分包括录入和查询部分
2)服务端使用Java编写,使用JDBC访问数据库;客户端使用Java和C++分别编写。
实习前确保java/bin以及omniORB-4.1.0\bin\x86_win32已经配置在环境变量path
创建自己的文件夹,编写DataService.idl,定义查询与录入函数。
DataService.idl的代码如下:
interface DataService
{ void insert(in string stuName,in string StuNo,in float score);
float search(in string stuNo);
};
1)用以下命令生成存根与框架:
idlj -fall DataService.idl
生成如下文件:
2) 在自己创建的文件夹下,创建server文件夹,并将1)得到的存根和框架java文件拷贝至server,并使用eclipse创建一工程,将这些文件包含进去。
3)编写服务端服务实现,并写main函数以启动corba服务
编写的DataServiceServer.java的代码如下:
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import org.omg.PortableServer.*;
import jdbc.DBmanager;
//编写相对应的服务,一定要从 _类名ImplBase继承,并实现相应的方法
class DataServiceImpl extends DataServicePOA // 具体的服务实现
{
private ORB orb;
public void setOrb(ORB orb_val)
{
this.orb = orb_val;
}
@Override
public void insert(String stuName, String StuNo, float score) {
// TODO Auto-generated method stub
DBmanager.insert(stuName, StuNo, score);
}
@Override
public float search(String stuNo) {
// TODO Auto-generated method stub
return DBmanager.search(stuNo);
}
}
public class DataServiceserver// 起动服务的程序
{
public static void main(String args[]) {
try{
System.out.println("创建和初始化 ORB ");
ORB orb = ORB.init(args, null);
POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootpoa.the_POAManager().activate();
System.out.println("创建服务对象并将其向 ORB 注册 ");
DataServiceImpl dataServiceImpl = new DataServiceImpl();
dataServiceImpl.setOrb(orb);
//System.out.println(orb.object_to_string(sysProImpl));
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
NameComponent[] path = {new NameComponent("DataService", "")};
org.omg.CORBA.Object ref = rootpoa.servant_to_reference(dataServiceImpl);
DataService href = DataServiceHelper.narrow(ref);
System.out.println(orb.object_to_string(href));
System.out.println(ncRef.getClass().toString());
ncRef.rebind(path, href);
System.out.println("DataServiceServer ready and waiting ...");
orb.run();
} catch (Exception e) {
System.err.println("Error: " + e);
e.printStackTrace(System.out);
}
}
}
在工程中创建了dbmanager类,用来连接mysql数据库,dbmanager.java的代码如下:
package jdbc;
import java.sql.*;
public class DBmanager {
private static String password = "123";
private static Connection conn;
private static Statement sta;
private static ResultSet result;
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/student";
private static float score;
public static Connection getcon() {
Connection con = null;
try {
Class.forName(driver);// 注册驱动
} catch (ClassNotFoundException e) {
System.out.println("未完成注册驱动");
e.printStackTrace();
}
try {
con = DriverManager
.getConnection(url, "root", password);// 建立连接
} catch (SQLException e) {
// TODO: handle exception
System.out.println("未完成数据连接");
e.printStackTrace();
}
return con;
}
public static void insert(String stuName, String StuNo, float score) {
try {
conn = getcon();
String sql = "insert into mytable(stuName,stuNo,score) values(?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.clearBatch();
ps.setString(1, stuName);
ps.setString(2, StuNo);
ps.setFloat(3, score);
ps.addBatch();
ps.executeBatch();
ps.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static float search(String StuNo) {
try {
conn = getcon();
String sql = "select * from mytable where StuNo=" + StuNo;
sta = conn.createStatement();
result = sta.executeQuery(sql);
if (result.next()) {
String stuName = result.getString(1);
String stuNo = result.getString(2);
score = result.getFloat(3);
}
sta.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return score;
}
}
1)在自己创建的文件夹下,创建java_client文件夹,并将B的1)得到的存根和框架java文件拷贝至java,并使用eclipse创建一工程,将这些文件包含进去。
2) 编写客户端对查询与录入的调用
DataService.java的代码如下:
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
public class DataServiceClient {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
String SetInfo, ReturnInfo, ref;
float score=0;
org.omg.CORBA.Object objRef;
DataService dataserviceRef;
ORB orb = ORB.init(args, null);
/*if (args.length >= 1)
{
ref = args[0];
//System.out.println(ref);
}
else
{
System.out.println("aaaaaaaaaaaaaaaaaa");
return ;
}*/
//下面一条语句得到的是一个NamingContext对象,并非SysProp对象
//objRef = orb.string_to_object(ref);
//dataserviceRef = DataServiceHelper.narrow(objRef);
objRef = orb.resolve_initial_references("NameService");
System.out.println(orb.object_to_string(objRef));
NamingContext ncRef = NamingContextHelper.narrow(objRef);
NameComponent nc =new NameComponent("DataService","");
NameComponent path[] = {nc};
dataserviceRef = DataServiceHelper.narrow(ncRef.resolve(path));
/*if (args.length>1)
{
SetInfo=args[1];
}
else
{
SetInfo="0";
}
System.out.println("开始调用");*/
System.out.println("运行成功!");
dataserviceRef.insert("lc", "200901294", 90);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
打开mySQL
查看存在的数据库:
创建student数据库
查看数据库
进入数据库
查看数据库中的表
创建mytable表格
查看表格
查看表格描述
查看表中数据
启动java_server,需要先启动命名服务器,然后再启动具体服务
首先:开始--运行,输入<java_home>/bin/tnameserv
cd java_server/bin
java DataServiceserver
cd java_client\bin
java DataServiceClient
执行后可看到插入的数据:
即在student数据库中完成了数据的插入;