一、方式一接口数据读取及返回值
public class SapConnect {
static final String ABAP_AS_POOLED = "ABAP5_WITHOUT_POOL";//配置文件名
static{
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST,"ip");//IP
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,"00");//系统编号
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT,"100"); //客户端编号
connectProperties.setProperty(DestinationDataProvider.JCO_USER,"user");//用户名
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD,"pass");//password
connectProperties.setProperty(DestinationDataProvider.JCO_LANG,"ZH");//语言
createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
}
/* 创建连接池的配置文件 */
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){
System.out.println("Create Data file fault, error msg: " + e.toString());
throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
}
}
public static void main(String[] args) {
String bapiFunctionName = "ZOA_GET_MARC"; //函数名称
JCoFunction function = null;
//JCoFunction是一个接口,代表SAP系统的函数库
JCoDestination destination = null;
try {
//连接SAP,获取一个连接对象
destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
System.out.println("链接成功");
//获取到SAP的函数
function = destination.getRepository().getFunction(bapiFunctionName);
System.out.println("function=="+function);
JCoParameterList input = function.getImportParameterList();
input.setValue("WERKS","2100"); //参数
input.setValue("MATNR", "000000000000031104"); //参数
function.execute(destination); //执行
// 第一部分,返回值
JCoParameterList output = function.getExportParameterList(); // 获得到的数据。
System.out.println("******************");
System.out.println(output);
// 第二部分,迭代器返回值调用
// Map resultMap = new HashMap<>();
// getExportParameterList(function,resultMap);
} catch (JCoException e) {
System.out.println("链接失败");
e.printStackTrace();
}
}
//第二部分调用函数
/**
* 获取表格输出列表
*
* @param jCoFunction 函数
* @param resultMap 返回值
*/
public static void getTableParameterList(JCoFunction jCoFunction, Map resultMap) {
for (Iterator<JCoField> iterator = jCoFunction.getTableParameterList().iterator(); iterator.hasNext(); ) {
traversalField(resultMap, iterator);
}
}
/**
* 获取输出参数列表
*
* @param jCoFunction 函数
* @param resultMap 返回值
*/
public static void getExportParameterList(JCoFunction jCoFunction, Map resultMap) {
for (Iterator<JCoField> iterator = jCoFunction.getExportParameterList().iterator(); iterator.hasNext(); ) {
traversalField(resultMap, iterator);
}
}
/**
* 遍历字段
*
* @param resultMap 返回值
* @param iterator 迭代器
*/
private static void traversalField(Map resultMap, Iterator<JCoField> iterator) {
JCoField jCoField = iterator.next();
if (jCoField.isTable()) {
JCoTable table = jCoField.getTable();
List resultList = new ArrayList();
for (int i = 0, len = table.getNumRows(); i < len; i++) {
Map retMap = new HashMap();
table.setRow(i);
for (JCoRecordFieldIterator jCoRecordFieldIterator = table.getRecordFieldIterator(); jCoRecordFieldIterator.hasNextField(); ) {
JCoField field = jCoRecordFieldIterator.nextRecordField();
retMap.put(field.getName(), field.getValue());
}
resultList.add(retMap);
}
resultMap.put(jCoField.getName(), resultList);
} else if (jCoField.isStructure()) {
JCoStructure jCoStructure = (JCoStructure) jCoField;
Map resultStructureMap = new HashMap();
for (JCoFieldIterator jCoFieldIterator = jCoStructure.getFieldIterator(); jCoFieldIterator.hasNextField(); ) {
JCoField jcf = jCoFieldIterator.nextField();
resultStructureMap.put(jcf.getName(), jcf.getValue());
}
resultMap.put(jCoField.getName(), resultStructureMap);
} else {
resultMap.put(jCoField.getName(), jCoField.getValue());
}
}
二、方式二
1、创建文件 sap_conf.properties
jco.client.ashost=127.0.0.1 #IP
jco.client.sysnr=00 #系统编号
jco.client.client=001 #客户端编号
jco.client.passwd=password #密码
jco.client.user=username #用户名
jco.client.lang=zh #语言
jco.destination.pool_capacity=30 #最大空闲连接数
jco.destination.peak_limit=150 #最大活动连接数
2、创建连接,sapPool
@Service("sap_conf")
public class sapPool {
private static final String ABAP_AS_POOLED = "ABAP5_WITHOUT_POOL";//配置文件名
static final String ABAP_T = "ABAP5_WITHOUT_POOL";
public static JCoDestination getSAPDestination(){
try {
JCoDestination dest = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
return dest;
} catch (JCoException ex) {
//System.out.println("连接失败,重新连接!");
return RegetJocodestination();
}
}
/*****
* 函数功能描述:重新获取JCODestination
* @return
*****/
public static JCoDestination RegetJocodestination(){
try{
Properties properFile = new Properties();
ClassLoader cl=Thread.currentThread().getContextClassLoader();
String filePath=cl.getResource("").toString()+"SAPConnectionPool.properties";
filePath=filePath.replace("file:", "");
filePath=filePath.replace("%20", " ");//替换文件名包含空格的
FileInputStream inputFile = new FileInputStream(filePath);
properFile.load(inputFile);
inputFile.close();
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, properFile.getProperty("jco.client.ashost"));
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, properFile.getProperty("jco.client.sysnr"));
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, properFile.getProperty("jco.client.client"));
connectProperties.setProperty(DestinationDataProvider.JCO_USER, properFile.getProperty("jco.client.user"));
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, properFile.getProperty("jco.client.passwd"));
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, properFile.getProperty("jco.destination.pool_capacity"));
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, properFile.getProperty("jco.destination.peak_limit"));
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, properFile.getProperty("jco.client.lang"));
// 验证连接,使用第4步方法
// CustomDestinationDataProvider provider = new CustomDestinationDataProvider();
// provider.addDestinationProperties(ABAP_AS_POOLED, connectProperties);
// Environment.registerDestinationDataProvider(provider);
try {
JCoDestination dest = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
return dest;
} catch (JCoException ex) {
System.out.println(ex);
System.out.println("重新连接失败");
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
3、输入参数,获取信息
public class sapgetdata {
public void sapgetData() throws JCoException {
JCoDestination destination =sapPool.getSAPDestination(); //获取连接
String bapiFunctionName = "ZOA_GET_MARC";
JCoFunction function=destination.getRepository().getFunction(bapiFunctionName);
JCoParameterList input=function.getImportParameterList();
input.setValue("WERKS","2100");
input.setValue("MATNR", "000000000000031104");
function.execute(destination);
JCoParameterList output = function.getExportParameterList();
System.out.printf(output.getString("DISPO"));
System.out.printf(output.getString("MMSTA"));
System.out.printf(output.getString("ISEINE")+'\n');
}
public static void main(String[] args) throws JCoException {
new sapgetdata().sapgetData();
}
}
4、*验证连接,创建接口文件信息
public class CustomDestinationDataProvider implements DestinationDataProvider {
private Map providers = new HashMap();
@Override
public Properties getDestinationProperties(String destName) {
if (destName == null)
throw new NullPointerException("请指定目的名称");
if (providers.size() == 0)
throw new IllegalStateException("请加入一个目的连接参数属性给提供者");
return (Properties)providers.get(destName);
}
// 没有实现事件处理
@Override
public boolean supportsEvents(){
return false;
}
@Override
public void setDestinationDataEventListener(DestinationDataEventListener listener) {
throw new UnsupportedOperationException();
}
public void addDestinationProperties(String destName, Properties provider) {
providers.put(destName, provider);
}
}