Java calls the sap RFC Function

Source: Internet
Author: User
Tags rfc

Recently, the project needs to be connected to sap, write data in the project to the SAP system, and communicate with sap to determine to use the RFC function provided by SAP. The specific call method is as follows:

Import Java. util. arraylist; import Java. util. list; import Java. util. properties; import Org. apache. log4j. logger; import COM. hafx. codesure. tqs. SAP. model. sapstock; import COM. hafx. codesure. tqs. util. propertiesutils; import COM. SAP. MW. jco. ifunctiontemplate; import COM. SAP. MW. jco. jco; /*** connection class with sap rfc * @ author lyh * @ version 2013-8-1 * @ see sapjco * @ since */public class sapjco {/*** log */Private Static logg Er log = logger. getlogger (sapjco. class);/*** name of the SAP function */Private Static string functionname = "zfm_crm_set_quty "; /*** name of the table corresponding to the data to be written to the function */Private Static string tablename = "lt_table";/*** sap connection */Private Static jco. client mconnection = NULL;/*** sap response */Private Static jco. repository mrepository;/*** properties */Private Static Properties P = new properties (); static {P = new properti Esutils (). getproperties ("/config/tqs. properties "); // obtain the mconnection = jco from the configuration file. createclient (P. getproperty ("jco. client. client "), // sap client p. getproperty ("jco. client. user "), // userid p. getproperty ("jco. client. passwd "), // password" en ", // language (null for the default language) p. getproperty ("jco. client. ashost "), // application server host name p. getproperty ("jco. client. sysnr ") // syste M number);}/***** Description: <br> * implement: <br> ** @ Param stocks dealer inventory set * @ return X indicates that the dealer has succeeded. Others indicate that the dealer has failed. * @ see */public static string callremote (list <sapstock> stocks) {string sapreturn = ""; try {/* // obtain mconnection = jco from the configuration file. createclient ("121", // sap client "jsydj", // userid "123456jsy", // password "en", // language (null for the default language) "192.168.2.22", // application se Rver host name "01"); // system number * // connect to mconnection. connect (); jco. function function = sapjco. createfunction (functionname); jco. table Codes = NULL; Codes = function. gettableparameterlist (). gettable (tablename ); /* dis_bp // dealer code 10-digit fixed matnr // material code 40-digit zyear // 4-digit zmonth Of The Year // 2-digit cycle of the month // cycle 01 02 03 04-1 accumulate final extra time is placed in week 04 quantity_quan // inventory quantity bottle datum // current date yyyymmdd uzeit // current time hhmmss dis_bp _ Desc // dealer name matnr_desc // logistics name */For (sapstock stock: stocks) {codes. appendrow (); codes. setvalue (stock. getdisbp (), "dis_bp"); codes. setvalue (stock. getmatnr (), "matnr"); codes. setvalue (stock. getzyear (), "zyear"); codes. setvalue (stock. getzmonth (), "zmonth"); codes. setvalue (stock. getcycle (), "Cycle"); codes. setvalue (stock. getquantityquan (), "quantity_quan"); codes. setvalue (stock. getdatum (), "datum "); Codes. setvalue (stock. getuzeit (), "uzeit"); codes. setvalue (stock. getdisbpdesc (), "dis_bp_desc"); codes. setvalue (stock. getmatnrdesc (), "matnr_desc"); // codes .} // execute mconnection.exe cute (function); // obtain the sap return value sapreturn = function. getexportparameterlist (). getstring ("ev_flag"); log.info ("sap dealer inventory return value:" + sapreturn); Return sapreturn;} catch (exception ex) {log. error ("an exception occurred when connecting sap RFC dealer inventory! Exception information: "+ ex. getmessage ();} finally {mconnection. disconnect ();} return sapreturn;}/***** Description: SAP function <br> * implement: <br> ** @ Param name * @ return * @ throws exception * @ see */Private 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. ");}}}

Public class propertiesutils {/*** log */Private Static final logger = logger. getlogger (propertiesutils. class);/***** Description: Read the properties file ** @ Param ppath * @ return properties * @ throws ioexception * @ see */public properties getproperties (string ppath) {properties P = NULL; try {inputstream is = getclass (). getresourceasstream (ppath); If (is = NULL) {logger. error ("Path =" + ppath + "= properties file stream is null");} p = new properties (); p. load (is);} catch (exception e) {logger. error (E. getmessage () ;}return P ;}}

You need to add sapjco. jar to the project.

You also need to put librfc32.dll and sapjcorfc. dll under javahome \ JRE \ bin \; otherwise, the following exception occurs: [No sapjcorfc in Java. Library. Path]. java. Library. Path

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.