SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Connect to SAP through Java



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Connect to External system, Unix и Perl
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Wed Nov 07, 2007 10:55 pm    Post subject: Connect to SAP through Java Reply with quote

Connect to SAP through Java
Author: Max Titov

This example will focus on exploring the ability to connect to an SAP application through the use of a remote Java client. The internal SAP arcitecture is designed in such a way that it does not matter if you connect through C, perl, .NET or Java the results will always be the same - A Function Module will take as in put particular information and will return accordingly resulting data.

This example will cover two main stages, first I will discuss the creation of the actual ABAP function following which I will explain the required libraries needed to connect to that function and retrieve the desired information. To make things simple I will build on top of the previous example.

Lets begin by creating a Function Module that could be accessed remotely by a client.
To create a new function one must access the Function Builder: Initial Screen [SE37] display.
Every function must belong to a particular function group.

A Function group is a collection of functions with similar data associations.

A new function group can be created by executing the ‘GoTo - Function groups - Create Function group‘ operation. For this example let’s create a new function group called ‘ztest‘. Once created we are ready to create a new Function Module, lets call it Z_REMOTE_FUNCTIONS_01. Once created add a new Export parameter with the following attributes:

Parameter Name: USER
Tap spec: TYPE
Associated Type: STRING
Pass Value: Checked
Short text: List of users.

The completed attributes should generate the following line of source code:

Code:
FUNCTION Z_REMOTE_FUNCTIONS1.
*"----------------------------------------------------------------------
*"
*"Local interface:
*"  EXPORTING
*"     VALUE(USERS) TYPE  STRING
*"----------------------------------------------------------------------
In essence we created a string variable that will be returned upon the completion of the function. Our objective at this point is to gather informatoin from the USR02 table just like in the previous tutorial and package that up in to a return parameter.

*" Get reference to the user table. "
TABLES: USR02.

*" Initialize the return string. "
USERS   = '<users>'.

* Grab information from the USR02 table.
SELECT * FROM USR02.

*" Use the CONCATENATE operation to append information to the return string. "

  CONCATENATE USERS '<user>' into USERS SEPARATED BY SPACE.
  CONCATENATE USERS '<name>' USR02-BNAME '</name>' into USERS SEPARATED BY SPACE.
  CONCATENATE USERS '<logdate>' USR02-TRDAT '</logdate>' into USERS SEPARATED BY SPACE.
  CONCATENATE USERS '</user>' into USERS SEPARATED BY SPACE.
ENDSELECT.
  CONCATENATE USERS '</users>' into USERS SEPARATED BY SPACE.

Before proceeding to developing a Java client application ensure to switch off the Processing Type attribute to Remote-enabled module. This will ensure that it can be accessed by remote users.

Now lets turn our attention to setting up a Java client application.
Before we can write a single line of code we need to set up our environment so that our Java application can safely connect to an SAP engine.

To do that we will use JCo [Java Connector] that can be downloaded from the SAP Support Portal. Typically the file will be provided in a '.sar' format. This is a compressed package that you can unpack using the SAPCAR application.

In addition to that you will need to install an RFC library, more information on how to accomplish this task can be found in notes:

413708: RFC library that is current at this time.
336693: Replacing the librfc32.dll on a Win 32 Platform.


After you download the JCo unzip it and locate the docs\jco\intro.html file which will guide you further in the Installation section.
After you complete the installation we are ready to start coding.

Create a new Java class called FunctionCaller.java.

Code:
import com.sap.mw.jco.*;

public class FunctionCaller{
        public static void main(String[] args){
                JCO.Client client = null;

                try{
                        // Print the version of the underlying JCO library
                        System.out.println("nnVersion of the JCO-library:n" +
                                     "---------------------------n" + JCO.getMiddlewareVersion());
                        // Create a client connection.
                        client = JCO.createClient("000",
                                "username",
                                "password",
                                "EN",
                                "hostname",
                                "00");
                        // Open the connection.
                        client.connect();

                        // Create an empty input/outpu parameter list.
                        JCO.ParameterList input         = JCO.createParameterList();
                        JCO.ParameterList output        = JCO.createParameterList();
                        // Specify the parameters types the function will be returning.
                        output.addInfo("USERS", JCO.TYPE_STRING,255);
                        // Call the function.
                        client.execute("Z_REMOTE_FUNCTIONS1", input, output);
                        // Print the result
                    System.out.println("The function 'STFC_CONNECTION' returned these parameters:n" +
                         "-----------------------------------------------------------------");
                        for (int i = 0; i < output.getFieldCount(); i++) {
                                System.out.println("Name: " +  output.getName(i) + " Value: "
                                                                        + output.getString(i));
                        }

                        // Close the connection.
                        client.disconnect();

                } catch (Exception e){
                        System.out.println("Caught an exception: n" + e);
                        if ( client !=null )
                                client.disconnect();
                }
        }
}


The contents of the above class will establish a connection with the SAP server and execute the created function retrieving EXPORT variable USERS.

Note: Make sure you compile your code with the appropriate classpath including the sapjco.jar container.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Connect to External system, Unix и Perl All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.