Home > SAP administration / development Tips > ABAP/Java developer tips > Talk to SAP with JCo
SAP Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ABAP/JAVA DEVELOPER TIPS

Talk to SAP with JCo


Jeff Marin
09.30.2002
Rating: -3.38- (out of 5)


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


To create a Java-based Web Service that relies on SAP data and functionality we will need a mechanism for communicating with SAP. That mechanism is JCO. This article is not a complete tutorial on JCO. More information about JCo is available at ftp://ftp.sap.com/pub/sapjco/ and downloading jco-docs-1.1.zip. Unzip this file and view the file JCOTutorial.pdf in the docs directory.

In this article we will demonstrate how to use JCO to do the following:
1. Connect with SAP
2. Create a respository for BAPI metadata
3. Set BAPI input parameters
4. Execute the BAPI
5. Extract BAPI output parameters
6. Disconnect from SAP

Connect to SAP
The JCO.createClient() method uses SAP connection information to create a JCO.client object. Then the connect() attempts to login to R/3 using a specific userid and a password. If successful, the JCO.client object will represent an SAP connection.
JCO.Client client = JCO.createClient( look at docs for input parameters ); client.connect();

BAPI Repository and metadata
A BAPI Repository is a place to store BAPI metadata. This is information about the BAPI itself including all input, output, and in/out parameter names, data types, and lengths.
JCO.Repository repository = new JCO.Repository("Gamma", client);

We now ask the repository for metadata about a specific BAPI. Here we are working with the BAPI_MATERIAL_GETLIST. Please review SAP documentation for more information about this BAPI. This BAPI actually uses 10 different parameters. By asking the repository for BAPI metadata, we do not have to write the code to set up these 10 parameters. Also, we are returned a JCO.Function object that contains BAPI metadata.

JCO.Function function = repository.getFunctionTemplate("BAPI_MATERIAL_GETLIST").getFunction();

Setting input parameters
Before we execute this BAPI, we must set some input parameters. We will set th


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


RELATED CONTENT
ABAP/Java developer tips
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
Improving performance with ABAP Objects in SAP Workflow
Find a BADI in a minute

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


ree fields within the MATNRSELECTION table parameter, MATNR_LOW – this will contain a wildcard pattern to search for materials by name. For example, vac* will return all materials starting with the letters vac.
SIGN – set to I to include materials.
OPTION – set to CP to indicate that MATNR_LOW contains a pattern.

First we need to get the table parameter into a JCO.Table object.

JCO.Table matSelTable = function.getTableParameterList().getTable("MATNRSELECTION");
Now we append a row to this empty table and add values to these fields.
matSelTable.appendRow();
matSelTable.setValue("vac*", "MATNR_LOW");
matSelTable.setValue("I", "SIGN");
matSelTable.setValue("CP", "OPTION");

Executing the BAPI
Now that the function object's input parameters have been initialized, we can execute the BAPI with out JCO.client object.

client.execute(function);

Retrieving output parameters
BAPI_MATERIAL_GETLIST contains an output table parameter called MATNRLIST that contains the fields MATERIAL and MATL_DESC. We need to extract the table data into a JCO.Table object, go through all of its rows, and grab the values of these fields from each row.

JCO.Table table = function.getTableParameterList().getTable("MATNRLIST"); int numRows = table.getNumRows();

for(int ii=0; ii<numRows; ii++) {
table.setRow(ii);
System.out.println(table.getString("MATERIAL") + ":" + table.getString("MATL_DESC"));
}

Disconnect from SAP
Simply call the disconnect() method on the JCO.Client object.

client.disconnect();

Conclusion
Executing SAP functions from Java requires a firm understanding of the JCO classes and Java itself. This article just demonstrates some sample code. If you are interested in using JCO for your projects, please look at the JCO documentation and either get yourself a good book on Java or sign up for some good Java classroom training.

Author Jeff Marin is director of training and education for Gamma Enterprise Technologies Inc.


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




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
HomeNewsTopicsBlogsTipsAsk the ExpertsMultimediaWhite PapersProducts
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