Objective
The company is about to receive a project with SAP interface. Boss let us engage in SAP, first SAP contact, but did not play the development, this week focused on this piece.
All kinds of wall-bumping, SAP system let me say? Forget, said more are tears, attached to this week's study results, we discuss together, ABAP still have to learn more.
Package (TCODE:SE80)
Enter the package that needs to be created, for example: ZTP (before the T-Test, looks like no, you can try)
Click Yes to create.
Enter the information to create the package.
Table (TCODE:SE11)
Click Create.
Click Save, Pop-up Select Package interface
Save it.
Switch to the Fields tab:
A field can correspond to one data element, and one data element can correspond to multiple fields.
For example: Zage data element does not exist, double click to enter:
The hint does not exist, it is created.
Input data type, Length:
Save, you will be prompted to select the package, the following will not say.
Activates the data element.
Back to the CREATE table interface, you can see that the data type and length are displayed.
Click Technical Settings.
Save it.
Click Index.
Click Create INDEX.
Enter the index name.
Select fields, etc.
Save, activate the index, activate the table.
Forget to set key above, choose a field to set key.
Function Group (TCODE:SE37)
Enter the information to create the function group. Click Save to select the package interface:
Select the package you just created:
Save it.
Function Module (TCODE:SE37)
Z_test_1
Click Create to select the function group.
After creation, on the Attributes tab, select Remote Call function remotely.
Here's a quick import,export,tables. Three tabs
Import is the field that needs to be imported, in the test run of SAP can enter this value to test, external program call simultaneous value entered, can understand Java method parameters.
Export is the exported field, the program executes after the return of the field, you can understand the Java method return value, but can return more than one.
Tables can be used as import, export, his field as data results, is actually a complex parameter, for example, the peripheral program calls to pass in a batch of data, you can use him, export, you can return a batch of data.
Here we use table to do a batch upload data a function, click on the Tables tab, define the field
Parameter name, parameter name, own definition
Type spec, parameter type, normal value using type, but in tables here is generally defined complex type, using like to simulate a structure object
Associated type, use like to enter our reference object, which can be a table, a view, an element, and so on.
Then enter, the first return as if the red, and then enter once again, do not know the other great gods have encountered.
Click the Source Code tab to enter the encoding.
The small white also does not know the complex code, therefore can only write briefly.
Our goal is to insert the incoming data into SAP's internal table, coded as follows
INSERT ZTAB01 from Tableimpt.
The above code is OK, import the incoming IMPT into the inner table.
First save, click Check, no error activation can be.
then click Activate.
Activation is prompted in the lower left corner.
Then we can test whether the program is successful.
Enter the input parameters screen to see the tables fields we have defined.
Click to enter edit data.
When you are finished editing, return.
Click Execute.
This is the result of the execution.
View data by Tcode:se11 into a database table
The data has been inserted into the SAP internal table.
Z_test_2
Data upload has been completed, this time we do a data query function.
Also we choose to call the function remotely
This time we add an import parameter, selectively based on the criteria to query.
In the tables parameter we add a return parameter.
When set, enter code source code.
* Determine if the incoming parameter is empty if Dzname = '. * Query all data for NULL SELECT * intotable outt from ZTAB01. ELSE. SELECT * intotable Outt from ZTAB01 WHERE zname = dzname. ENDIF.
Click Check to activate the test after no problem.
When testing, the input parameters are displayed.
After execution, the results have been shown, click to enter to view details.
Query when a condition is not entered.
To this RfC we have created it.
Java
Preparation Tools
Eclipse
Sapjco.jar
Librfc32.dll
Sapjcorfc.dll
(The above two dynamic connections need to be loaded, the simplest way is to put together with the Java execution program, in fact, is the JDK Environment Bin directory)
Environment
Window Environment
JDK 1.6 32-bit
First, a tool class was prepared to connect to SAP.
Import com.sap.mw.jco.ifunctiontemplate;import com.sap.mw.jco.jco;import com.sap.mw.jco.jco.client;/** * SAP Connection Tool class * @ Author Berr * 2014.11.7 */public class Sapconn {private JCO. Client client; Client Connection object Private JCO. function function; RFC Function Object String host,clientid,username, Password,lang, Sysnr; Address, client, user name, password, language, System identity public sapconn () {}/** * Create connection object */public Void Connect () {client = Jco.createclient (ClientId, USERNAME,PASSWORD,LANG,HOST,SYSNR); Client.connect ();} /** * Disconnect */public void disconnect () {client.disconnect ();} Public client getclient () {return client;} public void Setclient (client client) {this.client = client;} /** * Register and get an RFC function * @param reName registered name, any * @param ftname RFC name */public void regfunction (String rename,string ftname) { JCO. Repository mrepository = new JCO. Repository (rename,this.client); Ifunctiontemplate ft = mrepository.getfunctiontemplate (Ftname.touppercase ()); this.function = Ft.getFunction ();} /** * Set Input parameters * @param parameter parameter name * @param value */public voID setimport (String parameter,string value) {JCO. Parameterlist im = This.function.getImportParameterList (); Im.setvalue (Value,parameter);} /** * Gets tables Structure Object * @param tableName parameter name * @return */public JCO. Table getTable (String tableName) {return this.function.getTableParameterList (). getTable (TableName);} /** * Performs the currently registered function */public void execute () {This.client.execute (this.function);} Public JCO. function GetFunction () {return function;} public void Setfunction (JCO. function function) {this.function = function;}}
Here we begin to prepare the test class
Test Insert RFC function, code as follows
Import Com.sap.mw.jco.jco;public class Callinsert {public static void main (string[] args) {Sapconn sc = new Sapconn (); sc.h Ost= "192.168.0.140"; sc.clientid = "001"; sc.username = "Dev"; sc.password = "d123456"; Sc.lang = "zh"; Sc.sysnr= "000";/* These are the information required to connect to SAP, log in to SAP GUI also need these, not much explained */sc.connect ();//Create connection string function1 = "Z_test_1"; Sc.regfunction (Function1, Function1); Register and obtain a functionjco.table IMT = sc.gettable ("IMPT");//Get the tables parameter set in function for (int i = 0; i <; i++) {Imt.ap Pendrow (); Add a row of Imt.setvalue (i, "zage"); Set Zage value, must be uppercase Imt.setvalue ("C" + I, "zname");// set Zname value, must be uppercase}sc.execute (); Sc.disconnect ();}}
After successful execution, return to SAP to view the results.
Test the query RFC function with the following code
Import Com.sap.mw.jco.jco;public class Callquery {public static void main (string[] args) {Sapconn sc = new Sapconn (); Sc.ho St= "192.168.0.140"; sc.clientid = "001"; sc.username = "Dev"; sc.password = "d123456"; Sc.lang = "zh"; Sc.sysnr= "000";/* These are the information required to connect to SAP, login SAP GUI also need these, not much explained */string TPL = "name:%s,age:%s"; Sc.connect ();//Create connection String function1 = "z_test_1"; Sc.regfunction (Function1,function1); Register and get a functionstring zname = "C1"; Sc.setimport ("Dzname", zname); Set input parameters Sc.execute (); JCO. Table IMT = sc.gettable ("Outt"); Gets the data System.out.println in the function tables ("-------based on condition" +zname+ "Query-----------");d o{object name = Imt.getvalue (" Zname "), Object age = Imt.getvalue (" Zage "); System.out.println (String.Format (TPL, Name,age));} while (Imt.nextrow ()); System.out.println ("------------------not based on conditions"); Sc.regfunction (Function1,function1); Register and obtain a functionsc.execute (); IMT = sc.gettable ("Outt"); Gets the data in function tables Do{object name = Imt.getvalue ("Zname"), Object age = Imt.getvalue ("Zage"); System.out.println (StRing.format (TPL, Name,age));} while (Imt.nextrow ()); Sc.disconnect ();}}
The results of the implementation are as follows:
Above, the simple RFC program development and external program call is complete, welcome to communicate together.
The following summarizes some exceptions for external program calls.
Dynamic link library not loaded
Exceptionin thread "main" java.lang.ExceptionInInitializerError:JCO.classInitialize (): Could not load middleware Layer ' Com.sap.mw.jco.rfc.MiddlewareRFC ' Jco.nativeinit (): Could not initialize dynamic link library SAPJCORFC [no SAPJCORFC Injava.library.path]. Java.library.path[d:\work\wms\jdk1.6.0_26\jre\bin; C:\WINDOWS\Sun\Java\bin; C:\WINDOWS\system32; C:\WINDOWS; e:/jdk15/bin/. /jre/bin/client; e:/jdk15/bin/. /jre/bin; e:/jdk15/bin/. /jre/lib/i386;d:\app\berr\product\11.2.0\client_1\bin; E:\jdk15\BIN; C:\WINDOWS\system32; C:\WINDOWS; C:\WINDOWS\System32\Wbem; C:\WINDOWS\System32\WindowsPowerShell\v1.0\;D: \eclipse-jee-indigo-sr2-win32\eclipse;;.] Atcom.sap.mw.jco.jco.<clinit> (jco.java:776) at Sapconn.connect (sapconn.java:26) at CallInsert.main (callinsert.java:18)
Solution: Load the Sapjcorfc.dll,librfc32.dll into the JDK dynamic-link library, the simplest way and copy it to the Java_home/bin directory.
RFC does not turn on remote calls
Exceptionin thread "main" Com.sap.mw.jco.jco$exception: (104) rfc_error_system_failure:the function module "Z_test_2" Cannot beused for ' remote ' calls. Atcom.sap.mw.jco.rfc.middlewarerfc$client.nativeexecute (Nativemethod) at com.sap.mw.jco.rfc.middlewarerfc$ Client.execute (middlewarerfc.java:1242) Atcom.sap.mw.jco.jco$client.execute (jco.java:3816) Atcom.sap.mw.jco.jco$client.execute (jco.java:3261) at Sapconn.execute (sapconn.java:82) at CallQuery.main (callquery.java:30)
Solution: Turn on remote calls.
Above, I hope everyone exchanges and exchanges! I e-mail: [email protected], blog can not upload attachments, need Java source code can email me.
Java source has been uploaded http://download.csdn.net/detail/iberr/8133125.
SAP RFC Functions Create Java Program Call Learning Summary step by step illustrations