gpt4 book ai didi

java - 使用 JCo 从 SAP 系统读取表

转载 作者:行者123 更新时间:2023-12-01 14:14:27 24 4
gpt4 key购买 nike

我正在尝试从 SAP 系统读取表,但总是收到此错误:

Exception in thread "main" com.sap.conn.jco.JCoRuntimeException: (127) 
JCO_ERROR_FIELD_NOT_FOUND: Field EMPLOYEE is not a member of INPUT
at com.sap.conn.jco.rt.AbstractMetaData.indexOf(AbstractMetaData.java:404)
at com.sap.conn.jco.rt.AbstractRecord.setValue(AbstractRecord.java:4074)
at testConf.StepServer.main(StepServer.java:50)

这是我的代码:
public static void main(String[] args) {

// This will create a file called mySAPSystem.jcoDestination
System.out.println("executing");
String DESTINATION_NAME1 = "mySAPSystem";

Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "xxx.xxx.x.xxx");
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "xx");
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "xxx");
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "username");
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "test");
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "en");
createDestinationDataFile(DESTINATION_NAME1, connectProperties);

// This will use that destination file to connect to SAP
try {
JCoDestination destination = JCoDestinationManager.getDestination("mySAPSystem");
System.out.println("Attributes:");
System.out.println(destination.getAttributes());
System.out.println();
destination.ping();
} catch (JCoException e) {
e.printStackTrace();
}
try{

//here starts the problem

JCoDestination destination = JCoDestinationManager.getDestination(DESTINATION_NAME1);
JCoFunction function = destination.getRepository().getFunction("RFC_READ_TABLE");
JCoParameterList listParam = function.getImportParameterList();

listParam.setValue("EMPLOYEE", "EMPLOYEE"); // I have found this in an example and I don't understand exactly what should I put there
// I was thinking maybe is the column name but I am not sure
function.execute(destination);

JCoTable table = function.getTableParameterList().getTable("ZEMPLOYEES");//name of my table from SAP

System.out.println(table);

}
catch (JCoException e)
{
System.out.println(e.toString());
return;
}
}

当它说 时,错误很明显JCO_ERROR_FIELD_NOT_FOUND:字段 EMPLOYEE 不是 INPUT 的成员但员工是我表中的一个字段。

enter image description here

该文档没有太大帮助,它只说:
Sets the object as the value for the named field.
Parameters:
value - the value to set for the field
name - the name of the field to set

女巫,在我看来,我已经做到了。

我应该在 sap 中进行任何额外的修改,以便从 java 中读取这个新表吗?我所做的只是按照本教程( Create a simple table in SAP)创建一个新表。

也许有更多经验的人可以告诉我应该如何配置这个示例代码才能工作。

最佳答案

RFC_READ_TABLE 的一般用途

我从未使用过 JCo,但据我所知,它的接口(interface)与 .Net 连接器 NCo 非常相似。这基本上是添加了一些猜测的 NCo 代码,但它应该可以工作。

// get the table parameter FIELDS that should be in the parameter list
// the parameter table has several fields, only the field FIELDNAME has to be set before calling the function module
JCOTable inputTableParam = function.getTableParameterList().getTable("FIELDS");

// add a row to the FIELDS table parameter
inputTableParam.appendRow();

// set values for the new row
inputTableParam.setValue("FIELDNAME", "EMPLOYEE");
// just for fun, add another field to retrieve
inputTableParam.appendRow();
inputTableParam.setValue("FIELDNAME", "SURNAME");

// now we have to set the non-table parameters
JCoParameterList inputParamList = function.getImportParameterList();
// parameter QUERY_TABLE, defines which table to query
inputParamList.setValue("QUERY_TABLE", "ZEMPLOYEES");
// parameter DELIMITER - we get a single string as the return value, the field values within that string are delimited by this character
inputParamList.setValue("DELIMITER", ";");

// execute the function
function.execute(destination);

// the parameter table DATA contains the rows
JCoTable table = function.getTableParameterList().getTable("DATA");

最后,你的变量 table将保存一个表对象,其中包含一个名为 WA 的字段。 .该字段包含您在输入参数表 FIELDS 中选择的字段的内容。 .您可以遍历 table并逐行获取值。

使用 RFC_READ_TABLE 的查询
RFC_READ_TABLE并不真正允许查询,它只允许您定义 WHERE条款。 TABLE参数 OPTIONS有一个字段 TEXT , 72 个字符宽,只能取 ABAP符合 WHERE条款。

为了扩展上面的示例,我们将添加一个 where 子句来仅从表 ZEMPLOYEES 中选择条目与 SURNAME = "史密斯"和 FORNAME =“约翰”。
JCOTable optionsTableParam = function.getTableParameterList().getTable("OPTIONS");

// add a row to the FIELDS table parameter
optionsTableParam.appendRow();
optionsTableParam.setValue("TEXT", "SURNAME EQ 'SMITH' AND FORNAME EQ 'JOHN');

现场 TEXT仅 72 个字符长,因此如果要添加更长的子句,您必须手动将条件分成几行。 RFC_READ_TABLE有点粗糙和有限。

可以通过在 SAP 系统中创建 View (事务 SE11)来实现表之间的复杂连接,然后使用 RFC_READ_TABLE 查询该 View 。

如果您想从 JCo 调用功能模块,如果您熟悉基本功能模块属性将非常有帮助。您可以查看事务 SE37中的功能模块定义.在那里你可以看到 IMPORT , EXPORT , CHANGINGTABLE参数。您必须填写的参数以及包含结果的参数取决于您调用的功能模块 - RFC_READ_TABLEBAPI_DELIVERY_GETLIST 不同.

这是 JCoFunction 的文档以及 JCo 和 NCo 之间的区别之一,JCo 具有获取和设置不同参数类型的单独函数: https://help.hana.ondemand.com/javadoc/com/sap/conn/jco/JCoFunction.html

关于java - 使用 JCo 从 SAP 系统读取表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52201563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com