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 INT4 10 *" VALUE(MSG) TYPE CHAR255 11 *"---------------------------------------------------------------------- 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包,
本处提供下载: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 jay 17 */ 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 EN 28 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 name 38 * ABAP管道名称 39 * @param suffix 40 * 属性文件后缀 41 * @param properties 42 * 属性文件内容 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://blog.ytso.com/tech/pnotes/18033.html