Java_SAP RFC

2018-03-06  本文已影响0人  hijll

Java 和 SAP 数据交互

RFC 模式

  1. 引入相关 sapjco3.jarsapjco.dll

说明 : 相关 JDK , 开发工具 IntelliJ IDEAEclipse TomcataWeb 容器版本必须一致,目前是64位。编码格式设置一致为 UTF-8

  1. 配置相关的 SAP 信息
Properties connectProperties = new Properties();
//IP 地址
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "xxx.xxx.xxx.xxx");
//系统编号
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "xx");
//集团编号
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "xxx");
//用户名
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "xxx");
//密码
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "xxxx");
//语言
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "ZH");
//最大连接数
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "10");
//最大连接线程
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10");
//超时时间
connectProperties.setProperty(DestinationDataProvider.JCO_MAX_GET_TIME, "1000");
  1. 根据配置文件生成相关信息
private static void createDataFile(String name, String suffix, Properties properties){
File cfg = new File(name+"."+suffix);
if(cfg.exists()){
cfg.deleteOnExit();
}
try{
FileOutputStream fos = new FileOutputStream(cfg, false);
properties.store(fos, "for tests only !");
fos.close();
}catch (Exception e){
// log.error("Create Data file fault, error msg: " + e.toString());
throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
}
}
  1. 创建连接
public static JCoDestination connect(){
JCoDestination destination =null;
try {
destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
} catch (JCoException e) {
// log.error("Connect SAP fault, error msg: " + e.toString());
}
return destination;
}
  1. 方法调用
JCoFunction function = null;
//获取连接
JCoDestination destination = Test.connect();
try {
//获取远程 function 进行后续数据交互
function = destination.getRepository().getFunction("Z_RFC_TEST1");
//导出参数
JCoParameterList imParaList = function.getImportParameterList();
//导入参数
JCoParameterList exportParam = function.getExportParameterList();
//设置传递值
//  exportParam.setValue("xxx", "xxx");
//执行调用
function.execute(destination);
//查询表
JCoTable tb = function.getTableParameterList().getTable("IT");
//循环输出查询到的表数据
for (int i = 0; i < tb.getNumRows(); i++) {
tb.setRow(i);
System.out.print("名字 :" + tb.getString("NAME"));
System.out.print(" | ");
System.out.print("年龄 :" + tb.getInt("AGE"));
System.out.print(" | ");
System.out.println("地址 :"+tb.getString("ADDRESS"));
System.out.println("------------------------------------");
}
} catch (JCoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
上一篇下一篇

猜你喜欢

热点阅读