Knowledge Base Nr: 00180 callrfc.java - http://www.swe-kaiser.de
Downloads:
Java: SAP JCO sample. RFC function aufrufen und werte auslesen
import com.sap.mw.jco.*;
public class callrfc
{
// The MySAP.com system we gonna be using
static final String SID = "R3";
// The repository we will be using
IRepository repository;
public callrfc()
{
try
{
// Add a connection pool to the specified system
// The pool will be saved in the pool list to be used
// from other threads by JCO.getClient(SID).
// The pool must be explicitely removed by JCO.removeClientPool(SID)
JCO.addClientPool( SID, // Alias for this pool
10, // Max. number of connections
"002", // SAP client
"***", // userid
"***", // password
"DE", // language
"153.95.24.5", // host name
"17" ); // system number
// Create a new repository
// The repository caches the function and structure definitions
// to be used for all calls to the system SID. The creation of
// redundant instances cause performance and memory waste.
repository = JCO.createRepository("MYRepository", SID);
}
catch (JCO.Exception ex)
{
System.out.println("Caught an exception: \n" + ex);
}
}
// Retrieves and prints information about the remote system
public void systemInfo()
{
try
{
// Get a function template from the repository
IFunctionTemplate ftemplate = repository.getFunctionTemplate("RFC_SYSTEM_INFO");
// if the function definition was found in backend system
if (ftemplate != null)
{
// Create a function from the template
JCO.Function function = ftemplate.getFunction();
// Get a client from the pool
JCO.Client client = JCO.getClient(SID);
// We can call 'RFC_SYSTEM_INFO' directly since it does not need any input parameters
client.execute(function);
// The export parameter 'RFCSI_EXPORT' contains a structure of type 'RFCSI'
JCO.Structure s = function.getExportParameterList().getStructure("RFCSI_EXPORT");
// Use enumeration to loop over all fields of the structure
System.out.println("System info for " + SID + ":\n" +
"--------------------");
for (JCO.FieldIterator e = s.fields(); e.hasMoreElements(); )
{
JCO.Field field = e.nextField();
System.out.println(field.getName() + ":\t" + field.getString());
}//for
System.out.println("\n\n");
// Release the client into the pool
JCO.releaseClient(client);
}
else
{
System.out.println("Function RFC_SYSTEM_INFO not found in backend system.");
}
}
catch (Exception ex)
{
System.out.println("Caught an exception: \n" + ex);
}
}
// Retrieves and displays a sales order list
public String callrfcfunction(String strFunction, String strTable, String[] strParam, String[] strValue)
{
char tmpdelim1[] = {1};
String DELIM1 = new String(tmpdelim1);
char tmpdelim2[] = {2};
String DELIM2 = new String(tmpdelim2);
String strValues = new String("");
JCO.Client client = null;
try
{
// Get a function template from the repository
IFunctionTemplate ftemplate = repository.getFunctionTemplate(strFunction);
// if the function definition was found in backend system
if(ftemplate != null)
{
// Create a function from the template
JCO.Function function = ftemplate.getFunction();
// Get a client from the pool
client = JCO.getClient(SID);
// Fill in input parameters
JCO.ParameterList input = function.getImportParameterList();
for (int n=0; n<strValue.length; n++)
{
if ((strValue[n] != null) && (strParam[n] != null))
{
input.setValue(strValue[n], strParam[n]);
}
}
// Call the remote system
client.execute(function);
// Get table containing the orders
JCO.Table table = function.getTableParameterList().getTable(strTable);
// Print results
if (table.getNumRows() > 0)
{
// Loop over all rows
do
{
// Loop over all columns in the current row
for (JCO.FieldIterator e = table.fields(); e.hasMoreElements(); )
{
JCO.Field field = e.nextField();
//System.out.println(field.getName() + ":\t" + field.getString());
strValues += field.getName() + DELIM1 + field.getString() + DELIM2;
}//for
} while (table.nextRow());
}
else
{
//System.out.println("No results found");
strValues = "No results found!";
}//if
}
else
{
//System.out.println("Function BAPI_SALESORDER_GETLIST not found in backend system.");
strValues = "ERROR: Function " + strFunction + " not found in backend system.";
}//if
}
catch (Exception ex)
{
//System.out.println("Caught an exception: \n" + ex);
strValues = "ERROR: " + ex;
}
finally
{
// Release the client to the pool
JCO.releaseClient(client);
}
//System.out.println(strValues);
return strValues;
}
protected void cleanUp()
{
JCO.removeClientPool(SID);
}
public String callrfcmain(String[] argv)
{
//systemInfo();
String strReturn = new String("");;
if (argv.length < 2)
{
return "ERROR: zu wenig parameter!\nusage: callrfcmain Z_RFC_FA_DATEN Z_SG_FADATEN [paramname paramvalue] ...\n";
}
String strParams[] = new String[50];
String strValues[] = new String[50];
for (int n=2; n<argv.length; n+=2)
{
if ((argv[n] != null) && (argv[n+1] != null))
{
strParams[0] = new String(argv[n]);;
strValues[0] = new String(argv[n+1]);;
}
}
// strReturn = e.callrfcfunction("Z_RFC_FA_DATEN", "Z_SG_FADATEN", "FANUMMER", "000071648175");
strReturn = callrfcfunction(argv[0], argv[1], strParams, strValues);
cleanUp();
return strReturn;
}
public static void main(String[] argv)
{
//bauen: C:\j2sdk1.4.2_01\bin\javac -classpath .;sapjco.jar callrfc.java
//aufruf: C:\j2sdk1.4.2_01\bin\java -classpath .;sapjco.jar callrfc Z_RFC_FA_DATEN Z_SG_FADATEN FANUMMER 000071648175
callrfc e = new callrfc();
e.systemInfo();
System.out.println("===================");
String strReturn = new String("");;
String str[] = new String[50];
if (argv.length == 0)
{
str[0] = new String("Z_RFC_FA_DATEN");;
str[1] = new String("Z_SG_FADATEN");;
str[2] = new String("FANUMMER");;
str[3] = new String("000071648175");;
}
else
{
for (int n=0; n<argv.length; n++)
str[n] = argv[n];
}
for (int n=0; n<str.length; n++)
{
if (str[n] != null)
System.out.println("str[" + n + "] = " + str[n]);
}
System.out.println("===================");
strReturn = e.callrfcmain(str);
System.out.println(strReturn);
}
}