SAP接口编程 之 JCo3.0系列(03) : Table参数
2016-07-14 17:34
501 查看
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重要方法总结
jcoTable_methods.gif
文/StoneWM(简书作者)
原文链接:http://www.jianshu.com/p/a088510cf965
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
相关文章推荐
- github基础命令
- Java解读内存,优化编程
- QAbstractListModel:Qml 使用C++model
- 几个著名java开源缓存系统的介绍
- 完全理解Python迭代对象、迭代器、生成器
- 使用了google zxing作为二维码生成工具
- Python解析excel文件并存入sqlite数据库
- java递归实现二分查找算法
- jdk1.7和jdk1.8区别
- java递归实现二分查找算法
- Leetcode 67. Add Binary (Easy) (cpp)
- 使用JDOM方式解析XML
- 《文件传输基础----Java IO流---《一》》
- python笔记 - day4-之装饰器
- C/C++ sort函数的用法
- 批量下载文件
- 运行phpmyadmin的时候下方总是出现“wapm您配置文件中的设置 (空密码的 root) 与 MySQL 默认管理员账户对应。您的 MySQL 服务器使用默认值运行
- python简要
- SpringMVC拦截器
- 使用Eclipse进行远程调试