Problem solve Get help with specific problems with your technologies, process and projects.

Use Java hashtables to emulate an SAP table

Here's how to translate JCo specific table returns into the more generic Java Hashtable.

Although JCo solves the larger problem of connectivity into SAP, it still relies on a fairly specific understanding of R/3 table structures. As an SAP/Java developer, part of your task is to allow non-SAP Java developers to develop applications using your Java libraries easily. One of the simplest ways to do this is to translate JCo specific table returns into the more generic Java Hashtable.

In Java, Hashtables offer an easy mechanism for storing key/value pairs in an array-style structure. Moreover, Hashtables can be nested in other Hashtables, allowing multiple "rows" of data to be stored in a single Hashtable.

The following code retrieves all the field names and field values from a JCo table. These field name/value pairs are stored as key/value pairs in a Hashtable. When the end of a row is reached, that Hashtable is stored into a higher-level Hashtable, which acts as the table container.

    JCO.Table materials = function.getTableParameterList().getTable("MATNRLIST");

    Hashtable returnHash = new Hashtable();
    Hashtable rowHash = new Hashtable();
    int i = 0;
    if (materials.getNumRows() > 0) {
        do {
            for (JCO.FieldIterator fI = materials.fields();
                JCO.Field tabField = fI.nextField();
               returnHash.put("line" + i, rowHash);
               rowHash = new Hashtable();
            while(materials.nextRow() == true);
    else {
          System.out.println("No materials.");

In this code, you create two Hashtables. returnHash is the table container and rowHash is a temporary Hashtable that will maintain a row before it is stored in returnHash.

The if statement checks whether any rows have been return in the MATNRLIST table then executes a do loop. This loop retrieves the fields in a JCo FieldIterator and puts the field name and value into the temporary rowHash Hashtable.

Finally, this temporary Hashtable is stored in the returnHash container using an incremented value as the line number. When the loops have exited you end up with a fully populated table container maintaining a series of Hashtable rows.

To get at that data use the following code:

int i = 0;
String field;
int maxValue = rowHash.size();
while (i < maxValue)
 rowHash = (Hashtable)returnHash.get("line" + i);
 for (Enumeration e = rowHash.keys(); e.hasMoreElements();) {
  field = (String)e.nextElement();
  out.println(field + ": " + (String)rowHash.get(field));

Author Austin Sincock is a freelance Java/SAP consultant who contributes regularly to Web and print journals. He can be reached at Check out his upcoming book Enterprise Java for SAP

Dig Deeper on SAP development and programming languages

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.