首页 > 编程语言 >JAVA调RFC接口

JAVA调RFC接口

时间:2023-07-01 17:34:44浏览次数:42  
标签:SAP JAVA String 接口 RFC SAPROUTER getJCO JCO public

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

 

标签:SAP,JAVA,String,接口,RFC,SAPROUTER,getJCO,JCO,public
From: https://www.cnblogs.com/hua900822/p/17519582.html

相关文章

  • IDEA 2019 java开发工具软件安装教程
    IDEA全称IntelliJIDEA,是java编程语言开发的集成环境。IntelliJ在业界被公认为最好的java开发工具,尤其在智能代码助手、代码自动提示、重构、J2EE支持、各类版本工具(git、svn等)、JUnit、CVS整合、代码分析、创新的GUI设计等方面的功能可以说是超常的。软件介绍析您的代码,查找......
  • 怎么使用java 将url编码转汉字
    URL编码是将URL中的特殊字符转换为%加上对应的ASCII码的形式。而将URL编码转换回原始字符则是通过对URL进行解码操作。在Java中,您可以使用java.net.URLDecoder类来进行URL解码操作。下面是一个示例代码:publicclassURLDecode{publicstaticvoidmain(String[]args){......
  • 怎么使用java 将url编码转汉字
    URL编码是将URL中的特殊字符转换为%加上对应的ASCII码的形式。而将URL编码转换回原始字符则是通过对URL进行解码操作。在Java中,您可以使用java.net.URLDecoder类来进行URL解码操作。下面是一个示例代码:publicclassURLDecode{publicstaticvoidmain(String[]args){......
  • 怎么使用java 将url编码转汉字
    ​URL编码是将URL中的特殊字符转换为%加上对应的ASCII码的形式。而将URL编码转换回原始字符则是通过对URL进行解码操作。在Java中,您可以使用java.net.URLDecoder类来进行URL解码操作。下面是一个示例代码:publicclassURLDecode{publicstaticvoidmain(String[]args)......
  • 怎么使用java 将url编码转汉字
    ​URL编码是将URL中的特殊字符转换为%加上对应的ASCII码的形式。而将URL编码转换回原始字符则是通过对URL进行解码操作。在Java中,您可以使用java.net.URLDecoder类来进行URL解码操作。下面是一个示例代码:publicclassURLDecode{publicstaticvoidmain(String[]args)......
  • 【java】idea
    修改编码配置jdk......
  • javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabl
    一、问题现场二、处理方案VMoptions"-Djdk.tls.disabledAlgorithms=SSLv3,TLSv1.1,RC4,DES,MD5withRSA,DHkeySize<1024,ECkeySize<224,3DES_EDE_CBC,anon,NULL,includejdk.disabled.namedCurves"Workingdirectory$ProjectFileDir$ 三、处理结果......
  • 部署java的linux服务器远程报错:[USM] Channel request shell failed
    问题部署很多个微服务的linux服务器ssh远程突然进不去,远程工具提示:[USM]Channelrequestshellfailed排查分析因为部署的是很多个java进程,有可能线程数占满导致系统可创建线程耗尽,排查步骤如下:使用非远程方式进入服务器,使用top-H命令查看系统创建的线程数:查看系统允许创......
  • Java中的异常处理
    前言在学习过程中,遇到较少的异常处理,对这方面的知识不太熟。在这次重新学习的时候整理一次。1.异常1.1分类运行时异常:可以被避免,编译时可以忽略。检查时异常:人的问题无法遇见,例如打开不存在的文件错误:编译检查不到,脱离人的控制。1.2处理框架将异常定义为类处理,Th......
  • jmeter接口测试 -- 连接数据库(MySQL)
    三个步骤一、下载MySQL的连接驱动1、先查看MySQL的版本1)服务器上查看:mysql--version 2)在连接工具上查看 2、下载连接驱动,下载地址:https://dev.mysql.com/downloads/connector/j/1)选择系统(windows系统才选择我下图中......