Talk to SAP with JCo

To create a Java-based Web Service that relies on SAP data and functionality we need a mechanism for communicating with SAP.

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 three 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


This was first published in September 2002

Dig deeper on SAP ABAP

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

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:

-ADS BY GOOGLE

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close