【问题标题】:getNumRows() returns 0 when running SAP BAPI using JCo SAP使用 JCo SAP 运行 SAP BAPI 时,getNumRows() 返回 0
【发布时间】:2019-02-15 07:22:34
【问题描述】:

我正在编写 Java 代码以使用 Java 连接器 (JCo) 从 SAP BAPI 获取数据。这是我第一次使用 JCo 连接到 SAP。我能够获得数据源中可用的表,还可以使用 table_name.getNumColumns() 获得一个特定的表和列数,这给了我列的总数。但是当我这样做时,table_name.getNumRows(),它会显示 0。在我的数据源中,大约有 85 行。如何从该表中获取行?

我一直在使用的代码:

public class SapConnection {

      public static void gettingTableData(JCoFunction function) {

          JCoParameterList table_list = function.getTableParameterList();
          JCoTable my_table = function.getTableParameterList().getTable("SOME_TABLE");

          System.out.println("Field Count: "+my_table.getFieldCount());

          // This is not working as Number of Rows is 0.
          for(int i = 0; i<my_table.getNumRows(); i++, my_table.nextRow()) {
              // get those rows and do something ..
          }

          System.out.println("Is Empty: "+my_table.isEmpty()); // returns True
          System.out.println("Is First Row: "+my_table.isFirstRow()); // returns false
          System.out.println("Next Row: "+my_table.nextRow()); // returns false
          System.out.println("Num Rows: "+my_table.getNumRows()); // returning 0

        }

      public static void loadDataSourceAndGetData(JCoDestination dest) throws JCoException {

       JCoRepository sapRepository = dest.getRepository();
       JCoFunctionTemplate template = 
           sapRepository.getFunctionTemplate("DATA_SOURCE_NAME");
       JCoFunction my_function = template.getFunction();

       gettingTableData(my_function);

  }

     public static void main(String[] args) throws JCoException {
         // get the Properties created for connection.
         Properties pp = getJcoProperties();
         PropertiesDestinationDataProvider pddp = new PropertiesDestinationDataProvider(pp);
         Environment.registerDestinationDataProvider(pddp);

         JCoDestination dest = getDestination();

         try {
             // Using JCo Context for stateful function calls to Start() and End()
             JCoContext.begin(dest);
             loadDataSourceAndGetData(dest);
             JCoRepository sapRepository = dest.getRepository();
             System.out.println(sapRepository.getMonitor().getLastAccessTimestamp());
         } finally {
             // end the connection.
             JCoContext.end(dest);
         }
    }
}

【问题讨论】:

  • 如果可能,尝试在 SAP 端执行函数以验证返回的行数
  • 嘿@DarkKnight 你的意思是使用ABAP执行吗?
  • 是的,正是我想说的
  • 当然@DarkKnight 我会试试的。感谢您的回复。
  • 嗨@DarkKnight,我们已经使用ABAP 实现并且它正在工作。 IDK 为什么它不能在 Java 中工作。 :(

标签: java sap jco bapi


【解决方案1】:

如果您想从 SAP BAPI 获取一些数据,调用此 BAPI 也会有很大帮助。数据不会凭空自动在 JCo 对象中实现。
在您的代码中,您不执行任何 JCoFunction。

为此 BAPI 设置强制导入参数值(如果有),执行 BAPI(您的 JCoFunction 对象),然后您将从 SAP 系统获取导出数据作为响应,然后还会将适当的行添加到JCoTable 对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多