java使用JCO调用SAP接口

本文介绍如何使用Java连接SAP系统并调用SAP函数。主要内容包括配置连接属性、创建连接池、调用SAP函数及处理返回数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

博客参考:http://blog.youkuaiyun.com/jay_1989/article/details/51821069 (可以说我是基本照抄,所以本篇博客为转载)

其实在写这篇博客之前,已经有很多前辈写过类似的技术博客,
所以我这篇博客算是对我自己技术的一个记录。

首先。最基本的准备你需要有一下两个小东西(根据自己电脑装的jdk去下载)

  • sapjco3.jar
  • sapjco3.dll

sapjco3.dll,你可以将他放在WEB-INF\lib目录下。也可以将相对应位数的sapjco3.dll文件拷贝至system32文件下。
博主是将sapjco3.dll文件放在WEB-INF\lib

多的不说了,上代码
SAPConn.java即JAVA与sap连接代码

/**
 * 与SAP连接配置
 * @author changcheng
 */
public class SAPConn {

    private static Logger log = Logger.getLogger(SAPConn.class);
    private static final String ABAP_AS_POOLED = "ECD";//连接池名
    static{

        Properties connectProperties = new Properties();

        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "***.***.***.***"); //服务器开发机
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  "*");       //系统编号        
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "*");          //SAP集团 开发机
        connectProperties.setProperty(DestinationDataProvider.JCO_USER,   "x");     //账户
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "*");   //密码

        connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   "ZH");        //登录语言
        connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "30");  //最大连接数  
        connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "100");     //最大连接线程

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

    /**
     * 创建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();
        }
    /*
     *  Properties类是集合和IO技术结合的成果.可以把数据以Map集合的形式存入,也可以通过IO流技术读取或者输出. 
     *  该类的store方法,是一个输出方法. store(OutputStream out, String comments) 
     *  以适合使用 load(InputStream) 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。
     */
        try{
            FileOutputStream fos = new FileOutputStream(cfg, false);
            properties.store(fos, "for tests only !");
            fos.close();
        }catch (Exception e){
            log.error("Create Data file fault, error msg: " + e.toString());
            throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
        }
    }

    /**
     * 获取SAP连接
     * @return  SAP连接对象
     */
    public static JCoDestination connect(){
        JCoDestination destination =null;
        try {
            destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);//获取SAP连接
        } catch (JCoException e) {
            log.error("Connect SAP fault, error msg: " + e.toString());
        }
        return destination;
    }
}

SAPtest。连接sap获取到具体sap接口

public class SAPtest {


      public static void main(String[] args) {      
            try {
                 //连接SAP,获取一个连接对象
                 JCoDestination lj = SAPConn.connect();
                 //获取到SAP的函数
                 JCoFunction function = lj.getRepository().getFunction("ZEPIC_ACCT_DOC_POST");
                 //将该函数的结构打印出来
                 System.out.println(SAPUtil.formatXML(function.toXML()));


                 //该入参没有放在结构表或者表格中,直接使用getImportParameterList方法
                 JCoParameterList imP = function.getImportParameterList();
                 //利用setValue将入参参数传入
                 imP.setValue("key", "value");

                 //入参结构表时,使用JCoStructure声明对象
                 //IS_ORDER表示该结构的名称
                 JCoStructure  stu = function.getImportParameterList().getStructure("IS_ORDER");
                 stu.setValue("key", "value");
                 stu.setValue("key", "value");
                 stu.setValue("key", "value");
                 stu.setValue("key", "value");
                 stu.setValue("key", "value");


                 //入参表格     表格是具有多行数据的
                 JCoTable jCoTable = function.getTableParameterList().getTable("IT_ITEMS"); 
                 jCoTable.appendRow();//增加一行 

                 jCoTable.setValue("key", "value");
                 //执行将数据传给sap
                 function.execute(lj);


                 //既然是传给,那sap也会给你返回信息,首先你的看清楚返回的时表格还是结构

                 //返回的是表格
                 //获取到该表格的名称
                 JCoTable jCoTable1 = function.getTableParameterList().getTable("ET_TTCLEAR");

                 for (int i = 0; i < jCoTable.getNumRows(); i++) {
                    //定位到当前行
                    jCoTable.setRow(i);

                    //然后将字段数据一一拿出
                    String str = jCoTable.getString("字段名称");//为了方便
                 }    

                 //返回的时结构
                 //获取到该结构的名称
                 JCoStructure  jCoStructure = function.getExportParameterList().getStructure("ES_RETURN");
                 //然后将数据拿出
                 String str1 = jCoStructure.getString("字段名称");
                 String str2 = jCoStructure.getString("字段名称");


                 /**
                  * 接下来就时用SAP返回给你的数据处理自己的业务了
                  */

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

      }

}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值