Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

JCo( Java Connector)

Former Member
0 Likes
756

Hi Guys,

Any one give some info abt JCo( Java Connector). Thanks in advance.

Thanks

Kiran.B

2 REPLIES 2
Read only

ashok_kumar24
Contributor
0 Likes
517

Hi Kiran,

Check the following example

-


SAP Java Connector - Excample 1: Simple RFC call

Scenario

We will call the RFC function module ZNAS_HIE1_GET_MEMBER_FARM.that returns members that have owned a farm. Input to the function is a farm number, and output is a table of members that have owned the farm and Owner number (Sequence number of owners).

Class testJCO is excuted and calls class GetMemberFarm method GetMemberFarmFromSap. Input parameters to GetMemberFarm are Farm and Owner number.

GetMemberFarmFromSap executes RFC ZNAS_HIE1_GET_MEMBER_FARM, that returns a table of members that have owned the farm from the input parameters.

GetMemberFarmFromSap loops through the table and finds the member where ZZCHCODE (Owner numer), is equal owner number from the input-parameters. and returns the member.

Code

Class testJCO

public class testJco

{

public static void main(String[] args)

{

GetMemberFarm testGetMemberFarm = new GetMemberFarm();

String memberFarm= testGetMemberFarm.GetMemberFarmFromSap("0111020155","02");

System.out.println("Memberfarm: " + memberFarm);

}

}

Class GetMemberFram

import com.sap.mw.jco.*; //The JCO

public class GetMemberFarm {

public String GetMemberFarmFromSap(String farm, String OwnerNumber)

{ String zzmemb ="";

String memberFarm = "";

JCO.Repository mRepository;

JCO.Client mConnection = null;

JCO.Function myFunction = null;

//----


//Create Connection to SAP

//----


try

{

mConnection = JCO.createClient("800", //SAP client

"HFR", //User ID

"vimmer3", //Password

"EN", //Language

"53.205.22.71", //Host

"03"); //System

mConnection.connect();

System.out.println("Connection OK");

}

catch (Exception ex)

{

System.out.println(ex);

}

//----


// Create function and parameters

//----


try

{

//Create repository

mRepository = new JCO.Repository( "GetMember", mConnection );

//Get a function template from the repository

IFunctionTemplate ftemplate = mRepository.getFunctionTemplate("ZNAS_HIE1_GET_MEMBER_FARM");

//Create function

myFunction = new JCO.Function(ftemplate);

System.out.println("Function created");

//Set import parameter

JCO.Field zzfarm = myFunction.getImportParameterList().getField("ZZFARM");

zzfarm.setValue(farm);

System.out.println("Parameters ok");

}

catch (Exception ex)

{

System.out.println(ex);

}

//----


// Execute function

//----


try

{

mConnection.execute(myFunction);

System.out.println("RFC Call OK");

}

catch (Exception ex)

{ System.out.println(ex); //Exception from function

}

//----


// Handle return table GT_HIERARCHY

// Loop over the table and find the record that

// has ZZCHCODE = OwnerNumber from the

// method parameters, and return Member for the

// record

//----


JCO.Table gt_HIERARCHY = null;

try

{

gt_HIERARCHY=myFunction.getTableParameterList().getTable("GT_HIERARCHY");

//Loop thhrough table and return the member that has

// changecode (ZZCHCODE) = 02

for (int i = 0; i < gt_HIERARCHY.getNumRows(); i++)

{ gt_HIERARCHY.setRow(i);

String zzchcode = gt_HIERARCHY.getString("ZZCHCODE");

if (zzchcode.equals(OwnerNumber))

{

zzmemb = gt_HIERARCHY.getString("ZZMEMB");

}

}

}

catch (Exception ex)

{ System.out.println(ex);

}

//----


// Disconnect from SAP

//----


try

{

mConnection.disconnect();

System.out.println("Disconnected from SAP");

}

catch (Exception ex)

{

System.out.println(ex);

}

return zzmemb;

}

} //public class GetMemberFarm

-


Tips and tricks for the Java connector

-


How to set and import parameter - Field

Give the import parameter SALESDOCUMENT the value "00006973":

JCO.Field SalesDocumentField = jcoFunction.getImportParameterList().getField("SALESDOCUMENT");

SalesDocumentField.setValue("00006973");

How to handle an import parameter - Table

JCO.Table ORDER_PARTNERS = jcoFunction.getTableParameterList().getTable("ORDER_PARTNERS");

ORDER_PARTNERS.appendRow();

ORDER_PARTNERS.setValue("AG","PARTN_ROLE");

ORDER_PARTNERS.setValue("0000001032","PARTN_NUMB");

How to handle an import parameter - Structure

JCO.Structure order_header_inx = jcoFunction.getImportParameterList().getStructure("ORDER_HEADER_INX");

order_header_inx.setValue("I","UPDATEFLAG");

order_header_inx.setValue("X","DOC_TYPE");

How to handle an export parameter - Table

JCO.Table myTable = jcoFunction.getTableParameterList().getTable("STATUSINFO");

for (int i=0; i < myTable.getNumRows(); i++)

myTable.setRow(i);

// Retrieve the value of the field DOC_DATE

oDocDate = myTable.getField("DOC_DATE").getValue();

How to handle BAPI return table

Meesage type and message is stored in vector bapiReturn.

Vector bapiReturn;

JCO.Table jcoReturn = jcoFunction.getTableParameterList().getTable("RETURN");

for (int i = 0; i < jcoReturn.getNumRows(); i++)

{ jcoReturn.setRow(i);

String Message = jcoReturn.getField("TYPE").getValue() + " " +

jcoReturn.getField("MESSAGE").getValue();

bapiReturn.setSize(i + 1);

bapiReturn.setElementAt(new String(Message),i );

}

Using COMMIT

try

{ // Get a function template from the repository

IFunctionTemplate ftemplate = mRepository.getFunctionTemplate("BAPI_TRANSACTION_COMMIT");

// Create a function from the template

jcoCommit = new JCO.Function(ftemplate);

if ( jcoCommit == null )

// Do something..;

}

catch (Exception mException)

{ mException.printStackTrace(); }

// Execute COMMIT

try

{ sapConnection1.mConnection.execute(jcoCommit);

}

catch (Exception mException)

{ mException.printStackTrace(); }

Thanks and Good Luck

AK

Read only

Former Member
0 Likes
517

hi

good

SAP Java Connector (SAP JCo) is a middleware component that enables the development of SAP-compatible components and applications in Java. SAP JCo supports communication with the SAP Server in both directions: inbound calls (Java calls ABAP) and outbound calls (ABAP calls Java).

SAP JCo can be implemented with Desktop applications and with Web server applications.

SAP JCo is used as an integrated component in the following applications:

· SAP Business Connector, for communication with external Java applications

· SAP Web Application Server, for connecting the integrated J2EE server with the ABAP environment.

SAP JCo can also be implemented as a standalone component, for example to establish communication with the SAP system for individual online (web) applications

http://help.sap.com/saphelp_nw04/helpdata/en/6f/1bd5c6a85b11d6b28500508b5d5211/content.htm

thanks

mrutyun^