从Java中调用SAP方法

问题描述 投票:0回答:1

我正试图与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();
        }

    }
}
java jakarta-ee sap rfc sapjco3
1个回答
1
投票

与您在评论中的问题的第二部分相关,对于BAPI函数,您可以尝试以下片段。

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.rumlist_BAPI.html

© www.soinside.com 2019 - 2024. All rights reserved.