ga('set', 'anonymizeIp', 1);
Categories: CodingJAVA

[Java] Connect to SAP Using Sapjco

Share

Connect to SAP using sapjco in Java.

Environment

  • OS: Windows10
  • Java version: 1.8
  • Library: sapjco3

Import libs

Reference files: saro-lab/sap-jco-manager on Github

Sapjco.jar can also be find in this site: JAR download

  1. Sapjco3.dll is needed to put in "C:/windows/system32/" or "C:/windows/SysWOW64/". (Depends on your running environment.)

  2. Import the sapjco3.jar into your project.

Codes

Devided into 3 parts:

  1. SAP connection configuration class.
  2. Functions to connect to SAP.
  3. RFC.

SAP connection configuration class

package com.sap;

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 + '\'' +
                '}';
    }
}

Functions to connect to SAP

package com.sap;

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

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

public class SAPConnUtils {
    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 estiblished.");
        } catch (JCoException e) {
            System.out.println("Connect SAP fault, error msg: " + e.toString());
        }
        return destination;
    }
}

RFC

import java.util.ArrayList;

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoTable;

public class RfcFunctions {
    public ArrayList sendData(JCoDestination des, ArrayList dataList){
        LT_DATA1Data LT_DATA1 = null;
        ArrayList arrayReturnData = new ArrayList();
        JCoFunction  = null;
        try{
             = des.getRepository().getFunctionTemplate("SAP_RFC").getFunction();
            if (null == ) {
                throw new RuntimeException("get  not found in sap");
            } else {
                JCoTable INPUT_TABLE = .getTableParameterList().getTable("TABLE_INPUT_NAME");
                JCoTable OUTPUT_TABLE = null;
                for(int i=0;i 

Send data to SAP Using RFC

import java.util.ArrayList;
import java.util.Iterator;
import com.sap.conn.jco.JCoDestination;
public class sendDatatoSAP {
    public String doProcess(String JCO_HOST, String JCO_CLIENT, String JCO_USER, String JCO_PASSWD)throws Exception {
        JCoDestination destination = null;
        String returnStr = "";
        // Configuration of SAP connection
        SapConn con = new SapConn(JCO_HOST, "00", JCO_CLIENT, JCO_USER, JCO_PASSWD, "ZH", "", "", "");
        destination = SAPConnUtils.connect(con);
        RfcFunctions RfcFunctions = new RfcFunctions();
        ArrayList dataList = new ArrayList();
        ArrayList LT_RETURN = new ArrayList();

        dataList.add(new LT_DATA1Data("TEST1","Data1","Data2","Data3","Data4"));

        LT_RETURN = RfcFunctions.sendData(destination, dataList);
        Iterator iterator = LT_RETURN.iterator();
        if(iterator.hasNext()){
            while(iterator.hasNext()){
                returnStr += iterator.next();
                returnStr += "\n";
            }
        }else{
            returnStr = "Success!";
        }

        return returnStr;

Above, ArrayList and are defined by user. You can use other struct design.

Jys

Published by
Jys

Recent Posts

[python] Flask Create RESTful API

This article gi... Read More

3 年 前發表

[Javascript] 新增/刪除JSON中key值

在web訊息交換常會需要對JS... Read More

3 年 前發表

[JAVA] SQL Server Connection

本文介紹JAVA連線SQL s... Read More

3 年 前發表