SAP接口编程 之 JCo3.0系列(03) : Table参数
Table参数作为export parameter
BAPI_COMPANYCODE_GETDETAIL是一个适合演示的函数,没有import paramter参数,调用后COMPANYCODE_GETDETAIL 表参数返回SAP系统中所有公司代码的清单。只包括公司代码ID和公司代码名称两个字段。
JCo中,与表参数相关的两个接口是JCoTable和JCoRecordMetaDta, JCoTable就是RFM中tabl参数,而JCoRecordMetaDta是JCoTable或JCoStructure的元数据。
在.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重要方法总结
原文链接:http://www.jianshu.com/p/a088510cf965
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
SAP接口编程 之 JCo3.0系列(03) : Table参数的更多相关文章
- SAP接口编程 之 JCo3.0系列(01):JCoDestination
SAP接口编程 之 JCo3.0系列(01):JCoDestination 字数2101 阅读103 评论0 喜欢0 JCo3.0是Java语言与ABAP语言双向通讯的中间件.与之前1.0/2.0相比 ...
- SAP接口编程 之 JCo3.0系列(02) : JCo Client Programming
SAP接口编程 之 JCo3.0系列(02) : JCo Client Programming 字数545 阅读52 评论0 喜欢1 JCo3.0调用SAP函数的过程 大致可以总结为以下步骤: 连接至 ...
- SAP接口编程 之 JCo3.0系列(04) : 会话管理
在SAP接口编程之 NCo3.0系列(06) : 会话管理 这篇文章中,对会话管理的相关知识点已经说得很详细了,请参考.现在用JCo3.0来实现. 1. JCoContext 如果SAP中多个函数需要 ...
- SAP接口编程 之 JCo3.0系列(05) : Exception Handling
JCO3.0的Exception,常用的Exception如下: JCoException 继承自java.lang.Exception,是JCo3中Exception的基类. JCoRuntimeE ...
- 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 ...
- LXD 2.0 系列(四):资源控制
LXD 提供了各种资源限制.其中一些与容器本身相关,如内存配额.CPU 限制和 I/O 优先级.而另外一些则与特定设备相关,如 I/O 带宽或磁盘用量限制.-- Stéphane Graber 本文导 ...
- Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例
java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...
- Spring Boot 2.0系列文章(七):SpringApplication 深入探索
关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/30/springboot_SpringApplication/ 前言 在 Spring B ...
- java接口,接口的特性,接口实现多态,面向接口编程
package cn.zy.cellphone; /**接口是一种引用数据类型.使用interface声明接口,形式 * 形式:public interface 接口名称{} * 接口不能拥有构造方法 ...
随机推荐
- Table Properties [AX 2012]
Table Properties [AX 2012] 1 out of 2 rated this helpful - Rate this topic Updated: July 20, 2012 Ap ...
- inupt textarea提示文字(点击消失,不输入恢复)
<input name="textfield" type="text" maxlength="20" value="请输入 ...
- vmdk虚拟机转换为OVF模板,导入esxi
VMware WorkStation安装目录下,有一个OVFTool文件夹,例如我电脑上的路径为:D:\VMware\VMware\OVFTool.通过CMD进入到命令行模式,更改到该目录下,运行如下 ...
- 15、Jdbc的优化(BeanUtils组件)
Jdbc的优化! BeanUtils组件 自定义一个持久层的框架 DbUtils组件 案例优化 1. BeanUtils组件 1.1 简介 程序中对javabean的操作很频繁, 所以apach ...
- SQL Server 2005 日志文件过大处理
由于安装的时候没有计划好空间,默认装在系统盘,而且又没有做自动备份.截断事务日志等,很快LDF文件就达到十几G,或者几十G ,此时就不得不处理了. 备份和计划就不说了,现在就说下怎么把它先删除吧: 1 ...
- javascript 中==和===的区别
对于JavaScript中比较运算符,可能大家用的比较多的是“==”.对于“===”很多人可能很陌生.=== 表示恒等,首先比较两边的变量数据类型是否相等,其次比较两边的变量的数值是否相等:= ...
- a different object with the same identifier,同一个session中存在不同的对象问题
使用hibernate的函数 session.merge()函数,提交处于游离态的对象. merge在执行更新之前会将两个标识符相同的对象进行合并,具体合并的方向是向exituser2合并.
- raw_input() 与 input()
这两个均是 python 的内建函数,通过读取控制台的输入与用户实现交互.但他们的功能不尽相同. >>> raw_input_A = raw_input("raw_inpu ...
- Hiking 分类: 比赛 HDU 函数 2015-08-09 21:24 3人阅读 评论(0) 收藏
Hiking Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Subm ...
- 2016 Al-Baath University Training Camp Contest-1 F
Description Zaid has two words, a of length between 4 and 1000 and b of length 4 exactly. The word a ...