人法地,地法天,天法道,道法自然
分类:
2008-03-01 16:42:38
This is an example program for calling a BAPI from the IBM development platform, Access Builder for SAP R/3.
Detailed program examples are shipped with the Access Builder for R/3.
Access Builder for SAP R/3
//Importing the required classes:
import com.sap.rfc.*;
import com.sap.rfc.exception.*;
import com.ibm.sap.bapi.*;
import com.ibm.sap.bapi.generated.*;
//Connecting to the R/3 System:
static private IRfcConnection establishConnection(MiddlewareInfo aMiddlewareInfo)
throws JRfcRemoteException
{
IRfcConnection aConnection = null ;
ConnectInfo aConnectInfo = null ;
UserInfo aUserInfo = null ;
String orbServerName = aMiddlewareInfo.getOrbServerName() ;
// Please adjust the values written in UPPERCASE LETTERS
// in the lines below so that they fit to your needs!
// If you don't know the correct values ask your system
// administrator!
// After correcting these values you should change the
//
// from "false" to "true".
// Then you can re-compile ("javac SampleCompanyCode.java") and
// re-run ("java SampleCompanyCode -conn JNI") this sample...
boolean bAdjusted = true;
if (!bAdjusted) {
throw (new JRfcRfcConnectionException (
"Please adjust the Connection-Parameters to your
needs! (See method \"establishConnection\")"));
}
//Connection information:
aConnectInfo = new ConnectInfo (
3, // int aRfcMode 3=R/3 or 2=R/2
null, // String aDestination
"9.7.12.7", // String aHostName YOUR HOSTNAME (e.g. IP-
//address)
0, // int aSystemNo YOUR SYSTEM-NUMBER
null, // String aGatewayHost
null, // String aGatewayService
null, // String aSystemName
null, // String aGroupName
null, // String aMsgServer
false, // Boolean isLoadBalancing
true); // Boolean isCheckAuthorization
//User information:aUserInfo = new UserInfo (
"MUSTER", // String aUserName, YOUR USERID
"IDES", // String aPassword, YOUR PASSWORD
"800", // String aClient, YOUR CLIENT NUMBER
"e", // String aLanguage, YOUR PREFERRED
//LANGUAGE
1103); // int aCodePage YOUR REQUIRED CODEPAGE
//Technical conversion for the selected middleware;IRfcConnectionFactory aConnectionFactory = FactoryManager.getSingleInstance().getRfcConnectionFactory() ;
aConnection = aConnectionFactory.createRfcConnection(aConnectInfo, aUserInfo) ;
aConnection.open() ;
//Returning the connection:
return aConnection ;
}
//Calling the main method:
public static void main (java.lang.String[] args)
//Setting up the connection using the selected middleware:{
MiddlewareInfo aMiddlewareInfo = new MiddlewareInfo(args) ;
FactoryManager aFactoryManager = FactoryManager.getSingleInstance() ;
aFactoryManager.setMiddlewareInfo(aMiddlewareInfo) ;
//Initializing the connection object:IRfcConnection aConnection = null ;
try
{
aConnection = establishConnection(aMiddlewareInfo) ;
}
catch (Exception ex)
{
System.out.println("ERROR : Could not create connection : " + ex) ;
System.exit(-1) ;
}
System.out.println("Connection established.");
// --- TEST CODE (start) --------------------------------------
try
{
printList(aConnection) ;
//Calling the BAPI:
//Declare an empty Object ID for the Business Object
//CompanyCode:
objectId = CompanyCode.getEmptyObjectId() ;
//Entering a value in the object ID:objectId.getKeyField("COMPANYCODEID").setString("1000") ;
//Instantiate the object CompanyCode with the object ID:companyCode = new CompanyCode(objectId) ; // Create 2nd
CompanyCode
System.out.println ("Successfully created new CompanyCode : '" + companyCode + "'") ;
printDetails(companyCode, aConnection) ;
}
// --- TEST CODE (end) ----------------------------------------
catch (Exception ex)
{
System.out.println ("Unexpected exception occurred:");
System.out.println (ex);
}
}
private static void printDetails(CompanyCode companyCode, IRfcConnection connection)
{
try
{
//Declare the parameters of the BAPI CompanyCode.GetDetail:CompanyCodeGetdetailParams aCompanyCodeGetdetailParams =
new CompanyCodeGetdetailParams() ;
//Aufruf des BAPIs CompanyCode.GetDetail auf die Objektinstanz:companyCode.getdetail(connection, aCompanyCodeGetdetailParams);
//Splitting the parameter object into its separate componentsBapi0002_2Structure struct = aCompanyCodeGetdetailParams.getCompanycodeDetail() ;
System.out.println ("The details of the companycode are : ") ;
//Splitting the structure into individual fields:System.out.println ("CompCode : '" + struct.getCompCode() + "'");
System.out.println ("CompName : '" + struct.getCompName() + "'");
System.out.println ("City1 : '" + struct.getCity() + "'");
System.out.println ("Country1 : '" + struct.getCountry() + "'");
System.out.println ("Currency : '" + struct.getCurrency() + "'");
System.out.println ("Langu1 : '" + struct.getLangu() + "'");
System.out.println ("ChrtAccts : '" + struct.getChrtAccts() + "'");
System.out.println ("FyVariant : '" + struct.getFyVariant() + "'");
System.out.println ("VatRegNo : '" + struct.getVatRegNo() + "'");
System.out.println ("Company : '" + struct.getCompany() + "'");
System.out.println ("AddrNo : '" + struct.getAddrNo() + "'");
System.out.println() ;
}
catch (Exception ex)
{
System.out.println("Exception in printDetails() : " + ex) ;
}
return;
}
private static void printList(IRfcConnection connection)
{
try
{
//Declaring the parameter object:CompanyCodeGetlistParams aCompanyCodeGetlistParams =
new CompanyCodeGetlistParams() ;
//Actual BAPI call:CompanyCode.getlist(connection, aCompanyCodeGetlistParams);
//Splitting the parameter objects into its separate componentsBapi0002_1Table table = aCompanyCodeGetlistParams.getCompanycodeList();
int rowCount = table.getRowCount() ;
System.out.println ("Returned table has " + rowCount + " lines.");
//Evaluating the table row by row:for (int i = 0; i < rowCount; i++)
{
Bapi0002_1TableRow row = table.getRow(i) ;
System.out.println("\t" + row.getCompCode() + "\t" + row.getCompName()) ;
}
System.out.println() ;
}
catch (Exception ex)
{
System.out.println("Exception in printList() : " + ex) ;
}
return;
}
}