Call SAP methods from Java

荒凉一梦 提交于 2020-06-13 09:42:23

问题


I am trying to make a connection with the sap systems and I have all the connection properties which are required in order to do so.

I am trying my best but I am facing some issues I have no idea how to resolve.

All I need is a simple code example by which I will be able to integrate my java app with the sap systems.

I have gone through some websites but could not find a solution for making the connection with the sap system.

I am trying with the below code but i do not know that what to write inside createDataFile method.

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();
        }

    }
}

回答1:


Related to the second part of your question in the comments, for BAPI functions you can try the following snippet:

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"));
        }
    }

You can find a list of BAPI functions here: http://www.sapnet.ru/m/list_BAPI.html



来源:https://stackoverflow.com/questions/49850200/call-sap-methods-from-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!