Table参数作为export parameter

BAPI_COMPANYCODE_GETDETAIL是一个适合演示的函数,没有import paramter参数,调用后COMPANYCODE_GETDETAIL 表参数返回SAP系统中所有公司代码的清单。只包括公司代码ID和公司代码名称两个字段。

JCo中,与表参数相关的两个接口是JCoTableJCoRecordMetaDta, JCoTable就是RFM中tabl参数,而JCoRecordMetaDtaJCoTableJCoStructure的元数据。

在.net环境中,我喜欢将IRfcTable转换成DataTable,但Java没有类似的数据结构,所以决定直接在方法中传递JCoTable算了。但为了方便显示,可以考虑使用一个通用代码进行输出:

package jco3.utils;

import com.sap.conn.jco.JCoField;
import com.sap.conn.jco.JCoRecordMetaData;
import com.sap.conn.jco.JCoTable; public class JCoUtils
{
public static void printJCoTable(JCoTable jcoTable)
{
// header // JCoRecordMeataData is the meta data of either a structure or a table.
// Each element describes a field of the structure or table.
JCoRecordMetaData tableMeta = jcoTable.getRecordMetaData();
for(int i = 0; i < tableMeta.getFieldCount(); i++){
System.out.print(String.format("%s\t", tableMeta.getName(i)));
}
System.out.println(); // new line // line items for(int i = 0; i < jcoTable.getNumRows(); i++){
// Sets the row pointer to the specified position(beginning from zero)
jcoTable.setRow(i); // Each line is of type JCoStructure
for(JCoField fld : jcoTable){
System.out.print(String.format("%s\t", fld.getValue()));
}
System.out.println();
}
}
}

要点说明

对JCoTable,输出表头和行项目。表头通过获取JCoTable的meta-data,然后使用meta-data的getName()方法。

JCoRecordMetaData tableMeta = jcoTable.getRecordMetaData();
for(int i = 0; i < tableMeta.getFieldCount(); i++){
System.out.print(String.format("%s\t", tableMeta.getName(i)));
}

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

        for(int i = 0; i < jcoTable.getNumRows(); i++){
// Sets the row pointer to the specified position(beginning from zero)
jcoTable.setRow(i); // Each line is of type JCoStructure
for(JCoField fld : jcoTable){
System.out.print(String.format("%s\t", fld.getValue()));
}
System.out.println();
}

完成输出之后,接下来就是RFM调用:

package jco3.demo5;

import org.junit.Test;
import com.sap.conn.jco.*;
import jco3.utils.JCoUtils; public class JCoTableDemo
{
public JCoTable getCocdList() throws JCoException
{
/**
* Get company code list in SAP
* using BAPI BAPI_COMPANYCODE_GETLIST.
*
* Since JCoTable is rather flexible, we simply use
* this interface as return value
*/ 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 printCompanies() throws JCoException
{
JCoTable companies = this.getCocdList();
JCoUtils.printJCoTable(companies);
}
}

Table参数作为import parameter

table作为输入参数,主要解决填充table的问题,基本模式如下:

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

以RFC_READ_TABLE为例,读取SAP USR04表。

package jco3.demo5;

import org.junit.Test;
import com.sap.conn.jco.*;
import jco3.utils.JCoUtils; public class JCoTableAsImport
{
public JCoTable readTable() throws JCoException
{
/**
* Shows how to process JCoTable (as importing)
*/ JCoDestination dest = JCoDestinationManager.getDestination("ECC");
JCoFunction fm = dest.getRepository().getFunction("RFC_READ_TABLE"); // table we want to query is USR04
// which is user authorization table in SAP
fm.getImportParameterList().setValue("QUERY_TABLE", "USR04"); // output data will be delimited by comma
fm.getImportParameterList().setValue("DELIMITER", ","); // processing table parameters
JCoTable options = fm.getTableParameterList().getTable("OPTIONS");
// modification date >= 2012.01.01 and <= 2015.12.31
options.appendRow();
options.setValue("TEXT", "MODDA GE '20120101' ");
options.appendRow();
options.setValue("TEXT", "AND MODDA LE '20151231' "); // We only care about fields of [user id] and [modification date]
String[] outputFields = new String[] {"BNAME", "MODDA"};
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); JCoTable data = fm.getTableParameterList().getTable("DATA"); return data;
} @Test
public void printUsers() throws JCoException
{
JCoTable users = this.readTable();
JCoUtils.printJCoTable(users);
}
}

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

JCoTable options = fm.getTableParameterList().getTable("OPTIONS");
// modification date >= 2012.01.01 and <= 2015.12.31
options.appendRow();
options.setValue("TEXT", "MODDA GE '20120101' ");
options.appendRow();
options.setValue("TEXT", "AND MODDA LE '20151231' ");

第二种方法:

String[] outputFields = new String[] {"BNAME", "MODDA"};
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]);
}

JCoTable重要方法总结

jcoTable_methods.gif
文/StoneWM(简书作者)
原文链接:http://www.jianshu.com/p/a088510cf965
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

SAP接口编程 之 JCo3.0系列(03) : Table参数的更多相关文章

  1. SAP接口编程 之 JCo3.0系列(01):JCoDestination

    SAP接口编程 之 JCo3.0系列(01):JCoDestination 字数2101 阅读103 评论0 喜欢0 JCo3.0是Java语言与ABAP语言双向通讯的中间件.与之前1.0/2.0相比 ...

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

    SAP接口编程 之 JCo3.0系列(02) : JCo Client Programming 字数545 阅读52 评论0 喜欢1 JCo3.0调用SAP函数的过程 大致可以总结为以下步骤: 连接至 ...

  3. SAP接口编程 之 JCo3.0系列(04) : 会话管理

    在SAP接口编程之 NCo3.0系列(06) : 会话管理 这篇文章中,对会话管理的相关知识点已经说得很详细了,请参考.现在用JCo3.0来实现. 1. JCoContext 如果SAP中多个函数需要 ...

  4. SAP接口编程 之 JCo3.0系列(05) : Exception Handling

    JCO3.0的Exception,常用的Exception如下: JCoException 继承自java.lang.Exception,是JCo3中Exception的基类. JCoRuntimeE ...

  5. 2690036 - SAP HANA 2.0 SPS 03 Database Revision 034

    Symptom This is the SAP Release Note for SAP HANA 2.0 Database Revision 034 (2.00.034.00) of the SAP ...

  6. LXD 2.0 系列(四):资源控制

    LXD 提供了各种资源限制.其中一些与容器本身相关,如内存配额.CPU 限制和 I/O 优先级.而另外一些则与特定设备相关,如 I/O 带宽或磁盘用量限制.-- Stéphane Graber 本文导 ...

  7. Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  8. Spring Boot 2.0系列文章(七):SpringApplication 深入探索

    关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/30/springboot_SpringApplication/ 前言 在 Spring B ...

  9. java接口,接口的特性,接口实现多态,面向接口编程

    package cn.zy.cellphone; /**接口是一种引用数据类型.使用interface声明接口,形式 * 形式:public interface 接口名称{} * 接口不能拥有构造方法 ...

随机推荐

  1. Hibernate,JPA注解@SecondaryTables

    使用类一级的 @SecondaryTable或@SecondaryTables注解可以实现单个实体到多个表的映射. 使用 @Column或者 @JoinColumn注解中的table参数可指定某个列所 ...

  2. 19、文件上传与下载/JavaMail邮件开发

    回顾: 一. 监听器 生命周期监听器 ServletRequestListener HttpSessionListener ServletContextListener 属性监听器 ServletRe ...

  3. [算法][C]计算向量的角度

    C 语言里 double atan2(double y,double x) 返回的是原点至点(x,y)的方位角,即与 x 轴的夹角.也可以理解为复数 x+yi 的辐角.返回值的单位为弧度,取值范围为 ...

  4. python 补充-decode和encode

    1. decode与encode转码 在Python3中默认编码就是uncode,encode转成Byte类型 在Python2中默认编码就是ascii window下默认编码是GBK decode( ...

  5. 【转】西门子数控系统中MMC、PCU、NCU、CCU简略介绍

    转载地址:http://cyj221.blog.163.com/blog/static/34194117201093005526170/ 2010-10-30 01:06:09|  分类: 机械制造 ...

  6. 20150602_Andriod 向窗体传递参数

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:too ...

  7. Gift Hunting(分组背包)

    Gift Hunting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. 在opencv中实现中文输出

    http://pan.baidu.com/s/1hrQTWDe 已经成功 ; 来自为知笔记(Wiz)

  9. java中|与||,&与&&到底有什么区别呢?

    &是位运算符.&&是布尔逻辑运算符.在运行上,&两边的条件都要判断(不管前面的是ture还是false),而&&先判断前面的,若为false,则后面的不 ...

  10. combotree的加载方法

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...