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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容