Call SAP RFC by JCO

  Recently I try to call SAP RFC by .net, although i can connect to SAP successfully, but failed on passing table parameter with multi-columns to RFC. So today try to do that by java. Just log it for use when i want it. The below code is just for testing.

 

====================Code Begin==============================

package sapref;
import com.sap.mw.jco.*;
import java.io.FileInputStream;
import java.io.InputStream;

import java.util.Properties;


public class JavaCallRFC {
    private Properties p = new Properties();

    private static final String POOL_NAME = "MYCONN";


    public static JCO.Pool pool;
    public JCO.Repository mRepository;
    public JCO.Client client;

    public JavaCallRFC() {
        try {
            InputStream inStream;
            inStream = new FileInputStream("test.properties");
            p.load(inStream);
            pool = JCO.getClientPoolManager().getPool(POOL_NAME);
            if (pool == null) {
                JCO.addClientPool(POOL_NAME, 100, p);
            }
            client = JCO.getClient(POOL_NAME);
            //We have two methods to create Repository
            //User Pool when create Repository
            mRepository = new JCO.Repository("SAPJcorep", POOL_NAME);
           
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    //Create function using Repository and IFunctionTemplate
    public JCO.Function createFunction(String name) throws Exception {
        try {
            IFunctionTemplate ft;
            ft = mRepository.getFunctionTemplate(name.toUpperCase());
            if (ft == null) {
                return null;
            } else {
                return ft.getFunction();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }


    public static void main(String[] args) {
        JavaCallRFC javaCallRFC = new JavaCallRFC();
        JCO.Function function;
        try {
            function = javaCallRFC.createFunction("RFC_READ_TABLE");
            if (function == null) {
                System.out.println("Can't create function!");
                return;
            }
            JCO.ParameterList pl = function.getImportParameterList();
            //pass single value parameter to RFC
            pl.setValue("ZTAB_EMPLOYEE", "QUERY_TABLE");
            pl.setValue(";", "DELIMITER");
            //pass table parameter to RFC
            JCO.ParameterList itabList = function.getTableParameterList();
            JCO.Table table = itabList.getTable("FIELDS");
            table.appendRow();
            table.setValue("NAME", 0);
            table.setValue(10, 1);
            table.setValue(10, 2);
            table.appendRow();
            table.setValue("PHONE", 0);
            table.setValue(20, 1);
            table.setValue(20, 2);
            table.appendRow();
            table.setValue("EMAIL", 0);
            table.setValue(30, 1);
            table.setValue(30, 2);
            function.setTableParameterList(itabList);
            //execute the fucntion
            javaCallRFC.client.execute(function);
            JCO.ParameterList outParam = function.getTableParameterList();
            //Get return Table from RFC
            JCO.Table outTable = outParam.getTable("DATA");
            for (int i = 0; i < outTable.getNumRows(); i++) {
                outTable.setRow(i);
                System.out.print(outTable.getString(0));
                System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
====================Code End==============================

=======Properties file content========

jco.client.client=//Client
jco.client.user=//UserName

jco.client.passwd=//Password
jco.client.lang=//language
jco.client.sysnr=//system number
jco.client.ashost=//application server
jco.client.mshost=//message server
jco.client.group=// Group
jco.client.r3name=//R3 Name

 

 

转载于:https://www.cnblogs.com/arden/archive/2010/04/12/1710326.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值