java rfc 二围数据_java rfc 拉取数据

1. 连接rfc  需要有sap jar包   .jar放项目里,  windows  .dll 文件放项目lib下或者 jdk bin目录下  mac  .jnilib 放jdk 的jre lib 目录下

mac 下载地址: https://files.cnblogs.com/files/suibingbing/sapjco3_mac.zip

windows 下csdn 有。    这里用的是 sapjco3 的版本

1 packagecom.sapapi.util;2

3 importjava.io.File;4 importjava.io.FileOutputStream;5 importjava.util.Properties;6

7 importorg.apache.log4j.Logger;9

10 importcom.sap.conn.jco.JCoDestination;11 importcom.sap.conn.jco.JCoDestinationManager;12 importcom.sap.conn.jco.JCoException;13 importcom.sap.conn.jco.ext.DestinationDataProvider;14

15

16 /**

17 * 与SAP连接配置18 *@authorjay19 */

20 public classSAPConn {21 private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";22 static{23 Properties connectProperties = newProperties();24

25

26 connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "192.168.1.1");//服务器

27 connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "00"); //系统编号

28 connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "300"); //SAP集团

31 connectProperties.setProperty(DestinationDataProvider.JCO_USER, "*******"); //SAP用户名

32 connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "*******"); //密码

33 connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "zh"); //登录语言

34 connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3"); //最大连接数

35 connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10"); //最大连接线程

36

37 createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);38 }39

40 /**

41 * 创建SAP接口属性文件。42 *@paramname ABAP管道名称43 *@paramsuffix 属性文件后缀44 *@paramproperties 属性文件内容45 */

46 private static voidcreateDataFile(String name, String suffix, Properties properties){47 File cfg = new File(name+"."+suffix);48 if(cfg.exists()){49 cfg.deleteOnExit();50 }51 try{52 FileOutputStream fos = new FileOutputStream(cfg, false);53 properties.store(fos, "for tests only !");54 fos.close();55 }catch(Exception e){56 log.error("Create Data file fault, error msg: " +e.toString());57 throw new RuntimeException("Unable to create the destination file " +cfg.getName(), e);58 }59 }60

61 /**

62 * 获取SAP连接63 *@returnSAP连接对象64 */

65 public staticJCoDestination connect(){66 JCoDestination destination =null;67 try{68 destination =JCoDestinationManager.getDestination(ABAP_AS_POOLED);69 } catch(JCoException e) {70 log.error("Connect SAP fault, error msg: " +e.toString());71 }72 returndestination;73 }74 private static Logger log = Logger.getLogger(SAPConn.class); //初始化日志对象

75 }

2,封装读取数据  util,

package com.sapapi.util;

import com.sap.conn.jco.*;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import static org.hibernate.util.JDBCExceptionReporter.log;

public class SapRfcUtil{

private static JCoFunction function = null;

private static JCoDestination destination =null;

private SapRfcUtil(JCoFunction function) {

super();

this.function = function;

}

public SapRfcUtil() {

super();

// TODO Auto-generated constructor stub

}

public static SapRfcUtil newInstance(){

return new SapRfcUtil();

}

public SapRfcUtil setFunction(String functionName){

try {

destination = SAPConn.connect();

System.out.println(destination.getAttributes());

function = destination.getRepository().getFunction(functionName);

} catch (JCoException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return this;

}

public SapRfcUtil setParameterTable(String tableName, String[] values){

JCoTable codes = function.getTableParameterList().getTable(tableName);

for(int i = 0;i

codes.appendRow();

codes.setRow(i);

codes.setValue(0, values[i]);

}

return this;

}

public SapRfcUtil setParameter(String key, String value){

JCoParameterList input = function.getImportParameterList();

input.setValue(key,value);

return this;

}

public List execute(){

try {

function.execute(destination);

} catch (JCoException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

JCoParameterList tableParamList = function.getTableParameterList();

JCoMetaData jCoMetaDataList = tableParamList.getMetaData();

System.out.println(jCoMetaDataList);

List result = new ArrayList();

for(int y=0;y

String tableName = jCoMetaDataList.getName(y);

Map maplist = new HashMap();

JCoTable table = tableParamList.getTable(y);

JCoMetaData jCoMetaData = table.getMetaData();

System.out.println(jCoMetaData);

List list = new ArrayList();

for (int i = 0; i < table.getNumRows(); i++) {

Map map = new HashMap();

for(int j = 0;j

//为行设置值;

table.setRow(i);

try {

map.put(jCoMetaData.getName(j), table.getString(jCoMetaData.getName(j)));

} catch (JCoRuntimeException e1) {

map.put("message", "【"+jCoMetaData.getName(j)+"】 该输出参数错误!");

list.add(map);

log.info("-----strReturnCode = " + list);

}

}

list.add(map);

maplist.put(tableName,list);

}

result.add(maplist);

}

return result;

}

}

3 test 使用方法。表参形式

1 List list =SapRfcUtil.newInstance()2 .setFunction("ZFI_GET_UKM_MASSDATA") //rfc 函数名3 .setParameterTable("I_PARTNERS", new String[]{"0000000001","0000100109"}) //rfc 入参table 类型4 .setParameterTable("I_SGMNTS", new String[]{"Z001"})5 .execute();6 System.out.println(list.toString());

结果:

9f8467869a12df798d4c4fc2c9cfb71d.png

4.普通参数形式

List list =SapRfcUtil.newInstance()

.setFunction("ZMMI0006")

.setParameter("MATNR_LOW","000000000010000001")

.setParameter("MATNR_HIGH","000000000010999999")

.setParameter("MATKL_LOW","1001")

.setParameter("MATKL_HIGH","6999")//.setParameter("LAEDA_LOW","20190101")//.setParameter("LAEDA_HIGH","20190104")

.setParameter("REMARK","1")

.execute();

System.out.println(list.toString());

59f226a01a7c77e14cd290b65889eb2e.png

数据解析:

execute()方法中:

JCoParameterList tableParamList = function.getTableParameterList();

1 for (int y = 0; y < tableParamList.getFieldCount(); y++) {2 //表注释

3 String tableMemo =jCoMetaDataList.getDescription(y);4 //表名

5 String tableName = jCoMetaDataList.getRecordTypeName(y);17 JCoTable table =tableParamList.getTable(y);18 JCoMetaData jCoMetaData =table.getMetaData();19 log.info(jCoMetaData.toString());20

21 23 //字段

24 for (int i = 0; i < jCoMetaData.getFieldCount(); i++) {25 26 //字段注释

27 String expDetailMemo =jCoMetaData.getDescription(i);28 //字段

29 String expDetailName =jCoMetaData.getName(i);30 //字段长度

31 int length = jCoMetaData.getLength(i);32 39 40 }41 //数据

42 List list = newArrayList();43 for (int i = 0; i < table.getNumRows(); i++) {44 Map map = newHashMap();45 for (int j = 0; j < jCoMetaData.getFieldCount(); j++) {46 //为行设置值;

47 table.setRow(i);48 map.put(jCoMetaData.getName(j), table.getString(jCoMetaData.getName(j)));49 }50 list.add(map);51 }52 resultMap.put(tableName, list);53

54 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值