- 首先准备 sapjco3.jar sapjco3.dll两个文件
- 链接sap 创建 JcoDestination对象
public static JCoDestination getJcoDestination(SapConnInfo sapConnInfo, String destinationName) throws IOException, JCoException {
Properties properties = new Properties();
properties.setProperty(DestinationDataProvider.JCO_USER, sapConnInfo.getJcoUser()); // SAP连接名
properties.setProperty(DestinationDataProvider.JCO_PASSWD, sapConnInfo.getJcoPassword());// 连接密码
properties.setProperty(DestinationDataProvider.JCO_LANG, sapConnInfo.getJcoLang());// 语言"ZF"
properties.setProperty(DestinationDataProvider.JCO_CLIENT, sapConnInfo.getJcoClient());// 集团号
properties.setProperty(DestinationDataProvider.JCO_SYSNR, sapConnInfo.getJcoSysnr());// 系统编号,默认写00 "00"
properties.setProperty(DestinationDataProvider.JCO_ASHOST, sapConnInfo.getJcoAshost());// SAP服务器地址"10.134.28.98"
// String name = "ABAP_AS_BOOL";
String fileName = destinationName;
File cfg = new File(fileName + ".jcoDestination");
if (cfg.exists()) {
cfg.deleteOnExit();
}else {
FileOutputStream fos = new FileOutputStream(cfg, false);
properties.store(fos, "create file");
}
return JCoDestinationManager.getDestination(fileName);
}
3.调用sap 函数
JCoDestination jCoDestination = DbConnection.getJcoDestination(sapConnInfo, destinationName);
//sap 方法返回的table名
String resultTableName = "ZPOGR";
//创建 JCoFunction对象
JCoFunction jCoFunction = jCoDestination.getRepository()
.getFunctionTemplate("ZRFC_CTBS_CFA_POGR_01").getFunction();
//配置函数执行参数
JCoParameterList param = jCoFunction.getImportParameterList();
param.setValue("BSI011", dec.getBsi011());
param.setValue("BSI02", dec.getBsi02());
//调用sap 函数
jCoFunction.execute(jCoDestination);
//获取sap 函数返回的JcoTable
JCoTable jCoTable = jCoFunction.getTableParameterList().getTable(resultTableName);
//然后可以遍历JcoTable获取需要的值