SAP接口编程 之 JCo3.0系列(03) : 表参数

本文详细介绍了SAP JCo3中Table参数的使用方法,包括Table作为exporting和importing参数的应用实例。通过具体函数调用展示了如何操作Table参数,并提供了输出Table数据到控制台的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Table 作为 exporting parameter

Table parameter 既可以作为 exporting parameter,也可以作为 importing parameter,作为 exporting parameter 比较简单。以 BAPI_COMPANYCODE_GETLIST 函数为例,该函数无 importing parameter, 使用 table parameter 返回 SAP 系统中包括公司代码 ID 和公司代码名称两个字段的清单。JCo3.0 中,与表参数 (table parameter) 相关的接口:

  • JCoTable: 对应 table parameter
  • JCoRecordMetaDta: JCoTableJCoStructure 的元数据。

JCoTable 输出到控制台

首先编写一个将 JCoTable 输出到控制台的辅助类。

package jco3.jcoUtils;

import com.sap.conn.jco.*;

public class Utils {
    public static void printJcoTable(JCoTable table) {
        printHeader(table);
        printLines(table);
    }

    private static void printHeader(JCoTable table) {
        // JCoRecordMeataData is the meta data of either a structure or a table.
        // Each element describes a field of the structure or table.
        JCoRecordMetaData  tableMeta = table.getRecordMetaData();
        for (int i = 0; i < tableMeta.getFieldCount(); i++){
            System.out.print(
                    String.format("%s\t", tableMeta.getName(i))
            );
        }
        System.out.println(); // new line
    }

    private static void printLines(JCoTable table) {
        for (int i = 0; i < table.getNumRows(); i++){
            // set current row position (starting from 0)
            table.setRow(i);

            // Every row is of type JCoStructure
            // iterate each field in a row
            for (JCoField field : table){
                System.out.print(
                        String.format("%s\t",  field.getValue())
                );
            }
            System.out.println();
        }
    }
}

要点说明

输出表头,通过获取JCoTable的 meta-data,然后使用 meta-data 的 getName() 方法:

JCoTable 每一行都是一个 JCoStructure,可以通过 setRow() 设置指针的位置,然后再遍历各个 field:

Table 作为 exporting 参数示例

package jco3.demo6;

import com.sap.conn.jco.*;
import jco3.jcoUtils.Utils;
import org.junit.Test;

public class TableAsExportingDemo {
    public JCoTable getCocdList() throws JCoException {
        JCoDestination dest = JCoDestinationManager.getDestination("ECC");
        JCoFunction fm = dest.getRepository().getFunction("BAPI_COMPANYCODE_GETLIST");
        fm.execute(dest);

        JCoTable companies = fm.getTableParameterList().getTable("COMPANYCODE_LIST");

        return companies;
    }

    @Test
    public void testGetCocdList() throws JCoException {
        JCoTable companies = getCocdList();
        Utils.printJcoTable(companies);
    }
}

Table 作为 importing parameter

table 作为输入参数,需要在代码中填充 table,方法如下:

someTable.appendRow();
someTable.setValue("FLDNAME", someValue);

RFC_READ_TABLE 函数为例,读取 spfli (SAP 航空示例数据)表。

package jco3.demo6;

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

public class TableAsImportingDemo {
    public JCoTable readTable() throws JCoException {
        JCoDestination dest = JCoDestinationManager.getDestination("ECC");
        JCoFunction fm = dest.getRepository().getFunction("RFC_READ_TABLE");

        // QUERY_TABLE: importing, table name to be queried
        fm.getImportParameterList().setValue("QUERY_TABLE", "SPFLI");

        // DELIMITER: delimiter of output data
        fm.getImportParameterList().setValue("DELIMITER", ",");

        // OPTIONS: Selection entries, valid "WHERE clauses"
        // needing to be populated
        JCoTable options = fm.getTableParameterList().getTable("OPTIONS");

        options.appendRow();
        options.setValue("TEXT", "COUNTRYFR EQ 'US' ");

        options.appendRow();
        options.setValue("TEXT", " OR COUNTRYFR EQ 'DE' ");

        // FIELDS: names to be output in the structure, needing to be specified
        // if we want to restrict, otherwise all fields will be extracted
        String[] outputFields = new String[] {"CARRID", "COUNTRYFR", "CITYFROM", "CITYTO" };
        JCoTable fields = fm.getTableParameterList().getTable("FIELDS");

        int count = outputFields.length;
        fields.appendRows(count);
        for (int i = 0; i < count; i++){
            fields.setRow(i);
            fields.setValue("FIELDNAME", outputFields[i]);
        }

        fm.execute(dest);

        // Get data
        JCoTable data = fm.getTableParameterList().getTable("DATA");

        return data;
    }

    @Test
    public void testReadTable() throws JCoException {
        JCoTable data = readTable();

        for(int i = 0; i < data.getNumRows(); i++){
            data.setRow(i);
            String rowData = data.getValue("WA").toString(); // get value from first column
            String[] values = rowData.split(",");
            for(String item : values) {
                System.out.print(item + "\t");
            }
            System.out.println();
        }
    }
}

在代码中我们使用了两种方法来插入 table 的行项目,第一种方法:

第二种方法:

JCoTable 重要方法

源码

github - sap_interface_jco3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值