Java连接SAP RFC 实现数据抽取

包管理

首先要连接SAP需要两个包,分别为sapjco3.jar和sapjco3.dll,切记这两个包需要放在同一目录;否则会报错。

我先尝试将这两个包发到私服上,再在pom.xml文件中引入,发现.dll文件下载不了;于是就将这两个包放在根目录sap下,dockerfile构建镜像时将这两个包copy到jdk/lib目录下。

COPY sap/sapjco3.dll /usr/local/jdk-17.0.5/lib/
COPY sap/sapjco3.jar /usr/local/jdk-17.0.5/bin/

功能介绍

  • 连接SAP
  • 入参获取
  • 出参获取
  • 函数列表获取
  • 表名获取
  • 函数执行
  • 执行结果获取

Java连接SAP RFC

public class CheckSnFromSAP {

    private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";

    public static void main(String[] args) {
        JCoFunction function = null;

        MyDestinationDataProvider destDataProvider = new MyDestinationDataProvider();
        try {

            Properties connectProperties = new Properties();
            connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "xxx");// 服务器
            connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "xx"); // 系统编号
            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"); // 登录语⾔:ZH EN
            connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3"); // 最⼤连接数
            connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10"); // 最⼤连接线程

            // 注册连接参数

            destDataProvider.addDestination(ABAP_AS_POOLED, connectProperties);
            Environment.registerDestinationDataProvider(destDataProvider);

            // 创建一个与SAP系统的连接
            JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);

            // 调用函数
            function = destination.getRepository().getFunction("funcationName");
            if(null == function) {
                throw new RuntimeException("无此函数");
            }

            // 输入字段
            JCoListMetaData listMetaData = function.getImportParameterList().getListMetaData();
            List inputFields = new ArrayList();
            for (int i=0; i< listMetaData.getFieldCount(); i++) {
                Map row = new HashMap();
                String name = listMetaData.getName(i);
                String type = listMetaData.getTypeAsString(i);
                String sapType = listMetaData.getRecordTypeName(i);
                String description = listMetaData.getDescription(i);
                row.put("name", name);
                row.put("type", type);
                row.put("sapType", sapType);
                row.put("description", description);
                inputFields.add(row);
            }

            // 输出字段
            JCoTable output = function.getTableParameterList().getTable("XXX");
            JCoRecordMetaData recordMetaData = output.getRecordMetaData();
            List outputFields = new ArrayList();
            for (int i=0; i< recordMetaData.getFieldCount(); i++) {
                Map row = new HashMap();
                String name = recordMetaData.getName(i);
                String type = recordMetaData.getTypeAsString(i);
                String sapType = recordMetaData.getRecordTypeName(i);
                String description = recordMetaData.getDescription(i);
                row.put("name", name);
                row.put("type", type);
                row.put("sapType", sapType);
                row.put("description", description);
                outputFields.add(row);
            }

            // 获取所有函数
            function = destination.getRepository().getFunction("RFC_FUNCTION_SEARCH");
                    if(null == function) {
                throw new RuntimeException("无此函数");
            }
            JCoParameterList importParams = function.getImportParameterList();
                    importParams.setValue("FUNCNAME", "*"); // Search for all functions
                    function.execute(destination);
            JCoTable functionTable = function.getTableParameterList().getTable("FUNCTIONS");
            int max = Math.min(functionTable.getNumRows(),500);
                    for (int i = 0; i