JAVA调RFC接口

发布时间 2023-07-01 17:28:17作者: 往事只能回味---
package com.swift.oa;



/**
 * @Author: Wriprin
 * @Date: 2022/11/25 17:04
 * @Version 1.0
 */
public class SapConn {
    // SAP server
    private String JCO_ASHOST;
    // SAP system number
    private String JCO_SYSNR;
    // SAP client
    private String JCO_CLIENT;
    // SAP user name
    private String JCO_USER;
    // SAP user password
    private String JCO_PASSWD;
    // SAP language
    private String JCO_LANG;
    // MAX connection
    private String JCO_POOL_CAPACITY;
    // MAX thread
    private String JCO_PEAK_LIMIT;
    // SAP ROUTER
    private String JCO_SAPROUTER;

    public SapConn(String JCO_ASHOST, String JCO_SYSNR, String JCO_CLIENT, String JCO_USER,
                   String JCO_PASSWD, String JCO_LANG, String JCO_POOL_CAPACITY, String JCO_PEAK_LIMIT,
                   String JCO_SAPROUTER) {
        this.JCO_ASHOST = JCO_ASHOST;
        this.JCO_SYSNR = JCO_SYSNR;
        this.JCO_CLIENT = JCO_CLIENT;
        this.JCO_USER = JCO_USER;
        this.JCO_PASSWD = JCO_PASSWD;
        this.JCO_LANG = JCO_LANG;
        this.JCO_POOL_CAPACITY = JCO_POOL_CAPACITY;
        this.JCO_PEAK_LIMIT = JCO_PEAK_LIMIT;
        this.JCO_SAPROUTER = JCO_SAPROUTER;
    }

    public SapConn() {
    }

    public String getJCO_ASHOST() {
        return JCO_ASHOST;
    }

    public void setJCO_ASHOST(String JCO_ASHOST) {
        this.JCO_ASHOST = JCO_ASHOST;
    }

    public String getJCO_SYSNR() {
        return JCO_SYSNR;
    }

    public void setJCO_SYSNR(String JCO_SYSNR) {
        this.JCO_SYSNR = JCO_SYSNR;
    }

    public String getJCO_CLIENT() {
        return JCO_CLIENT;
    }

    public void setJCO_CLIENT(String JCO_CLIENT) {
        this.JCO_CLIENT = JCO_CLIENT;
    }

    public String getJCO_USER() {
        return JCO_USER;
    }

    public void setJCO_USER(String JCO_USER) {
        this.JCO_USER = JCO_USER;
    }

    public String getJCO_PASSWD() {
        return JCO_PASSWD;
    }

    public void setJCO_PASSWD(String JCO_PASSWD) {
        this.JCO_PASSWD = JCO_PASSWD;
    }

    public String getJCO_LANG() {
        return JCO_LANG;
    }

    public void setJCO_LANG(String JCO_LANG) {
        this.JCO_LANG = JCO_LANG;
    }

    public String getJCO_POOL_CAPACITY() {
        return JCO_POOL_CAPACITY;
    }

    public void setJCO_POOL_CAPACITY(String JCO_POOL_CAPACITY) {
        this.JCO_POOL_CAPACITY = JCO_POOL_CAPACITY;
    }

    public String getJCO_PEAK_LIMIT() {
        return JCO_PEAK_LIMIT;
    }

    public void setJCO_PEAK_LIMIT(String JCO_PEAK_LIMIT) {
        this.JCO_PEAK_LIMIT = JCO_PEAK_LIMIT;
    }

    public String getJCO_SAPROUTER() {
        return JCO_SAPROUTER;
    }

    public void setJCO_SAPROUTER(String JCO_SAPROUTER) {
        this.JCO_SAPROUTER = JCO_SAPROUTER;
    }

    @Override
    public String toString() {
        return "SapConn{" +
                "JCO_ASHOST='" + JCO_ASHOST + '\'' +
                ", JCO_SYSNR='" + JCO_SYSNR + '\'' +
                ", JCO_CLIENT='" + JCO_CLIENT + '\'' +
                ", JCO_USER='" + JCO_USER + '\'' +
                ", JCO_PASSWD='" + JCO_PASSWD + '\'' +
                ", JCO_LANG='" + JCO_LANG + '\'' +
                ", JCO_POOL_CAPACITY='" + JCO_POOL_CAPACITY + '\'' +
                ", JCO_PEAK_LIMIT='" + JCO_PEAK_LIMIT + '\'' +
                ", JCO_SAPROUTER='" + JCO_SAPROUTER + '\'' +
                '}';
    }
}
package com.swift.oa;


import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;

import com.sap.conn.jco.*;
import com.sap.conn.jco.ext.DestinationDataProvider;


/**
 * @Author: Wriprin
 * @Date: 2022/11/25 17:01
 * @Version 1.0
 */
public class SAPConnUtil {
    private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";

    /**
     * Establish SAP interface
     * @param name  ABAP name
     * @param suffix    file suffix
     * @param properties    file content
     */
    private static void createDataFile(String name, String suffix, Properties properties){
        File cfg = new File(name+"."+suffix);
        if(cfg.exists()){
            cfg.deleteOnExit();
        }
        try{
            FileOutputStream fos = new FileOutputStream(cfg, false);
            properties.store(fos, "for tests only !");
            fos.close();
        }catch (Exception e){
            System.out.println("Create Data file fault, error msg: " + e.toString());
            throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
        }
    }

    /**
     * Init SAP connection
     */
    private static void initProperties(SapConn sapConn) {
        Properties connectProperties = new Properties();
        // SAP server location
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, sapConn.getJCO_ASHOST());
        // SAP system number
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  sapConn.getJCO_SYSNR());
        // SAP client
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, sapConn.getJCO_CLIENT());
        // SAP user ID
        connectProperties.setProperty(DestinationDataProvider.JCO_USER,   sapConn.getJCO_USER());
        // SAP user PW
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, sapConn.getJCO_PASSWD());
        // SAP language
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   sapConn.getJCO_LANG());
        // MAX connection
        connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, sapConn.getJCO_POOL_CAPACITY());
        // MAX connection threads
        connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, sapConn.getJCO_PEAK_LIMIT());
        // SAP ROUTER
        connectProperties.setProperty(DestinationDataProvider.JCO_SAPROUTER, sapConn.getJCO_SAPROUTER());

        createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
    }

    /**
     * Get SAP connection
     * @return  SAP connection object
     */
    public static JCoDestination connect(SapConn sapConn){
        System.out.println("Connecting to SAP...");
        JCoDestination destination = null;
        initProperties(sapConn);
        try {
            destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
            destination.ping();
            System.out.println("Connection established.");
        } catch (JCoException e) {
            System.out.println("Connect SAP fault, error msg: " + e.toString());
        }
        return destination;
    }
}
package com.swift.oa;



import com.sap.conn.jco.*;

/**
 * @Author: Wriprin
 * @Date: 2022/11/25 17:20
 * @Version 1.0
 */
public class GetMaraInfo {
    public static void main(String[] args) throws JCoException {

        // Configuration of SAP connection
        String JCO_HOST = "192.168.1.250";
        String JCO_SYNSNR = "00";
        String JCO_CLIENT = "000";
        String JCO_USER = "ddic";
        String JCO_PASSWD = "123456789qq";
        String JCO_LANG = "ZH";
        String JCO_POOL_CAPACITY = "30";
        String JCO_PEAK_LIMIT = "100";
        String JCO_SAPROUTER = "/H/192.168.1.250";
        JCO_SAPROUTER="";

        SapConn con = new SapConn(JCO_HOST, JCO_SYNSNR, JCO_CLIENT, JCO_USER, JCO_PASSWD, JCO_LANG, JCO_POOL_CAPACITY, JCO_PEAK_LIMIT, JCO_SAPROUTER);
        JCoDestination jCoDestination = SAPConnUtil.connect(con);

        try {
            // 获取调用 RFC 函数对象
            JCoFunction func = jCoDestination.getRepository().getFunction("ZRFC");

            // 配置传入参数
            JCoParameterList importParameterList = func.getImportParameterList();
            importParameterList.setValue("NAME","Lucy");

            // 调用并获取返回值
            func.execute(jCoDestination);
            
            
            
            String R_NUMBER = func.getExportParameterList().getString("E_RESULT");
            System.out.println("ME51N返回采购申请单号===>"+R_NUMBER);
            
            
            
            
//            // 获取 内表 - ET_MARA
//            JCoTable maraTable = func.getTableParameterList().getTable("ET_MARA");
//
//            // 循环输出 Table 数据
//            for (int i = 0; i < maraTable.getNumRows(); i++) {
//                maraTable.setRow(i);
//
//                String matnr = maraTable.getString("MATNR");
//                String esdra = maraTable.getString("ERSDA");
//                String ernam = maraTable.getString("ERNAM");
//                String matkl = maraTable.getString("MATKL");
//                String meins = maraTable.getString("MEINS");
//
//                System.out.println("物料编号:" + matnr + " - 创建日期:" + esdra + " - 创建人:" + ernam + " - 物料组:" + matkl + " - 单位:" + meins);
//            }

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