-1

I am writing Java Code to get data from SAP BAPI using Java Connector (JCo). This is my first time to make a connection to SAP using JCo. I was able to get the Tables available in the Data Source and also get one particular Table and Number of Columns using table_name.getNumColumns() which gives me the total count of columns. But when I do, table_name.getNumRows(), it says 0. Where as in my Data source, there are around 85 Rows. How can I get the rows from this table?

The code I have been using:

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);
         }
    }
}
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48

1 Answers1

1

If you would like to get some data from a SAP BAPI it would help a lot also to call this BAPI. The data doesn't materialize automatically in the JCo objects out of thin air.
In your code you do not execute any JCoFunction.

Set the mandatory import parameter values for this BAPI (if there are any), execute the BAPI (your JCoFunction object) and then you will get the export data from the SAP system in response which will then also add appropriate rows to the JCoTable object.

Trixx
  • 1,796
  • 1
  • 15
  • 18