无法使用JCo SAP从表中获取行

时间:2019-02-15 07:22:35

标签: java sap jco bapi

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

以下是我一直在使用的代码:(对错误代码表示歉意)

import ....;
import ....;

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);
         }
    }
}

1 个答案:

答案 0 :(得分:0)

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

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