Show TOC

Example documentationExample Program JCo Client Locate this document in the navigation structure

 

In this section you find an example of a complete JCo client program. It is made up of the code examples from the activities described in the previous sections:

Syntax Syntax

JCo Client

  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.util.ArrayList;
  4. import java.util.Hashtable;
  5. import java.util.List;
  6. import java.util.Properties;
  7. import com.sap.conn.jco.AbapException;
  8. import com.sap.conn.jco.JCoDestination;
  9. import com.sap.conn.jco.JCoDestinationManager;
  10. import com.sap.conn.jco.JCoException;
  11. import com.sap.conn.jco.JCoField;
  12. import com.sap.conn.jco.JCoFunction;
  13. import com.sap.conn.jco.JCoStructure;
  14. import com.sap.conn.jco.JCoTable;
  15. import com.sap.conn.jco.ext.DestinationDataProvider;
  16. import com.sap.conn.jco.ext.JCoSessionReference;
  17. import com.sap.conn.jco.ext.SessionException;
  18. import com.sap.conn.jco.ext.SessionReferenceProvider;
  19. public class StepByStepClient
  20. {
  21.     static String DESTINATION_NAME1 = "ABAP_AS_WITHOUT_POOL";
  22.     static String DESTINATION_NAME2 = "ABAP_AS_WITH_POOL";
  23.     static
  24.     {
  25.         Properties connectProperties = new Properties();
  26.         connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "ls4065");
  27.         connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  "85");
  28.         connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "800");
  29.         connectProperties.setProperty(DestinationDataProvider.JCO_USER,   "homo farber");
  30.         connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "laska");
  31.         connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   "en");
  32.         createDestinationDataFile(DESTINATION_NAME1, connectProperties);
  33.         connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3");
  34.         connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT,    "10");
  35.         createDestinationDataFile(DESTINATION_NAME2, connectProperties);
  36.         
  37.     }
  38.     
  39.     static void createDestinationDataFile(String destinationName, Properties connectProperties)
  40.     {
  41.         File destCfg = new File(destinationName+".jcoDestination");
  42.         try
  43.         {
  44.             FileOutputStream fos = new FileOutputStream(destCfg, false);
  45.             connectProperties.store(fos, "for tests only !");
  46.             fos.close();
  47.         }
  48.         catch (Exception e)
  49.         {
  50.             throw new RuntimeException("Unable to create the destination files", e);
  51.         }
  52.     }
  53.     
  54.     public static void step1Connect() throws JCoException
  55.     {
  56.         JCoDestination destination = JCoDestinationManager.getDestination(DESTINATION_NAME1);
  57.         System.out.println("Attributes:");
  58.         System.out.println(destination.getAttributes());
  59.         System.out.println();
  60.     }
  61.     
  62.     public static void step2ConnectUsingPool() throws JCoException
  63.     {
  64.         JCoDestination destination = JCoDestinationManager.getDestination(DESTINATION_NAME2);
  65.         destination.ping();
  66.         System.out.println("Attributes:");
  67.         System.out.println(destination.getAttributes());
  68.         System.out.println();
  69.     }
  70.     
  71.     public static void step3SimpleCall() throws JCoException
  72.     {
  73.         JCoDestination destination = JCoDestinationManager.getDestination(DESTINATION_NAME2);
  74.         JCoFunction function = destination.getRepository().getFunction("STFC_CONNECTION");
  75.         If (function == null)
  76.             throw new RuntimeException("BAPI_COMPANYCODE_GETLIST not found in SAP.");
  77.         function.getImportParameterList().setValue("REQUTEXT", "Hello SAP");
  78.         
  79.         try
  80.         {
  81.             function.execute(destination);
  82.         }
  83.         catch (AbapException e)
  84.         {
  85.             System.out.println(e.toString());
  86.             return;
  87.         }
  88.         
  89.         System.out.println("STFC_CONNECTION finished:");
  90.         System.out.println(" Echo: " + function.getExportParameterList().getString("ECHOTEXT"));
  91.         System.out.println(" Response: " + function.getExportParameterList().getString("RESPTEXT"));
  92.         System.out.println();
  93.     }
  94.     
  95.     public static void step3WorkWithStructure() throws JCoException
  96.     {
  97.         JCoDestination destination = JCoDestinationManager.getDestination(DESTINATION_NAME2);
  98.         JCoFunction function = destination.getRepository().getFunction("RFC_SYSTEM_INFO");
  99.         if (function == null)
  100.             throw new RuntimeException("BAPI_COMPANYCODE_GETLIST not found in SAP.");
  101.         try
  102.         {
  103.             function.execute(destination);
  104.         }
  105.         catch (AbapException e)
  106.         {
  107.             System.out.println(e.toString());
  108.             return;
  109.         }
  110.         
  111.         JCoStructure exportStructure = function.getExportParameterList().getStructure("RFCSI_EXPORT");
  112.         System.out.println("System info for " + destination.getAttributes().getSystemID() + ":\n");
  113.         for(int i = 0; i < exportStructure.getMetaData().getFieldCount(); i++) 
  114.         {
  115.             System.out.println(exportStructure.getMetaData().getName(i) + ":\t" + exportStructure.getString(i));
  116.         }
  117.         System.out.println();
  118.         
  119.         //JCo still supports the JCoFields, but direct access via getXX is more efficient as field iterator
  120.         System.out.println("The same using field iterator: \nSystem info for " + destination.getAttributes().getSystemID() + ":\n");
  121.         for(JCoField field : exportStructure)
  122.         {
  123.             System.out.println(field.getName() + ":\t" + field.getString());
  124.         }
  125.         System.out.println();
  126.     }
  127.     public static void step4WorkWithTable() throws JCoException
  128.     {
  129.         JCoDestination destination = JCoDestinationManager.getDestination(DESTINATION_NAME2);
  130.         JCoFunction function = destination.getRepository().getFunction("BAPI_COMPANYCODE_GETLIST");
  131.         if (function == null)
  132.             throw new RuntimeException("BAPI_COMPANYCODE_GETLIST not found in SAP.");
  133.         try
  134.         {
  135.             function.execute(destination);
  136.         }
  137.         catch(AbapException e)
  138.         {
  139.             System.out.println(e.toString());
  140.             return;
  141.         }
  142.         
  143.         JCoStructure returnStructure = function.getExportParameterList().getStructure("RETURN");
  144.         if (! (returnStructure.getString("TYPE").equals("")||returnStructure.getString("TYPE").equals("S"))  )   
  145.         {
  146.            throw new RuntimeException(returnStructure.getString("MESSAGE"));
  147.         }
  148.         
  149.         JCoTable codes = function.getTableParameterList().getTable("COMPANYCODE_LIST");
  150.         for (int i = 0; i < codes.getNumRows(); i++) 
  151.         {
  152.             codes.setRow(i);
  153.             System.out.println(codes.getString("COMP_CODE") + '\t' + codes.getString("COMP_NAME"));
  154.         }
  155.         codes.firstRow();
  156.         for (int i = 0; i < codes.getNumRows(); i++, codes.nextRow()) 
  157.         {
  158.             function = destination.getRepository().getFunction("BAPI_COMPANYCODE_GETDETAIL");
  159.             if (function == null) 
  160.                 throw new RuntimeException("BAPI_COMPANYCODE_GETDETAIL not found in SAP.");
  161.             function.getImportParameterList().setValue("COMPANYCODEID", codes.getString("COMP_CODE"));
  162.             function.getExportParameterList().setActive("COMPANYCODE_ADDRESS",false);
  163.             
  164.             try
  165.             {
  166.                 function.execute(destination);
  167.             }
  168.             catch (AbapException e)
  169.             {
  170.                 System.out.println(e.toString());
  171.                 return;
  172.             }
  173.             returnStructure = function.getExportParameterList().getStructure("RETURN");
  174.             if (! (returnStructure.getString("TYPE").equals("") ||
  175.                    returnStructure.getString("TYPE").equals("S") ||
  176.                    returnStructure.getString("TYPE").equals("W")) ) 
  177.             {
  178.                 throw new RuntimeException(returnStructure.getString("MESSAGE"));
  179.             }
  180.             
  181.             JCoStructure detail = function.getExportParameterList().getStructure("COMPANYCODE_DETAIL");
  182.             
  183.             System.out.println(detail.getString("COMP_CODE") + '\t' +
  184.                                detail.getString("COUNTRY") + '\t' +
  185.                                detail.getString("CITY"));
  186.         }
  187.     }
  188.     
End of the code.