SAP接口编程 之 JCo3.0系列(02) : JCo Client Programming

JCo3.0 调用 SAP 函数的步骤

大致可以总结为以下步骤:

  • 连接至SAP系统
  • 创建 JcoFunction 接口的实例(这个实例代表 SAP 系统中相关函数)
  • 设置参数 (importing 等)
  • 调用函数
  • 从 exporting 参数或者 table 参数获取数据

以 BAPI_COMPANYCODE_GETDETAIL 函数为例的示例代码:

package jco3.demo5;

import com.sap.conn.jco.*;
import org.junit.Test;

public class FunctionCallDemo {

    public void getCoCdDetail(String cocd) throws JCoException {
        // JCoDestination : backend SAP system
        JCoDestination dest = JCoDestinationManager.getDestination("ECC");

        JCoRepository repository = dest.getRepository();

        JCoFunction fm = repository.getFunction("BAPI_COMPANYCODE_GETDETAIL");
        if (fm == null) {
            throw new RuntimeException("Function does not exist in SAP");
        }

        // Set importing parameters
        fm.getImportParameterList().setValue("COMPANYCODEID", cocd);

        fm.execute(dest);

        JCoStructure cocdDetail = fm.getExportParameterList()
                                    .getStructure("COMPANYCODE_DETAIL");

        this.printStructure(cocdDetail);

    }

    private void printStructure(JCoStructure stru){
        for (JCoField field : stru){
            System.out.println(String.format("%s \t %s", field.getName(), field.getString()));
        }
    }

    @Test
    public void testGetCocdDetail() throws JCoException {
        this.getCoCdDetail("Z900");
    }
}

JCoFunction 接口

  • JCoFunction 是接口,代表 SAP 系统的函数
  • JCoFunction 包含 importing 参数,exporting 参数,changing 参数,table 参数等,分别使用getImportParameterList() 方法,getExportParameterList() 方法,getChangingParameterList() 方法和 getTableParameterList() 方法获取。这些方法的返回值都为 JCoParameter 类型
  • JCoFunction.execute() 方法调用函数

创建 JCoFunction 对象的三种方法

方法一:

JCoRepository repository = dest.getRepository();
JCoFunction fm = dest.getRepository().getFunction("BAPI_COMPANYCODE_GETDETAIL");

方法二:如果我们不关心 JCoRepository,也可以这样写:

JCoFunction fm = dest.getRepository().getFunction("BAPI_COMPANYCODE_GETDETAIL");

方法三: 使用 JCoFunctionTemplate.getFunction() 方法,JCoFunctionTemplate 也是一个接口,代表 SAP 函数的 meta-data。

JCoFunctionTemplate fmTemplate
			= dest.getRepository().getFunctionTemplate("BAPI_COMPANYCODE_GETDETAIL");
JCoFunction fm = fmTemplate.getFunction();

JCoStructure 接口

BAPI_COMPANY_CODE_GETDETAIL 函数的 COMPANYCODE_DETAIL 参数是一个结构 - JCoStructure,JCoStructure 实现了 Iterable 接口,所以可以使用 for 循环遍历

private void printStructure(JCoStructure stru)
{
	for(JCoField field : stru){
		System.out.println(String.format("%s\\t%s", 
				field.getName(), 
				field.getString()));
	}
}

也可以使用 getFieldCount() 方法进行遍历:

private void printStructure2(JCoStructure jcoStructure)
{
	for (int i = 0; i < jcoStructure.getMetaData().getFieldCount(); i++){
		System.out.println(String.format("%s\\t%s", 
				jcoStructure.getMetaData().getName(i),
				jcoStructure.getString(i)));
	}
}

源码

github - sap_interface_jco3

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值