cancel
Showing results for 
Search instead for 
Did you mean: 

How to retrieve table values from RFC using JSP.

Former Member
0 Kudos

*Please guide me in the following process.*

*We are trying to fetch data from RFC through java program.*

*Until now we have used the Jco connection code and also the code to call the RFC.*

*The problem is, if the RFC contains 10 rows in it we are not able to get all the values.*

<%--

Document : index

Created on : 22 Jun, 2011, 2:19:06 PM

Author : J.Francisnoel

--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<%@ page language="java" %>

<%@page import="com.sap.mw.jco.*"%>

<HTML>

<BODY class=bg MARGINWIDTH="0" MARGINHEIGHT="0" LEFTMARGIN="0" TOPMARGIN="0" BGCOLOR="#FFFFFF">

<%

// The MySAP.com system we gonna be using

String SID = "ABC";

// The repository we will be using

IRepository repository;

try {

// Add a connection pool to the specified system

JCO.addClientPool(SID, // Alias for this pool

10, // Max. number of connections

"180", // SAP client

"USERID", // userid

"PASSWORD", // password

"EN", // language

"HOSTNAME",// host name

"30");

// Create a new repository

repository = JCO.createRepository("MYRepository", SID);

}

catch (JCO.Exception ex) {

System.out.println("Caught an exception: \n" + ex);

}

repository = JCO.createRepository("MYRepository", SID);

try {

// Get a function template from the repository

IFunctionTemplate ftemplate = repository.getFunctionTemplate("YABAP_FM4");

// Create a function from the template

JCO.Function function = new JCO.Function(ftemplate);

// Get a client from the pool

JCO.Client client = JCO.getClient(SID);

// We can call 'RFC_SYSTEM_INFO' directly since it does not need any input parameters

client.execute(function);

// The export parameter 'RFCSI_EXPORT' contains a structure of type 'RFCSI'

// JCO.Structure s = function.getExportParameterList().getStructure("S_VBAK"); the structure value is obtained

JCO.Table s = function.getTableParameterList().getTable("T_VBAK") ; Problem here is , the table has 10 rows but only one row is fetched!

// Use enumeration to loop over all fields of the structure

%>

<H1>System info for <%= SID %></H1>

<H1>RFC called is "YABAP_FM4"</H1>

-


<BR>

<%

for (JCO.FieldIterator e = s.fields(); e.hasMoreElements(); )

//for( JCO. e = s.getRow("5"); e.hasMoreElements(); )

{

JCO.Field field = e.nextField();

%>

<%= field.getName() %> : <%= field.getString() %><BR>

<%

}//for

%>

<BR>

<%

// Release the client into the pool

JCO.releaseClient(client);

}

catch (Exception ex) {

System.out.println("Caught an exception: \n" + ex);

}

%>

</BODY>

</HTML>

Can u give me sample code for fetching the table values.

ALSO can you help us display the table datau2019s fetched in a table or grid format in the JSP.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos
Former Member
0 Kudos

Hi i checked the link.

But the sample code has a structure called before we set the table parameters..

I want to directly call the table in RFC and the datas,ie.. the records in the table should be obtained.

In the present code we have the table output as only one record is obtained.not able to retrieve the remaining records.

I have the problem in table display.

i think there should be a loop.

can u help me.

Former Member
0 Kudos

Below should work for you :

JCO.Table myTable = function.getTableParameterList.getTable("TABLE_NAME");
int numberOfRows;
if  ((myTable != null) && ((numberOfRows = myTable.getNumRows()) > 0))
 {
   HashMap myMap = new HashMap();
   // Here Comes your loop
   for(int i=0;i<numberOfRows;i++)
    {
       myMap.put(myTable.getString("Col1"),myTable.getString("Col2"));
       myTable.newxtRow();
    }
 }

Former Member
0 Kudos

I'm getting error in this line

myTable.newxtRow();

also what will be the col1 and col2 values?

Edited by: crimsonnoel on Jun 27, 2011 8:55 AM

Former Member
0 Kudos

JCO.Table s = function.getTableParameterList().getTable("T_VBAK") ;

// Use enumeration to loop over all fields of the structure

for (JCO.FieldIterator e = s.fields(); e.hasMoreElements(); )

{

JCO.Field field = e.nextField();

<%= field.getName() %> : <%= field.getString() %><BR>

}//for

The above code gives only one first table value. but i need all the values from the table.

It will be help ful if i get the table loop solved

Former Member
0 Kudos

Hello,

It is a small typing error :

change myTable.newxtRow(); to myTable.nextRow();

Col1, Col2 are your fields in table. If you have multiple fields then .....make a bean object from those fields and the place them in some arraylist...

Additionaly in your code :

You are accessing all fields in 1 row.

Thus you are not able to get other rows.

Proper way of using field iterator is :

JCO.Table tableList = function.getTableParameterList().getTable("DATA");
  
  if (tableList.getNumRows() > 0) {
   do {
    for (JCO.FieldIterator fI = tableList.fields();
      fI.hasMoreElements();)
     {
      JCO.Field tabField = fI.nextField();
      System.out.println(tabField.getName()
           + ":t" +
           tabField.getString());
     }
     System.out.println("n");
   }
   while (tableList.nextRow() == true);
  }

Former Member
0 Kudos

THANK YOU... I"M getting the records retrieved...and displayed in HTML..

have to format it.

Answers (0)