1、下载 和你电脑匹配的32或者64为jco.jar和对应的sapjco.dll文件。
2、jar与dll文件放置位置
第一种(本地程序):
将配套的dll文件放到 system32下面;对应jar包放到自己程序的lib中。
第二种(Web程序):
将配套的dll文件放到tomcat对应的JDK同级的jre目录的bin下面。
下面是本地程序的 例子
3、主要代码
@Test
public void TestSAP1(){
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "192.168.100.23");
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "00");
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "800");
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "bydbpm");
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "987654");
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "zh");
JCoDestination destination = null;
JCoFunction function = null;
try {
// 创建DestinationDataProvider
// 创建配置文件
createDataFile("ABAP_AS_WITH_POOL", "jcoDestination", connectProperties);
destination = JCoDestinationManager.getDestination("ABAP_AS_WITH_POOL");
//ZBFI005_RPM_003_00是sap中定义的函数名称 function = destination.getRepository().getFunction("ZBFI005_RPM_003_00");
System.out.println("function:"+function.getImportParameterList());
//设置单值参数
function.getImportParameterList().setValue("COMPANY_CODEX", "X");
//上述方法就是设置单值的参数,类似于下面
JCoTable jcoTable_y = function.getTableParameterList().getTable("COMPANY_CODEX");
jcoTable_y.appendRow();
jcoTable_y.setValue("SIGN", "I");//默认值 I
jcoTable_y.setValue("OPTION", "EQ");//EQ是等于、BT是范围值
jcoTable_y.setValue("LOW", "X");//参数具体值
//如果参数是多值的情况就类似于下面
JCoTable jcoTable_y = function.getTableParameterList().getTable("COMPANY_CODEX");
jcoTable_y.appendRow();
jcoTable_y.setValue("SIGN", "I");//默认值 I
jcoTable_y.setValue("OPTION", "EQ");//EQ是等于、BT是范围值
jcoTable_y.setValue("LOW", "X");//参数具体值
jcoTable_y.setValue("HIGH","H"); //参数具体值
//拿取 SAP的值
//拿取返回值参数
Table jcoTable = function.getTableParameterList().getTable("COMPANY_CODE");
System.out.println("jcoTableCount:"+jcoTable.getNumRows());
for (int i = 0; i < jcoTable.getNumRows(); i++) {
jcoTable.setRow(i);
String BUKRS = jcoTable.getString("BUKRS");
String BUTXT = jcoTable.getString("BUTXT");
String WAERS = jcoTable.getString("WAERS");
String ADRNR = jcoTable.getString("ADRNR");
String KTOPL = jcoTable.getString("KTOPL");
System.out.println("Test:"+BUKRS+":"+BUTXT+":"+WAERS+":"+ADRNR+":"+KTOPL);
}
} catch (JCoException ex) { System.out.println("SAP连接失败" + ex.getMessage()); } }
void createDataFile(String name, String suffix, Properties properties){
File cfg = new File(name + "." + suffix);
try {
FileOutputStream fos = new FileOutputStream(cfg, false);
properties.store(fos, "Destination - ABAP_AS_WITH_POOL");
fos.close();
} catch (Exception e) {
System.out.println("SAP连接失败" + e.getMessage());
}
}