Table 作為 exporting parameter
Table parameter 既可以作為 exporting parameter,也可以作為 importing parameter踢京,作為 exporting parameter 比較簡單仅父。以 BAPI_COMPANYCODE_GETLIST
函數(shù)為例搪泳,該函數(shù)無 importing parameter楚殿, 使用 table parameter 返回 SAP 系統(tǒng)中包括公司代碼 ID 和公司代碼名稱兩個字段的清單哮兰。JCo3.0 中贺纲,與表參數(shù) (table parameter) 相關(guān)的接口:
-
JCoTable
: 對應(yīng) table parameter -
JCoRecordMetaDta
:JCoTable
或JCoStructure
的元數(shù)據(jù)航闺。
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();
}
}
}
要點(diǎn)說明:
輸出表頭猴誊,通過獲取JCoTable
的 meta-data潦刃,然后使用 meta-data 的 getName()
方法:
JCoTable 每一行都是一個 JCoStructure
,可以通過 setRow()
設(shè)置指針的位置懈叹,然后再遍歷各個 field:
Table 作為 exporting 參數(shù)示例
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 作為輸入?yún)?shù)乖杠,需要在代碼中填充 table,方法如下:
someTable.appendRow();
someTable.setValue("FLDNAME", someValue);
以 RFC_READ_TABLE
函數(shù)為例澄成,讀取 spfli (SAP 航空示例數(shù)據(jù))表胧洒。
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 的行項目,第一種方法:
第二種方法: