连接sap大概需要以下几步:
windows环境下执行需要:sapjco3.jar、sapjco3.dll
1、获取连接(有四种格式) 2、获取相应函数 3、入参设置(一般有三种格式的入参:一般参数、JCoStructure参数、JCoTable参数)4、执行execute()方法,获取结果 5、根据返回结果解析相应数据。
一、获取连接:
(1)主动生成配置文件
Properties connectProperties = new Properties(); connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "127.0.0.1");//IP connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "00");//系统编号 connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "001");//客户端编号 connectProperties.setProperty(DestinationDataProvider.JCO_USER, "username");//用户名 connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "password");//密码 connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "zh");//语言 createDestinationDataFile(DESTINATION_NAME1, connectProperties); connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "30");//最大空闲连接数 connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "150");//最大活动连接数 createDestinationDataFile(DESTINATION_NAME2, connectProperties);//创建连接文件
//创建配置文件的方法
static void createDestinationDataFile(String destinationName, Properties connectProperties) {
File destCfg = new File(destinationName + ".jcoDestination");
try {
FileOutputStream fos = new FileOutputStream(destCfg, false);
connectProperties.store(fos, "For tests only !");
fos.close();
} catch (Exception e) {
throw new RuntimeException("Unable to create the destination files", e);
}
}
(2)配置文件读取法(此处不做介绍)
(3)通过代码不生成配置文件方式(主要介绍)
首先创建DestinationDataProviderImpl类实现DestinationDataProvider接口,代码如下
package com.intmes.message.webservice.util;
import com.sap.conn.jco.ext.DestinationDataEventListener;
import com.sap.conn.jco.ext.DestinationDataProvider;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class DestinationDataProviderImpl implements DestinationDataProvider {
private Map provider = new HashMap();
@Override
public Properties getDestinationProperties(String destName) {
if (destName == null){
throw new NullPointerException("Destination name is empty.");
}
if (provider.size() == 0){
throw new IllegalStateException("Data provider is empty.");
}
return (Properties) provider.get(destName);
}
@Override
public boolean supportsEvents() {
return false;
}
@Override
public void setDestinationDataEventListener(DestinationDataEventListener destinationDataEventListener) {
throw new UnsupportedOperationException();
}
public void addDestinationProps(String destName, Properties props){
provider.put(destName, props);
}
}
然后创建DestinationManager(类名可以自己命名),此处我这边设置了两种方式:一种代码直接读取,另一种从代码里配置
package com.intmes.message.webservice.util;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.DestinationDataProvider;
import com.sap.conn.jco.ext.Environment;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class DestinationManager {
private static Properties setProperties(String destName)
{
// SAP connection parameters and other properties
Properties props = new Properties();
String filePath = "D://"+destName+".jocdestination";
File cfg = new File(filePath);
if (cfg.exists()){
try {
FileInputStream inputStream = new FileInputStream(cfg);
props.load(inputStream);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}else{
if (destName == "SAP_AS") {
props.setProperty(DestinationDataProvider.JCO_ASHOST, "192.168.44.100");//host
props.setProperty(DestinationDataProvider.JCO_SYSNR, "00");
props.setProperty(DestinationDataProvider.JCO_USER, "STONE");//账号
props.setProperty(DestinationDataProvider.JCO_PASSWD, "w123456");//密码
props.setProperty(DestinationDataProvider.JCO_CLIENT, "001");//clentid
props.setProperty(DestinationDataProvider.JCO_LANG, "EN");//语言
props.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "30");//最大空闲连接数
props.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "150");//最大活动连接数
}
}
return props;
}
public static JCoDestination getDestination (String destName) throws JCoException {
Properties props = setProperties(destName);
DestinationDataProviderImpl providerImpl = new DestinationDataProviderImpl();
providerImpl.addDestinationProps(destName, props);
Environment.registerDestinationDataProvider(providerImpl);
JCoDestination dest = JCoDestinationManager.getDestination(destName);
return dest;
}
}
二、获取函数
public static JCoDestination getSAPDestination() throws JCoException {
JCoDestination destination = DestinationManager.getDestination(DESTINATION_NAME2);//DESTINATION_NAME2为连接池名字,可自行命名,需要的地方保持一致即可
return destination;
}
JCoDestination destination = getSAPDestination();
JCoFunction function = destination.getRepository().getFunction("FUNCTION_NAME");//获取函数
三、设置参数
//1.单独的参数,不在表结构下
function.getImportParameterList().setValue("NAME1", "VALUE1");// 参数
//2.JCoStructure 一般为HEADER参数
JCoStructure Structure=function.getImportParameterList().getStructure("_HEADER");
Structure.setValue("NAME1", "VALUE1");//参数
Structure.setValue("NAME2", "VALUE2");//参数
//3.JCoTable 主体参数,可为多个主体参数。。。
JCoTable headerImportParam = function.getTableParameterList().getTable("_TABLE");//返回的值i个字段作为一个表
//如果为参数集合,在外层加for循环即可
headerImportParam.appendRow();//附加表的最后一个新行,行指针,它指向新添加的行。
headerImportParam.setValue("NAME1", "VALUE1");//参数
headerImportParam.setValue("NAME2", "VALUE2");//参数
四、执行获取结果(具体结果需要根据相应需求进行解析)
function.execute(destination); JCoParameterList returnData = function.getExportParameterList();
版权声明:本文为qq_35154053原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。