Connect to SAP in Java for RFC call

Source: Internet
Author: User

1. Create a Java Project first.
2. Add sapjco. jar to the Java Project Build path of the Project (add external Jar ).
3. In the resource manager, place librfc32.dll and sapjcorfc. dll in the root directory of the Project.
4. Compile the Jco program:
Method 1:
Public class SapJco {
Static JCO. Client mConnection = null;
Static JCO. Repository mRepository;
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
Try {
MConnection = JCO. createClient ("200", // SAP client
"******", // Userid
"*******", // Password
"EN", // language (null for the default language)
"Ip address", // application server host name
"10"); // system number
MConnection. connect ();
JCO. Function function = SapJco. createFunction ("RFC Function name ");
JCO. Table codes = null;
Codes = function. getTableParameterList (). getTable ("ITAB ");

Codes. appendRow ();
Codes. setValue ("9220", "WERK ");
Codes. setValue ("90", "LINE ");
Codes. setvalues ("12345", "AUFNR ");
Codes. setValue ("12", "SERIALNO ");
Codes. setValue ("ss", "WERKNAME ");
Codes. setValue ("x1", "LINETXT ");
Codes. setValue ("33", "XINGHAO ");
Codes. setValue ("21", "NUM1 ");
Codes. setvalues ("093000", "BEGINTIME ");
Codes. setvalues ("103000", "ENDTIME ");
Codes. setValue ("", "DEL ");
MConnection.exe cute (function );

System. out. println (mConnection. getAttributes ());
} Catch (Exception ex ){
Ex. printStackTrace ();
System. exit (1 );
} Finally {
MConnection. disconnect ();
}
}
Public static JCO. Function createFunction (String name) throws Exception {
MRepository = new JCO. Repository ("ARAsoft", mConnection );
Try {
IFunctionTemplate ft = mRepository. getFunctionTemplate (name
. ToUpperCase ());
If (ft = null)
Return null;
Return ft. getFunction ();
} Catch (Exception ex ){
Throw new Exception ("Problem retrieving JCO. Function object .");
}
}
}

Method 2:
Put the logon. properties file in the directory where the compiled file is located,
The file content is as follows:
Jco. client. client = 200
Jco. client. user = *****
Jco. client. passwd = ****
Jco. client. ashost = IP Address
Jco. client. sysnr = 10

Java File Content
Public class TestJco {
Static final String POOL_NAME = "Pool ";
Public static void main (String [] arg ){
JCO. Pool pool = JCO. getClientPoolManager (). getPool (TestJco. POOL_NAME );
JCO. Client mConn = null;
OrderedProperties logonProperties = null;
Try {
LogonProperties =
OrderedProperties. load ("logon. properties ");
}
Catch (Exception ie ){
System. out. println ("cannot read the configuration file! ");
}

If (pool = null ){
JCO. addClientPool (TestJco. POOL_NAME, // pool name
5, // maximum number of connections
LogonProperties); // properties
}
MConn = JCO. getClient (TestJco. POOL_NAME );

JCO. Repository mRepository;
MRepository = new JCO. Repository ("ARAsoft", mConn );

Try {
IFunctionTemplate ft =
MRepository. getFunctionTemplate ("BAPI_COMPANYCODE_GETLIST ");

JCO. Function fun = ft. getFunction ();

MConn.exe cute (fun );

}
Catch (Exception ex ){
System. out. println ("error in generating RFC Funtion! ");
}

System. out. println (mConn. getAttributes ());
JCO. releaseClient (mConn );
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.