SAP Java Connector(JCo)
       JCo是一个高性能的,基于JNI的中间件,它实现了SAP的RFC(Remote Function Call)协议。
      1、JCo的安装

      从 https://files.cnblogs.com/byfhd/jco.rar 下载JCo,解压后将librfc32.dll和sapjcorfc.dll拷贝到WINNT"SYSTEM32下,将sapjco.jar加入项目的classpath中。

      2、与SAP的连接

      JCo支持两种连接方式,direct connections和connection pools。

      direct connections:

      import com.sap.mw.jco.*;

      public TutorialConnect1() {
         JCO.Client mConnection;
         try {
            mConnection = JCO.createClient("001", // SAP client
                                                         "",           // userid
                                                         "****",               // password
                                                         null,                 // language
                                                         "",         // server host name
                                                         "00");                // system number          mConnection.connect();                                                                           
         mConnection.disconnect();
         } catch (Exception ex) {
         }
     }

      connection pools:

      static final String POOL_NAME = "Pool";
      public TutorialConnect2() {
         JCO.Client mConnection;
         try {
            JCO.Pool pool = JCO.getClientPoolManager().getPool(POOL_NAME);
            if (pool == null) {
               OrderedProperties properties = OrderedProperties.load("/logon.properties");
               JCO.addClientPool(POOL_NAME,  // pool name
                                                 5,          // maximum number of connections
                                                 logonProperties); // properties
            }
            mConnection = JCO.getClient(POOL_NAME);
         } catch (Exception ex) {
         } finally {                                                                                                                            //返回连接到连接池,否则会导致无可用连接
            JCO.releaseClient(mConnection);
         }
      }

      logon.properties文件的定义:

      jco.client.client=001
      jco.client.user=userid
      jco.client.passwd=****
      jco.client.ashost=hostname
      jco.client.sysnr=00

      3、对SAP进行操作

      SAP中有JCO.Repository对象,包含着SAP的RFM的运行环境元数据。

      JCO.Repository mRepository;
      mRepository = new JCO.Repository("ARAsoft", mConnection);
      构造器中包含两个参数,第一个是一个任意名称,第二个是连接池或JCO.Client对象。

      IFunctionTemplate对象包含着某个特定的RFM元数据,JCO.Function则代表着一个包含所有参数的RFM。它们之间的关系类似Java中Class和Object的关系。

      IFunctionTemplate ft = mRepository.getFunctionTemplate("BAPI_SALESORDER_GETLIST");
      //ft为空表示没有在SAP发现对应的RFM
      if (ft == null) return null;
      JCO.Function function = ft.getFunction();

      JCO.ParameterList对象包含做Function的输入、输出和表参数。

      client = JCO.getClient(SID);    //从Pool中取得Client
      JCO.ParameterList input = function.getImportParameterList();
      //设置参数,参数名为第二个参数,参数值为第一个参数,
      input.setValue("0000001200", "CUSTOMER_NUMBER"   );
      input.setValue(      "1000", "SALES_ORGANIZATION");
      client.execute(function);

 

posted on 2009-10-25 11:46  ricoo  阅读(839)  评论(0编辑  收藏  举报