需求背景
公司的PLM系统后台使用了PostgreSQL数据库,需要将PLM系统的项目相关信息同步到ERP系统。同时对方(PLM系统开发)表示没有现成接口可以提供,可以开放数据库的JDBC连接信息。由于在网上没有找到ORACLE ERP通过java jdbc连接PostgreSQL数据库的例子,于是整理了这篇文章与君分享。
开发工具和环境
开发工具:Jdeveloper 10.1.3.5.0
EBS系统版本:12.2.8
RDBMS: 12.1.0.2.0
jdbc jar包:postgresql-42.2.27.jre6.jar
开发环境说明
1、Jdeveloper如何导入jar包
在工具栏 File下,选择Import
选择EJB JAR (.jar) File
选择要导入的jar包,这里选择本地的postgresql-42.2.27.jre6.jar
选择完成后,在左侧项目文件列表栏可以看到导入的jar包
2、代码开发说明
项目结构如下图:
2.1、创建jdbc连接信息相关的properties文件
2.2、创建获取properties配置信息的java类
public class DbProperties {
public static String sourceUrl;
public static String sourceUser;
public static String sourcePassword;
static {
Properties properties = new Properties();
InputStream in =
DbProperties.class.getClassLoader().getResourceAsStream("cux/oracle/apps/cux/program/jdbc_conn.properties");
try {
properties.load(in);
sourceUrl = properties.getProperty("source_url");
sourceUser = properties.getProperty("source_user");
sourcePassword = properties.getProperty("source_password");
} catch (IOException e) {
e.printStackTrace();
}
}
public DbProperties() {
}
}
2.3、获取PLM系统数据并导入ERP数据表
public class DrawThirdPartyDBData implements JavaConcurrentProgram {
public DrawThirdPartyDBData() {
}
public void syncData(CpContext cpContext) throws SQLException {
// 记录同步记录的数量
int count = 0;
// 获取PLM JDBC URL
String plm_url = DbProperties.sourceUrl;
// 获取PLM数据库用户名
String plm_user = DbProperties.sourceUser;
// 获取PLM数据库密码
String plm_password = DbProperties.sourcePassword;
Driver driver = new org.postgresql.Driver();
// 连接PLM数据库,返回连接对象
Connection plmConn =
DriverManager.getConnection(plm_url, plm_user, plm_password);
// 连接ERP数据库,返回连接对象
Connection erpConn = cpContext.getJDBCConnection();
//调用ERP过程,前3个是入参,最后2个是出参
String erpSyncSql =
"{call cux_xxx_erp_data_sync_pkg.load_xxx_data(?, ?, ?, ?, ?, ?, ?)}";
CallableStatement call = erpConn.prepareCall(erpSyncSql);
//PLM数据查询
String plmQuerySql = "select no, name, belong, state, pstate from proj where 1 = 1 and del = 0 and wkaid <> '3'";
PreparedStatement statement = plmConn.prepareStatement(plmQuerySql);
ResultSet resultSet = statement.executeQuery();
//遍历PLM数据查询结果
while (resultSet.next()) {
String no = resultSet.getString(1);
String name = resultSet.getString(2);
String belong = resultSet.getString(3);
String state = resultSet.getString(4);
String pstate = resultSet.getString(5);
//打印参数
//cpContext.getOutFile().writeln(col1 + "," + col2 + "," + col3);
call.setString(1, no);
call.setString(2, name);
call.setString(3, belong);
call.setString(4, state);
call.setString(5, pstate);
call.registerOutParameter(6, OracleTypes.VARCHAR);
call.registerOutParameter(7, OracleTypes.VARCHAR);
//执行ERP存储过程
call.execute();
//处理执行结果
String outParamStatus = call.getString(6);
String outParamMsg = call.getString(7);
if (!"S".equals(outParamStatus)) {
cpContext.getLogFile().writeln("plm project sync erp failed(" +
no + "," + name + "," + belong + "," + state +
"), cause:" + outParamMsg,
LogFile.STATEMENT);
}
count++;
}
// 自动关闭,不用手工关闭ERP连接
/*
if (call != null) {
call.close();
}
if (erpConn != null) {
erpConn.close();
}*/
// 关闭PLM连接
if (statement != null) {
statement.close();
}
if (plmConn != null) {
plmConn.close();
}
cpContext.getOutFile().writeln("数据同步ERP成功,共传送记录:" + count);
}
public void runProgram(CpContext cpContext) {
ParameterList paramList = cpContext.getParameterList();
ReqCompletion reqCompletion = cpContext.getReqCompletion();
try {
//获取请求参数
while (paramList.hasMoreElements()) {
NameValueType nameVT = paramList.nextParameter();
//打印参数
//cpContext.getOutFile().writeln(nameVT.getName() + ":" + nameVT.getValue());
cpContext.getLogFile().writeln(nameVT.getName() + ":" +
nameVT.getValue(),
LogFile.STATEMENT);
}
//同步PLM数据到ERP
syncData(cpContext);
reqCompletion.setCompletion(ReqCompletion.NORMAL,
"Program Completed Normal!");
} catch (Exception e) {
//设置程序的执行结果
reqCompletion.setCompletion(ReqCompletion.WARNING,
"Exception Error!");
cpContext.getOutFile().writeln("数据同步ERP异常,详情见日志!");
cpContext.getLogFile().write(e.toString(), LogFile.STATEMENT);
}
}
}
3、上传代码到应用服务器
3.1、上传postgresql jdbc jar到应用服务器lib目录($JAVA_TOP 上一层目录的lib目录)
3.2、修改ebs3rdpartyManifest.jar配置文件
在MANIFEST.MF中添加postgresql的上传jar包路径
PS: 不同EBS版本,引用jar包方式可能有差别,此种方式是自己无意中摸索出来的,具体如何导jar,请参考官方提供的方式。
3.3、将java代码编译后,上传编译后 的项目文件到JAVA_TOP下面
3.4、配置java并发请求
最后,提交并发请求,确认数据是否导入ERP。