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 接口名称{} * 接口不能拥有构造方法 ...
随机推荐
- 严重: IOException while loading persisted sessions: java.io.EOFException
tomcat在启动时出现如下异常问题: 严重: IOException while loading persisted sessions: java.io.EOFException 严重: Excep ...
- WKWebView新特性及JS交互
引言 一直听说WKWebView比UIWebView强大许多,可是一直没有使用到,今天花了点时间看写了个例子,对其API的使用有所了解,为了日后能少走弯路,也为了让大家更容易学习上手,这里写下这篇文章 ...
- DockerUI安装、使用
虽然大多数开发人员和管理人员通过命令行来创建及运行Docker容器,但Docker的Remote API让他们可以通过充分利用REST(代表性状态传输协议)的API,运行相同的命令.这时,Docker ...
- java利用zxing编码解码一维码与二维码
最近琢磨了一下二维码.一维码的编码.解码方法,感觉google的zxing用起来还是比较方便. 本人原创,欢迎转载,转载请标注原文地址:http://wallimn.iteye.com/blog/20 ...
- HDU 1715 大菲波数
大菲波数 问题描述 : Fibonacci数列,定义如下: f(1)=f(2)=1 f(n)=f(n-1)+f(n-2) n>=3. 计算第n项Fibonacci数值. 输入: 输入第一行为一 ...
- EF查询分页
static List<T> GetPageList(Func<T,bool> whereLambda,Func<T,object> orderLambda,int ...
- java 用socket制作一个简易多人聊天室
代码: 服务器端Server import java.io.*; import java.net.*; import java.util.ArrayList; public class Server{ ...
- Ugly Numbers
Ugly Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21918 Accepted: 9788 Descrip ...
- JAVA基础知识之数据类型
JAVA的数据类型知识点主要包括基本数据类型,包装类,字符串类(String,StringBuffer, StringBuilder区别和用法),数组,数据类型转换等等,暂时只想到这么多,后面会再补充 ...
- 1. WP8.1学习笔记
数据绑定 含义:将对象绑定到控件上 2.基本名词 控件:绑定目标 对象:绑定源(数据源) 控件与对象属性的联系:路径 如何绑定 创建对象,设置控件 在控件需要数据绑定的地方使用拓展语法 <But ...