Java connects to sap through jco

Source: Internet
Author: User
Tags rfc
About jco

SAP Java connector (SAP jco) is a middleware for communication between Java and sap. This component supports two-way communication mode (Inbound cballsAnd outbound cballs).

Jco supports connection pools and direct connections. Direct Connection requires developers to control the creation and release of connections. Using the connection pool, the pool can be used to manage the allocation, management, and release of connections, minimizing system overhead, compared with direct mode, the advantage is obvious. The example in this article also uses the pool connection method.

Install jco

Download the jco version 3.0 or 2.1 From the https://service.sap.com/connectors (which requires the sap service marketplace account) and choose from your JVM for 32bit or 64bit.

How can I view my JVM version?

-Cmd uses Java-version 32-bit: Java hotspot (TM) Client VM .... 64-bit: Java hotspot (TM) 64-bit server VM .... -Or system. Out. println ("JVM bit size:" + system. getproperty ("Sun. Arch. Data. Model "));
-Or system. Out. println ("JVM bit size:" + system. getproperty ("OS. Arch "));

1. Decompress the zip package to a directory (For example, if you only develop a web app, you can choose not to install it .. Simply drop sapjco3.dll and sapjco3.jar to the lib directory.)

2. InSpecify the jco installation directory in the PATH environment variable.

3. Classpass specifies the installation directory \ sapjco3.jar

Open cmd to the installation directory and execute Java-jar sapjco3.jar. After successful installation, you can see the following information ..

Note:You can alsoSapjco3.dll thrown inSystem32 orThe syswow64 directory does not need to set environment variables. However, SAP officially does not recommend this ..

Decompress the javadoc in the directory. The examples folder contains some simple examples and documents for your reference.

Use of jco

Destination is involved in development with jo3.At runtime, thisDestination willThe destinationdataprovider or serverdataprovider implementation class obtains information such as server ADDR. and logon info. Then we can use it to find the corresponding RFC. The following is used for registrationDestinationdataproviderTool class.

 

 Import  Java. util. hashmap;  Import  Java. util. properties;  Import  Com. sap. Conn. jco. Ext. dataproviderexception; Import  Com. sap. Conn. jco. Ext. destinationdataeventlistener;  Import  Com. sap. Conn. jco. Ext. destinationdataprovider;  Public   Class Jcoprovider Implements  Destinationdataprovider {  Private Hashmap <string, Properties> securedbstorage = New Hashmap <string, Properties> ();  Private Destinationdataeventlistener El; @ override  Public  Properties getdestinationproperties (string destinationname ){  Try  {  //  Read the destination from DB Properties P = Securedbstorage. Get (destinationname );  If (P! = Null  ){  // Check if all is correct, for example                  If  (P. isempty ())  Throw   New Dataproviderexception (dataproviderexception. Reason. invalid_configuration, "Destination configuration is incorrect ", Null  );  Return  P ;}  Return   Null  ;}  Catch (Runtimeexception re ){  Throw   New  Dataproviderexception (dataproviderexception. Reason. internal_error, RE); }}@ override  Public   Void  Setdestinationdataeventlistener (destinationdataeventlistener eventlistener ){  This . El = Eventlistener;} @ override  Public   Boolean  Supportsevents (){ Return   True  ;}  //  Implementation that saves the properties in a very secure way      Public   Void  Changepropertiesforabap_as (string destname, properties Properties ){  Synchronized  (Securedbstorage ){  If (Properties = Null  ){  If (Securedbstorage. Remove (destname )! = Null  ) El. Deleted (destname );}  Else  {Securedbstorage. Put (destname, properties); El. Updated (destname );  //  Create or updated  }}}} 

 

 

Create an rfcmanager class to read sap_conf.properties under the src directory to register the provider, obtain the RFC function, and execute the execute () method.

Sap_conf.properties

 

 
# Thu Nov 01 11:17:09 kst 2012Jco. Client. ashost= ***********Jco. Client. Client= 300Jco. Client. Langu=Kojco. Client. passwd=Passwordjco. Client. sysnr= 02Jco. Client. User= ABAP-01

 

 

 Rfcmanager. Java

Import  Java. Io. ioexception;  Import  Java. util. properties;  Import  Org. Apache. log4j. Logger;  Import  Com. sap. Conn. jco. jcodestination;  Import  Com. sap. Conn. jco. jcodestinationmanager;  Import  Com. sap. Conn. jco. jcoexception;  Import  Com. sap. Conn. jco. jcofunction; Import  Com. sap. Conn. jco. jcoparameterlist;  Import  Com. sap. Conn. jco. Ext. environment;  Import  Com. Shany. Common. util. stringutil;  Public   Final   Class  Rfcmanager {  Private   Static Logger logger = logger. getlogger (rfcmanager. Class  ); Private   Static String abap_as_pooled = "XXX" ;  Private   Static Jcoprovider provider = Null  ;  Private   Static Jcodestination Destination = Null  ;  Static  {Properties Properties = Loadproperties (); provider = New  Jcoprovider ();  //  Catch illegalstateexception if an instance is already registered          Try  {Environment. registerdestinationdataprovider (provider );}  Catch  (Illegalstateexception e) {logger. debug (E);} provider. changepropertiesforabap_as (abap_as_pooled, properties );}  Public   Static  Properties loadproperties () {rfcmanager Manager =New  Rfcmanager (); properties prop = New  Properties ();  Try  {Prop. Load (Manager. getclass (). getresourceasstream ( "/Sap_conf.properties" ));}  Catch  (Ioexception e) {logger. debug (E );}  Return  Prop ;}  Public   Static Jcodestination getdestination () Throws  Jcoexception {  If (Destination = Null  ) {Destination = Jcodestinationmanager. getdestination (abap_as_pooled );}  Return  Destination ;}  Public   Static  Jcofunction getfunction (string functionname) {jcofunction Function = Null ;  Try  {Function = Getdestination (). getrepository (). getfunctiontemplate (functionname). getfunction ();}  Catch  (Jcoexception e) {logger. Error (E );}  Catch  (Nullpointerexception e) {logger. Error (E );}  Return  Function ;}  Public   Static  Void  Execute (jcofunction function) {logger. debug ( "Sap function name:" + Function. getname (); jcoparameterlist paramlist = Function. getimpparameparameterlist ();  If (Paramlist! = Null  ) {Logger. debug ( "Function import structure:" + Paramlist. tostring ());}  Try  {Function.exe cute (getdestination ());}  Catch (Jcoexception e) {logger. Error (E);} paramlist = Function. getexportparameterlist ();  If (Paramlist! = Null  ) {Logger. debug ( "Function export structure:" + Paramlist. tostring ());}}  /*  * Sap zookeeper Ping zookeeper  */      Public   Static  String Ping () {string msg =Null  ;  Try  {Getdestination (). Ping (); msg = "Destination" + abap_as_pooled + "is OK" ;}  Catch  (Jcoexception ex) {msg = Stringutil. getexceptiontrace (Ex);} logger. debug (MSG );  Return  MSG ;}  Public   Static   Void Main (string [] ARGs) {rfcmanager. Ping ();}} 

 

Then you can use the followingCodeTo call RFC.

 Public   Void  Callrfcexample (){  //  Get RFC object Jcofunction function = rfcmanager. getfunction ("function_name" );  //  Set import Parameters Jcoparameterlist importparam = Function. getimpparameparameterlist (); importparam. setvalue ( "Field_name", "Val");  //  Run RFC  Rfcmanager.exe cute (function );  //  Obtain the field value returned by RFC Jcoparameterlist exportparam = Function. getexportparameterlist (); string exparama = Exportparam. getstring ("field_a" ); String exparamb = Exportparam. getstring ("field_ B" );  //  Traverse the table objects returned by RFC Jcotable TB = function. gettableparameterlist (). gettable ("table_name");  For ( Int I = 0; I <TB. getnumrows (); I ++ ) {TB. setrow (I); system. Out. println (Tb. getstring ( "Field01" ); System. Out. println (Tb. getstring ( "Field02" ));}} 

 

 

 

 

 

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.