Home > SAP software/management Tips > SAP ABAP/Java developer tips > Use Java hashtables to emulate an SAP table
SAP Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

SAP ABAP/JAVA DEVELOPER TIPS

Use Java hashtables to emulate an SAP table


Austin Sincock
11.11.2002
Rating: -3.00- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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

Rate this Tip
To rate tips, you must be a member of SearchSAP.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
SAP ABAP/Java developer tips
How to do additional dialog processing after SAP COMMIT WORK statement
How to find a piece of SAP ABAP code without debugging
How to read an SAP transaction in an ABAP code
How to provide an SAP R/3 4.5B application server with a Web service interface
How to find owners and transports of deleted ABAP programs
Fixing a common OPEN_FORM and START_FORM error in SAPscript
Select Text fields: Case-insensitive
Is this the quickest way to find a BADI?
Easily debug error messages in SAP processes
Accessing private attributes in ABAP Objects

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

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.



NetWeaver SAP White Papers
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
SearchSAP.com is a search service provided by TechTarget and is completely
independent of and not affiliated with SAP AG.
  TechTarget - The IT Media ROI Experts