JCO与bapi的联合使用

来源:互联网 发布:安装网络打印机 编辑:程序博客网 时间:2024/05/17 23:42
JCO简介
JCO是Java Connector的简称,它封装了JAVA-enabled RFC,实现了基于RFC的BAPI与JAVA接口。它提供结构化的BAPI调用,目前不支持面向对象的开发。
JCO使用
1)类导入
import com.sap.mw.jco.*;

2)建立R3连接,有两种方法:
a.持久连接
  1. //申明连接
  2. JCO.Client mConnection;
  3. // 初始化连接
  4. mConnection =
  5. JCO.createClient("001", // SAP client
  6. "<userid>", // userid
  7. "****", // password
  8. "EN", // language (null for the default language)
  9. "<hostname>", // application server host name
  10. "00"); // system number
  11. //建立连接
  12. try {
  13. mConnection.connect();
  14. }
  15. catch (Exception ex) {
  16. ex.printStackTrace();
  17. System.exit(1);
  18. }
  19. //关闭连接
  20. mConnection.disconnect();
复制代码


b.连接池方式
  1. static final String POOL_NAME = "Pool";
  2. JCO.Pool pool = JCO.getClientPoolManager().getPool(POOL_NAME);
  3. if (pool == null) {
  4. OrderedProperties logonProperties =
  5. OrderedProperties.load("/logon.properties");
  6. JCO.addClientPool(POOL_NAME, // pool name
  7. 5, // maximum number of connections
  8. logonProperties); // properties
  9. mConnection = JCO.getClient(POOL_NAME);
  10. System.out.println(mConnection.getAttributes());
  11. }
  12. catch (Exception ex) {
  13. ex.printStackTrace();
  14. }
  15. 。。。。。。
  16. finally {
  17. JCO.releaseClient(mConnection);
  18. }
复制代码


3)获得BAPI方法
  1. JCO.Repository mRepository;
  2. mRepository = new JCO.Repository("ARAsoft", mConnection);
  3. IFunctionTemplate ft =
  4. try{
  5. mRepository.getFunctionTemplate(“BAPI_SALESORDER_GETSTATUS”);
  6. }
  7. catch (Exception ex) {
  8. throw new Exception("Problem retrieving JCO.Function object.");
  9. }
  10.       // Create a function from the template
  11.      jcoFunction = new JCO.Function(ft);
复制代码


4)设定输入参数
  1. JCO.Field SalesDocumentField = jcoFunction.getImportParameterList().getField("SALESDOCUMENT");
  2.      SalesDocumentField.setValue(iSalesDocument);
复制代码


5)执行BAPI
  1. mConnection.execute(jcoFunction);
复制代码


6)处理“return”参数
  1. JCO.Structure jcoBapiReturn = jcoFunction.getExportParameterList().getStructure("RETURN");

  2.       if ((jcoBapiReturn.getField("TYPE").getValue()).toString().equals("E"))
  3.         throw new Exception();
复制代码


7) 获得返回值
  1. JCO.Table jcoStatusInfo = jcoFunction.getTableParameterList().getTable("STATUSINFO");
复制代码


8)最后是异常的捕获与处理

JCO开发建议
基于JAVA语言的特征,建议以如下的方式开发JCO应用。

1)主要的类:
输入参数封装到java bean类中,并统一进行有效性检查。
针对所要调用的BAPI建立代理类,将BAPI的业务功能封装起来。
建立一个解释类,负责调用业务功能类,并将参数从java beans传递到业务功能类。
2)流程: