创建系统级别的账号类型
roduction Password
连接参数不能登陆且没有密码。
以下是RFC直连方式:
package com.pcmall;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
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;
/**
* 与SAP连接配置
* @author jay
*/
public class SAPConn {
private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";
static{
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "xxx.xxx.xxx.xxx");//服务器
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "xxx"); //系统编号
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "xxx"); //SAP集团
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "xxx"); //SAP用户名
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "xxx"); //密码
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "zh"); //登录语言
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3"); //最大连接数
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10"); //最大连接线程
createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
}
/**
* 创建SAP接口属性文件。
* @param name ABAP管道名称
* @param suffix 属性文件后缀
* @param properties 属性文件内容
*/
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);
}
}
/**
* 获取SAP连接
* @return SAP连接对象
*/
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;
}
private static Logger log = Logger.getLogger(SAPConn.class); // 初始化日志对象
}
package com.pcmall;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoTable;
public class MultiFromSAP {
public static void main(String[] args) {
JCoFunction function = null;
JCoDestination destination = SAPConn.connect();
String sum="";//求和
try {
//调用Z_WS_TEST函数
function = destination.getRepository().getFunction("Z_WS_TEST");
JCoParameterList input = function.getImportParameterList();
//NUM1
input.setValue("NUM1", "10");
//NUM2
input.setValue("NUM2", "8");
function.execute(destination);
sum= function.getExportParameterList().getString("NUM");//调用接口返回值
System.out.println("求和为:" + sum);
}catch (Exception e) {
e.printStackTrace();
}
}
}