Tip

Use Java hashtables to emulate an SAP table

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();
                 fI.hasMoreElements();)
              {
                JCO.Field tabField = fI.nextField();
                rowHash.put(tabField.getName(),tabField.getString());
  }
               returnHash.put("line" + i, rowHash);
               rowHash = new Hashtable();
 

    Requires Free Membership to View

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

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

This was first published in November 2002

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.