当使用 JCo SAP 运行 SAP BAPI 时,getNumRows() 返回 0。

问题描述 投票:-1回答:1

我正在编写Java代码来从 SAP BAPI 使用 Java连接器(JCo). 这是我第一次使用JCo与SAP进行连接。我能够在数据源中获得可用的表,也能够使用JCo获得一个特定的表和列数。表_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);
         }
    }
}
java sap jco bapi
1个回答
1
投票

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

为这个BAPI设置强制性的导入参数值(如果有的话),执行BAPI(你的JCoFunction对象),然后你将从SAP系统中得到响应的导出数据,这也将添加适当的行到JCoTable对象中。

© www.soinside.com 2019 - 2024. All rights reserved.