转载

【SAP】(一)通过JAVA连接SAP 提供参考

温馨提示:
本文最后更新于 2020年08月24日,已超过 1,544 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

(一)通过JAVA连接SAP调用接口 (sapjco3.jar在Windows和MacOS上的配置)

一、sapjoc3.jar获取

由于sap官网提供的链接需要合作公司提供账号密码,如果商用请索要正确的用户名密码下载,如果只是想做测试使用,可以使用我这里提供的链接,资源失效请及时提醒我,谢谢

点击这里下载windows版 密码:1u0t

点击这里下载linux版 密码:7zuv

点击这里下载macOS版 密码:lafl

二、测试项目环境准备

  1. 新建项目
    这个就不多谈了,直接上图,我这里新建了一个空的测试项目,新建lib文件夹,准备把需要用到的jar包丢进来

  2. windows和macOS环境配置

    1. windows
      直接把 sapjco3.jar sapjco3.dll sapjco3.pdb 拷贝至lib下,然后add library把他们都添加进去
    2. macOS
      把 sapjco3.jar 和 libsapjco3.jnilib 拷贝至lib下,同样add libray添加进去,这里只需要add sapjco3.jar 即可。

WARNING: 这里很多博客讲到需要添加classpath在变量里面,这是参照了官方提供的安装方式,这里的话我们只需要将上述文件拷贝至lib文件夹下即可

三、源码编写及测试

首先编写用来放置sap连接信息的SapConn实体类

SapConn

  

/**
 * Created by gang.xu01@hand-china.com on 2018/12/6
 */
public class SapConn {

    // SAP服务器
    private String JCO_ASHOST;
    // SAP系统编号
    private String JCO_SYSNR;
    // SAP集团
    private String JCO_CLIENT;
    // SAP用户名
    private String JCO_USER;
    // SAP密码
    private String JCO_PASSWD;
    // SAP登录语言
    private String JCO_LANG;
    // 最大连接数
    private String JCO_POOL_CAPACITY;
    // 最大连接线程
    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 + '\'' +
                '}';
    }
}

 

然后编写用来建立sap连接的SapConn作为连接类

编写连接类SAPConnUtils 下面是源码

/**
 * Created by gang.xu01@hand-china.com on 2018/12/4
 */
public class SAPConnUtils {

    private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";

    /**
     * 创建SAP接口属性文件。
     * @param name  ABAP管道名称
     * @param suffix    属性文件后缀
     * @param properties    属性文件内容
     */
    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);
        }
    }

    /**
     * 初始化SAP连接
     */
    private static void initProperties(SapConn sapConn) {
        Properties connectProperties = new Properties();
        // SAP服务器
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, sapConn.getJCO_ASHOST());
        // SAP系统编号
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  sapConn.getJCO_SYSNR());
        // SAP集团
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, sapConn.getJCO_CLIENT());
        // SAP用户名
        connectProperties.setProperty(DestinationDataProvider.JCO_USER,   sapConn.getJCO_USER());
        // SAP密码
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, sapConn.getJCO_PASSWD());
        // SAP登录语言
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   sapConn.getJCO_LANG());
        // 最大连接数
        connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, sapConn.getJCO_POOL_CAPACITY());
        // 最大连接线程
        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);
    }

    /**
     * 获取SAP连接
     * @return  SAP连接对象
     */
    public static JCoDestination connect(SapConn sapConn){
        System.out.println("正在连接至SAP...");
        JCoDestination destination = null;
        initProperties(sapConn);
        try {
            destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
            destination.ping();
            System.out.println("已成功建立sap的连接");
        } catch (JCoException e) {
            System.out.println("Connect SAP fault, error msg: " + e.toString());
        }
        return destination;
    }

}

 

接下来编写一个简单的测试类test

/**
 * Created by gang.xu01@hand-china.com on 2018/12/9
 */
public class testCon {

    public static void main(String[] args) {

        SapConn con = new SapConn(
                "127.0.0.1",
                "123",
                "456",
                "abc",
                "abc",
                "abc",
                "123",
                "321",
                "123456"
        );

        // 测试连接
        SAPConnUtils.connect(con);
    }
}

 

如果出现如下结果表明连接失败,请检查连接类。

此时如果调用结果如下则表明建立连接成功。

四、常见报错

  1. 缺少build path

    这个错误是因为缺少了上述提到的文件,请按照文中提到的顺序将其加入项目中即可。

 

正文到此结束
本文目录