Java Connects to SAP ————JCO 3.0 Technology Detailed Explanation

SAP's ERP platform is very powerful, there is no doubt that it is used in every industry field, and because of this, after the company goes online with SAP, it always takes a lot of time to cultivate users. Sometimes, on SAP, users The operation requirements of the company are very simple, but on the SAP platform, many complex operations have to be carried out.

===================================================== ======================================

In our company, many Sales and "bosses" are Computer idiots, although SAP is very powerful, they find it very complicated and cumbersome to use it by themselves. They want to check reports and operate some functions, and always remember various T-CODEs. With the T-CODEs developed by developers The more, the more the user has to remember. Although SAP is very powerful, it also has many inconveniences, because it is not designed for a certain company, so it also has many imperfections and cannot meet the needs of the company.

As the company has more and more users, the account allocation and authority allocation purchased by the company's SAP are also constantly being demanded. In many cases, dozens of users have the same operational requirements for SAP, but the company has to Purchase and assign SAP accounts for them... Based on the company's internal needs, we hope to extract some simple functional modules from SAP and set up a Web platform. Efficient, convenient and mainstream interface platform.

After comparison and analysis, we decided to use the JCO technology. As an ABAP developer from Java, I have never really understood the JCO technology. I found out that the JCO technology has already been updated to version 3.0 on the Chinese website ( Many information on JCO 2.0 technology (Baidu, 360 Search) are related to JCO 2.0 technology, and there is very little information on JCO3.0. Although there are a lot of them on Google, many questions encountered during the development process are also answered by people on Google. not much. But in order to keep pace with the times, I still spent a period of time seriously studying the API of JCO 3.0, and found a lot of information. It was finally researched for me. After continuous development and continuous penetration and understanding of JCO 3.0, I can now control it very well.

Without further ado, let's share with you the implementation technology of JCO 3.0.

<!--[if !supportLists]--> 1. <!--[endif]--> First of all, if you don't know what JCO is? What can you do with JCO? Go Google it yourself, I won't explain it here. Let's take a look at how JCO 3.0 connects to SAP Netweaver.

  There are many differences between JCO 3.0 and 2.0. Its connection to SAP is faster, more stable and more efficient. If you don't believe it, you can go to see the 2.0 technology after learning the 3.0 technology.

  There are generally two ways for JCO 3.0 to interface with SAP: one uses the Web server as the client and SAP Netweaver as the server to transmit and receive data; the other is as the server, but I don't know much about this method. , just tested, what kind of response SAP will have after stopping and starting Web Server on the Web side; and a lot of information on the Internet is also about the first connection method.

     Therefore, here, I will only introduce the first connection method. The principle of the first connection SAP method is:

    With Web Server (Tomcat) as the client, JCO technology as the intermediate interface bridge, and SAP as the service, all the user's operation data on the Web side will eventually be rolled back to SAP for maintenance through RFC, and SAP will automatically process these data. The principle of BAPI is the same. Many companies, after going online with SAP, need to write the data on the original company's internal ERP software into SAP, which is also handled by JCO technology as a bridge.

  

<!--[if !supportLists]--> 2. <!--[endif]--> Connection Step 1:

   When JCO connects to SAP, a connection pool must be created first, and its connection information is saved in a Properties file inside.

   /**

* Create properties file



* @param filename

* file name

* @param stuffix

* extension name

* @param host

* host address

* @param sysnr

* system ID

* @param client

* client

* @param user

* username

* @param pass

* password

* @param lang

*            語言

*  @param  pool_capacity  最大空閒連接

*  @param  peak_limit 最大活動鏈接數

*  @return

*/

public   static   boolean   creatSapPros (String filename, String stuffix,

String host, String sysnr, String client, String user, String pass,

String lang, String pool_capacity, String peak_limit) {

Properties pros =  new  Properties();

boolean  iscreate =  false ;

String sysno =  "" ;

if  (sysnr.equals( "AFD" )) {

sysno =  "00" ;

}

if  (sysnr.equals( "AFQ" )) {

sysno =  "00" ;

}

if  (sysnr.equals( "AFP" )) {

sysno =  "01" ;

}

if  (lang.equals( "" )) {

lang =  "ZF" ;

}

pros.clear();  // 先清空

pros.setProperty(DestinationDataProvider. JCO_ASHOST , host);

pros.setProperty(DestinationDataProvider. JCO_SYSNR , sysno);

pros.setProperty(DestinationDataProvider. JCO_CLIENT , client);

pros.setProperty(DestinationDataProvider. JCO_USER , user);

pros.setProperty(DestinationDataProvider. JCO_PASSWD , pass);

pros.setProperty(DestinationDataProvider. JCO_LANG , lang);

pros.setProperty(DestinationDataProvider. JCO_POOL_CAPACITY ,

pool_capacity);

pros.setProperty(DestinationDataProvider. JCO_PEAK_LIMIT , peak_limit);

iscreate =  createFiles (filename, stuffix, pros);

if  (iscreate) {

return   true ;

}  else  {

return   false ;

}

}

/**

* 判斷文件是否存在



*  @param  filename

*  @param  stuffix

*  @return

*/

public   static   boolean  isFileExist(String filename, String stuffix) {

File file =  new  File(filename +  "."  + stuffix);

if  (file.exists()) {

try  {

System. out .println( "路徑:"  + file.getCanonicalPath());

}  catch  (IOException e) {

e.printStackTrace();

}

return   true ;

}  else  {

return   false ;

}

}   

/**

* 創建新文件



*  @param  filename

*            文件名

*  @param  suffix

*            後綴名

*  @param  pros

*            Properties

*  @return

*/

public   static   boolean   createFiles (String filename, String suffix,

Properties pros) {

File file =  new  File(filename +  "."  + suffix);

FileOutputStream fos =  null ;

if  (!file.exists()) {

try  {

System. out .println( "********* 正在寫入文件 **********" );

fos =  new  FileOutputStream(file,  false );

pros.store(fos,  "Author: EthanLiang" );

fos.close();

return   true ;

}  catch  (FileNotFoundException e) {

System. out .println( "-------- 找不到文件! ---------");

e.printStackTrace();

return false ;

} catch (IOException e) {

System.out .println( "--------- Content writing failed! ---------" );

e. printStackTrace();

return false ;

} finally {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

} else {

return false ;

}

}

Above I wrote a method to create a Property file method, in this case, this file is saved in the web server. As for the location, after you create it yourself, you can look for it if you are interested.    

<!--[if !supportLists]--> 3. <!--[endif]--> Step 2: Start connecting to SAP:

   In this step, we can also call it "login to SAP". In fact, the principle of JCO implementation It is the same as the client principle of SAP. If you want to log in to SAP, you must first log in. The first step is to create a connection pool. Next, we will write the user's login information in this file. SAP It will be called through the RFC itself.

    public   synchronized   JCoDestination  getJCoDestination(String filename,

String stuffix, String host, String sysnr, String client,

String user, String pass, String lang) {

boolean  isexist =  false ;  // 判断文件是否存在

boolean  isCreate =  false ;  // 穿件pro文件

JCoDestination destination;

isexist = FileUtil. isFileExist (filename, stuffix);

if  (isexist ==  true ) {

System. out .println( "-------- 文件已經存在 -----------" );

try  {

destination = JCoDestinationManager. getDestination (filename);

return  destination;

}  catch  (JCoException e) {

System. out

.println( "---------- 獲取JCoDestination失敗! -----------" );

e.printStackTrace();

return null ;

}

} else {

isCreate = FileUtil.creatSapPros (filename, stuffix, host, sysnr,

client, user, pass, lang, "3000" , "500" );

if (isCreate == true ) {

try {

destination = JCoDestinationManager

. getDestination (filename);

System. out

.println( "---------- Get JCoDestination! Success, returning data --------" );

return destination;

} catch (JCoException e) { System.out

.println

( "---------- Could not get JCoDestination! -----------" );

e.printStackTrace();

return null ;

}

} else { // if the file doesn't exist

return null ;

}

}

}

  ===================================================== ================

The above connection SAP code, I designed a very user-friendly code, I wrote a getJCoDestination method, this method is used to connect to SAP, the connection is successful After that, the remote connection destination information, which is JCoDestination, will be returned. In the code, before logging in, first determine whether the property file has been created. If it is created, directly take out the file login information to connect to SAP. If not, create it first. Please see step 2 for the created code.

JCoDestinationManager.getDestination (filename);

This code is to obtain the login information according to the name of the property file you created;

the filename here is named by your own random definition, when you create the property file, what name is it used, you getDestination also What name to use, this filename does not contain the property suffix name, you must remember. For example: createFiles (“filetest”, “pros”, Properties pros); This method is the method of [Step 2] to create a property file. We assumed above to create a property file, the filename is [filetest], and the suffix is ​​[pros] ], after the creation is successful, you will see an additional file [ filetest.pros ] on the server, this file is the file that saves the SAP login information; 

then when you log in, it should be:

JCoDestinationManager.getDestination(" filetest ");

After successfully connecting to SAP, you can test whether the connection is successful or not, and you can print the connection information:

// GetJCoDestination Write the parameters to be passed by yourself, I just make an example.

Destination dest = getJCoDestination(String filename,

String stuffix, String host, String sysnr, String client,

String user, String pass, String lang) ;

System.out.println( destination.getAttributes( ) );

<!--[if ! supportLists]--> 4. <!--[endif]--> After successfully connecting to SAP, it starts to interact with SAP: When

   Java interacts with JCO technology and SAP, it mainly operates through SAP's RFC. It is also the Function of SAP. This Function can be any Function of SAP. Of course, before successfully connecting the Function, please remember to go to the [Properties] setting of the SAP Function and select [Remote-Enable Module to start immediately]. Remember, otherwise connect If it is not a Function, if it is a Function developed by yourself, you need to activate the Function to use it.

First, after we successfully connected through the third part above:

  Destination dest = getJCoDestination(String filename,

String stuffix, String host, String sysnr, String client,

String user, String pass, String lang) ;

// start connection function 

try {

JCoFunction fun = dest.getRepository()

    .getFunction(“ ZRFC_PRICE_STATUS ” );

} catch (JCoException e ) {

   fun = null ;

System. out .println( "JCO exception, unable to get Function" );

e.printStackTrace();

}

If the connection is successful, you can print function.getName () to see;

<!-- [if !supportLists]--> 5, <!--[endif]--> Get interface parameters:

   Everyone who does ABAP development knows that ABAP's Functionz mainly has four interfaces: Export, Import, Changing and Table;

   Function connection After success, we start to get the parameters of these four interfaces; the first is to get these three interfaces:

JCoParameterList imlist = function.getImportParameterList();

JCoParameterList e mlist = function.getExportParameterList();

JCoParameterList t mlist = function.getTableParameterList();

JCoParameterList c mlist = function.getChangingParameterList();

Then we can start to pass data to SAP, if you don't need to pass data, you can skip the following A step of. We pass parameters in the import interface of the function, and the other three interfaces are similar. For the transmission method of the other three excuses, you can refer to the API case or go to Baidu.

imlist .setValue( "STATUS" , "D" ); // pass in parameters

imlist .setValue( "VBELN_LOW" , vbeln .trim()); // pass in parameters

<!--[if !supportLists]--> 6. <!--[endif]--> Start calling the function:

    try {

      JCoParameterList imlist = function.getImportParameterList();

      JCoParameterList t mlist = function.getTableParameterList();

       imlist .setValue( "STATUS" , "D" ); // pass in parameters

       imlist .setValue( "VBELN_LOW" , vbeln .trim()); // pass in the parameter

JCoContext . begin ( destination ); // start a transaction 

       // start executing the function 

function .execute( destination );    

    // get the returned value of the function Data, because after my function is executed successfully, the data returned by abap is stored in the table interface, so I get the data in the above way, if you want to return it in the export, then use the export method to get the 

    JCoTable tab = t mlist .getTable( "IT_RETURN" );   

    for ( int i = 0; i < tab .getNumRows(); i++) {

tab .setRow(i); // Set the pointer position, this is very important 

     // Get the name in the table interface Data

      tab .getString( "VBELN" ); //Order number 

  tab .getString( "NAME1" ); //Buyer 

  tab .getString( "NAME2" ); //Payer 

  tab .getString( "NAME3" ); //Consignee

   }

        try {

JCoContext. end ( destination ); // can be closed after the remote call ends 

} catch (JCoException e1) { e1.

printStackTrace();

}

    catch (JCoException e1) { e1.

printStackTrace();

}

<!--[if !supportLists]--> 7. <!--[endif]--> Finally:

   Well, the interaction between Java and SAP has been finished, and there are still many details and functions in the middle. You are exploring this idea yourself. Bar.

   Of course, to use JCO, you must first download the JAR file of JCO 3.0. I will not provide it here. It is also available on the Internet. If you want, you can also ask me for it. In the compressed package of JCO 3.0, in addition to the Jar package, there are API and Demo, you can take a look at the Demo slowly.

    Finally, it is stated that in order to successfully connect to SAP, there is an important file "sapjco3.dll" in the JCO 3.0 package. You must put this file in the WEB-INF/lib folder of your project, or put it in your local Where did it come from? I forgot, go search it yourself, anyway, I didn't put it in the local directory. Remember this!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326778697&siteId=291194637