Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Java connector to consume functions of ABAP system

Shulou Source: shulou.com Published: 2022-06-02 15:52:24 09月16日 Update

Editor to share with you how to use Java connector consumer ABAP system functions, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

My ABAP system has a function called ZDIS_GET_UPSELL_MATERIALS, which inputs a customer ID and product ID and outputs a set of Upsell product ID and description information maintained for the customer and product combination.

The tests are as follows:

Here is the code that consumes the function using Java:

Package jco;import java.io.File;import java.io.FileOutputStream;import java.util.Properties;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.JCoRepository;import com.sap.conn.jco.JCoTable;import com.sap.conn.jco.ext.DestinationDataProvider / * * basic examples for Java to ABAP communication * See help: https://help.sap.com/saphelp_nwpi711/helpdata/en/48/70792c872c1b5ae10000000a42189c/frameset.htm * / public class StepByStepClient {static String DESTINATION_NAME = "ABAP_AS_WITHOUT_POOL"; static public final String ABAP_DURATION = "abapLayerDuration"; static public final String UPSELL_PRODUCT = "upsellProducts"; static public final String PRODUCT_ID = "productID"; static public final String PRODUCT_TEXT = "productText" Static private Properties prepareProperty () {Properties connectProperties = new Properties (); connectProperties.setProperty (DestinationDataProvider.JCO_ASHOST, "your abap system host name"); connectProperties.setProperty (DestinationDataProvider.JCO_SYSNR, "00"); connectProperties.setProperty (DestinationDataProvider.JCO_CLIENT, "111"); connectProperties.setProperty (DestinationDataProvider.JCO_USER," WANGJER "); connectProperties.setProperty (DestinationDataProvider.JCO_PASSWD," your password ") ConnectProperties.setProperty (DestinationDataProvider.JCO_LANG, "en"); createDestinationDataFile (DESTINATION_NAME, connectProperties); connectProperties.setProperty (DestinationDataProvider.JCO_POOL_CAPACITY, "3"); connectProperties.setProperty (DestinationDataProvider.JCO_PEAK_LIMIT, "10"); createDestinationDataFile (DESTINATION_NAME, connectProperties); return connectProperties } static public void main (String [] arg) {createDestinationDataFile (DESTINATION_NAME, prepareProperty ()); JCoDestination destination = null; try {destination = JCoDestinationManager.getDestination (DESTINATION_NAME); JCoRepository repo = destination.getRepository () JCoFunction stfcConnection = repo.getFunction ("ZDIS_GET_UPSELL_MATERIALS"); JCoParameterList imports = stfcConnection.getImportParameterList (); String customerID = "1000040"; String materialID = "11"; imports.setValue ("IV_CUSTOMER_ID", customerID) Imports.setValue ("IV_MATERIAL_ID", materialID); stfcConnection.execute (destination); JCoParameterList exports = stfcConnection.getExportParameterList (); / / int result = exports.getInt ("EV_RESULT"); int abapDuration = exports.getInt ("EV_DURATION") StringBuilder sb = new StringBuilder (); sb.append ("{\"+ ABAP_DURATION +"\ ":" + abapDuration + ","); sb.append ("\"+ UPSELL_PRODUCT +"\ ": ["); JCoTable codes = exports.getTable ("ET_MATERIALS") Int row = codes.getNumRows (); System.out.println ("Total rows:" + row); System.out.println ("ABAP duration:" + abapDuration); for (int I = 0; I < row; iTunes +) {codes.setRow (I) System.out.println (codes.getString ("MATERIAL_ID") +'\ t' + codes.getString ("MATERIAL_TEXT")) Sb.append ("{\" + PRODUCT_ID + "\": "+ codes.getString (" MATERIAL_ID ") +", "+"\ "" + PRODUCT_TEXT + "\":\ "" + codes.getString ("MATERIAL_TEXT") + "\"); if (I < row-1) {sb.append ("},") } else {sb.append ("}");}} sb.append ("]}"); System.out.println ("Final json:" + sb.toString ()) } catch (JCoException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}} static private void createDestinationDataFile (String destinationName, Properties connectProperties) {File destCfg = new File (destinationName+ ".jcoDestination") Try {FileOutputStream fos = new FileOutputStream (destCfg, false); connectProperties.store (fos, "for tests only!"); fos.close ();} catch (Exception e) {throw new RuntimeException ("Unable to create the destination files", e);}

Serialize Json without using Google's gson library for simplicity.

Execution result:

These are all the contents of this article entitled "how to use Java connector to consume the functions of the ABAP system". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Functions systems consumption articles content not much code information most customers sequences more knowledge results industry information information channels channels references learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Linux MariaDB Xiaomi Docker