【问题标题】:Call SAP methods from Java从 Java 调用 SAP 方法
【发布时间】:2018-04-16 05:34:40
【问题描述】:

我正在尝试与 sap 系统建立连接,并且我拥有执行此操作所需的所有连接属性。

我正在尽力而为,但我遇到了一些我不知道如何解决的问题。

我所需要的只是一个简单的代码示例,通过它我将能够将我的 java 应用程序与 sap 系统集成。

我浏览了一些网站,但找不到与 sap 系统建立连接的解决方案。

我正在尝试使用下面的代码,但我不知道在 createDataFile 方法中写什么。

import com.sap.conn.jco.ext.DestinationDataProvider;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoDestinationManager;
import java.util.Properties;
public class TestMySAP {

    public static void main(String[] args) {
        // This will create a file called mySAPSystem.jcoDestination
        String DESTINATION_NAME1 = "mySAPSystem";
        Properties connectProperties = new Properties();
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "10.129.19.151"); //host
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  "00"); //system number
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "442"); //client number
        connectProperties.setProperty(DestinationDataProvider.JCO_USER,   "MPOSRFC");
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "123456");
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   "en");
        createDataFile(DESTINATION_NAME1, connectProperties);
        // This will use that destination file to connect to SAP
        try {

            JCoDestination destination = JCoDestinationManager.getDestination("mySAPSystem");
            System.out.println("Attributes:");
            System.out.println(destination.getAttributes());
            System.out.println();
            destination.ping();

        } catch (JCoException e){
            e.printStackTrace();
        }

    }
}

【问题讨论】:

  • 为您的第一个问题好友提供良好的格式(通过编辑帮助您)。干得好,祝你好运!
  • 您遇到的错误是什么? @Abishek
  • 我收到此错误@Afgan:com.sap.conn.jco.JCoException:(102) JCO_ERROR_COMMUNICATION:目标 mySAPSystem 初始化失败:连接到 SAP 网关失败连接参数:TYPE=A DEST= mySAPSystem ASHOST=10.129.19.151 SYSNR=00 PCS=1
  • @Abhisheksharma 添加有问题的异常
  • 显然你成功创建了“数据文件”,否则你会得到一个不同的异常。 JCo 默认 DestinationDataProvider 搜索名为 <destinationName>.jcoDestination 的文本文件,其中包含用于此目标的所有登录属性。

标签: java jakarta-ee saprfc sapjco3


【解决方案1】:

关于你在 cmets 中问题的第二部分,对于 BAPI 函数,你可以尝试以下 sn-p:

public static void getCompanyCodes throws JCoException {
        JCoDestination destination = JCoDestinationManager.getDestination(DESTINATION_NAME1);
        JCoFunction function = destination.getRepository().getFunction("BAPI_COMPANYCODE_GETLIST");
        if (function == null)
            throw new RuntimeException("Function not found in SAP.");
        try {
            function.execute(destination);
        } catch (AbapException e) {
            System.out.println(e.toString());
            return;
        }

        JCoStructure returnStructure = function.getExportParameterList().getStructure("RETURN");
        if (!(returnStructure.getString("TYPE").equals("") || returnStructure.getString("TYPE").equals("S"))) {
            throw new RuntimeException(returnStructure.getString("MESSAGE"));
        }

        JCoTable codes = function.getTableParameterList().getTable("COMPANYCODE_LIST");
        for (int i = 0; i < codes.getNumRows(); i++) {
            codes.setRow(i);
            System.out.println(codes.getString("COMP_CODE") + '\t' + codes.getString("COMP_NAME"));
        }
    }

您可以在此处找到 BAPI 函数列表:http://www.sapnet.ru/m/list_BAPI.html

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 2014-04-05
    • 2020-05-04
    • 2016-03-26
    • 2018-09-22
    • 2019-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多