博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA连接SAP
阅读量:7043 次
发布时间:2019-06-28

本文共 5474 字,大约阅读时间需要 18 分钟。

1.首先需要在SAP事务码SE37中新建一个可以被远程调用的RFC

事务码:SE37

新建一个函数组:输入事务码SE37回车后,来到函数构建器屏幕,到上面一排菜单栏:转到 -> 函数组 -> 创建组

 

输入描述信息,方便以后使用,以后功能相似的函数都可以放到该函数组下

 

 函数组创建完毕后,回到SE37初始界面,创建函数,键入函数名后,点击创建按钮

在属性页签下,输入函数的描述,将远程启用的模块选上

 

 

在导入导出参数页签下设置输入输出参数(远程调用模块的注入,输出),要注意参考类型,可选性和传递值

 

 

在源代码中

1 FUNCTION zchenh001. 2 *"---------------------------------------------------------------------- 3 *"*"局部接口: 4 *"  IMPORTING 5 *"     VALUE(P1) TYPE  INT4 DEFAULT 0 6 *"     VALUE(P2) TYPE  INT4 DEFAULT 0 7 *"     VALUE(OPERATOR) TYPE  CHAR1 DEFAULT '+' 8 *"  EXPORTING 9 *"     VALUE(RESULT) TYPE  INT410 *"     VALUE(MSG) TYPE  CHAR25511 *"----------------------------------------------------------------------12   DATA:err_text TYPE string,13        e TYPE REF TO cx_root.14   TRY .15     CASE operator.16       WHEN '+'.       result = p1 + p2.17       WHEN '-'.       result = p1 - p2.18       WHEN '*'.       result = p1 * p2.19       WHEN '/'.       result = p1 / p2.20       WHEN OTHERS.21         CONCATENATE '操作符' operator ',SAP无法识别' into msg.22     ENDCASE.23     CATCH cx_root INTO e.24       err_text = e->get_text( ).25       msg = err_text.26 27   ENDTRY.28 29 ENDFUNCTION.

 在SAP中测试如下:

测试一:

 测试二:

 

测试三:

测试四:

 

 接下来需要下载连接SAP的驱动sapjco3.jar包,

本处提供下载:

  解压密码:1187163927

激活后可以在SAP内部测试 ,至此SAP部分已完成

 

2在eclipse(myeclipse)新建的项目中将sapjco3.jar导入,记得build path.

  打开该项目树状图,将下载好的sapjco3.jar直接拖到该项目中,然后鼠标左键选中该文件,右键Build Path即可。

 2.1 配置与SAP系统的连接(此处最好在SAP系统中新建一个RFC用户)

1 package com.cee.conn; 2  3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.util.Properties; 6 import java.util.logging.Logger; 7  8 import com.sap.conn.jco.JCoDestination; 9 import com.sap.conn.jco.JCoDestinationManager;10 import com.sap.conn.jco.JCoException;11 import com.sap.conn.jco.ext.DestinationDataProvider;12 13 /**14  * 与SAP连接配置15  * 16  * @author jay17  */18 public class SAPConn {19     private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";20     static {21         Properties connectProperties = new Properties();22         connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "xxxx.xxxx.xxxx.xxxx");// 服务器23         connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "xx"); // 系统编号24         connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "xxx"); // SAP集团25         connectProperties.setProperty(DestinationDataProvider.JCO_USER, "xxxx"); // SAP用户名26         connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "xxxxx"); // 密码27         connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "ZH"); // 登录语言:ZH EN28         connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3"); // 最大连接数29         connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10"); // 最大连接线程30 31         createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);32     }33 34     /**35      * 创建SAP接口属性文件。36      * 37      * @param name38      *            ABAP管道名称39      * @param suffix40      *            属性文件后缀41      * @param properties42      *            属性文件内容43      */44     private static void createDataFile(String name, String suffix, Properties properties) {45         File cfg = new File(name + "." + suffix);46         if (cfg.exists()) {47             cfg.deleteOnExit();48         }49         try {50             FileOutputStream fos = new FileOutputStream(cfg, false);51             properties.store(fos, "for tests only !");52             fos.close();53         } catch (Exception e) {54             System.out.println("Create Data file fault, error msg: " + e.toString());55             throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);56         }57     }58 59     /*60      * * 获取SAP连接61      * 62      * @return SAP连接对象63      */64     public static JCoDestination connect() {65         JCoDestination destination = null;66         try {67             destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);68         } catch (JCoException e) {69             System.out.println("Connect SAP fault, error msg: " + e.toString());70         }71         return destination;72     }73 }

2.2 在java代码中测试连接

1 package com.cee.test; 2 import java.io.ObjectInputStream.GetField; 3 import com.cee.conn.SAPConn; 4 import com.sap.conn.jco.JCoDestination; 5 import com.sap.conn.jco.JCoFunction; 6 import com.sap.conn.jco.JCoParameterList; 7 import com.sap.conn.jco.JCoTable; 8  9 public class CheckSnFromSAP {10     public static void main(String[] args) {11         JCoFunction function = null;12         JCoDestination destination = SAPConn.connect();13         int result=0;//调用接口返回状态14         String message="";//调用接口返回信息15         try {16             //调用ZCHENH001函数17             function = destination.getRepository().getFunction("ZCHENH001");18             JCoParameterList input = function.getImportParameterList();19             input.setValue("P1", 10);20             input.setValue("P2", 2);21             input.setValue("OPERATOR", "?"); // 输入参数22             function.execute(destination);23             result= function.getExportParameterList().getInt("RESULT");//调用接口返回结果24             message= function.getExportParameterList().getString("MSG");//调用接口返回信息 25             System.out.println("调用返回结果--->"+result+";调用返回状态--->"+message);26         }catch (Exception e) {27             e.printStackTrace();28         }29     }30 }

运行结果如下:

测试一: 注入参数分别为:10,2,?

    

测试二: 注入参数分别为:10,2,/

    

 

转载于:https://www.cnblogs.com/1187163927ch/p/8670409.html

你可能感兴趣的文章
POJ 2728 Desert King (算竞进阶习题)
查看>>
Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1) C.Producing Snow
查看>>
oo第四单元作业总结
查看>>
linux-阿里云仓库搭建-搭建本地仓库-yum
查看>>
oozie执行报错日志
查看>>
web前端(11)—— 页面布局1
查看>>
1627: [Usaco2007 Dec]穿越泥地
查看>>
算法模板——sap网络最大流 1(非递归+邻接矩阵)
查看>>
sql 去除重复值的问题
查看>>
centos7 修复引导
查看>>
【以2-SAT为主题的婚礼UVA11294】
查看>>
导航浮动
查看>>
Linux命令——watch
查看>>
什么是事物?
查看>>
Python HTTPServer
查看>>
团队作业4——beta阶段冲刺2
查看>>
20个人事主管最常问的问题和最喜欢的答案
查看>>
说说API的防重放机制
查看>>
基于ThreadPool的简单工作管理器
查看>>
webservice 获取调用者IP
查看>>