Show TOC

Example documentationExample Program BAPI2 Locate this document in the navigation structure

 

Syntax Syntax

  1. import com.sap.mw.jco.*;
  2. public class Bapi2 extends Object {
  3.    JCO.Client mConnection;
  4.    JCO.Repository mRepository;
  5.    public TutorialBapi2() {
  6.       try {
  7.         // Change the logon information to your own system/user
  8.         mConnection =
  9.           JCO.createClient("001", // SAP client
  10.             "<userid>", // userid
  11.             "****", // password
  12.             null, // language
  13.             "<hostname>", // application server host name
  14.             "00"); // system number
  15.         mConnection.connect();
  16.         mRepository = new JCO.Repository("ARAsoft", mConnection);
  17.      }
  18.      catch (Exception ex) {
  19.        ex.printStackTrace();
  20.        System.exit(1);
  21.      }
  22.      JCO.Function function = null;
  23.      JCO.Table codes = null;
  24.        try {
  25.          function = this.createFunction("BAPI_COMPANYCODE_GETLIST");
  26.          if (function == null) {
  27.            System.out.println("BAPI_COMPANYCODE_GETLIST" +
  28.                               " not found in SAP.");
  29.            System.exit(1);
  30.          }
  31.          mConnection.execute(function);
  32.          JCO.Structure returnStructure =
  33.            function.getExportParameterList().getStructure("RETURN");
  34.          if (! (returnStructure.getString("TYPE").equals("") ||
  35.                 returnStructure.getString("TYPE").equals("S")) ) {
  36.            System.out.println(returnStructure.getString("MESSAGE"));
  37.            System.exit(1);
  38.          }
  39.          codes =
  40.            function.getTableParameterList().getTable("COMPANYCODE_LIST");
  41.          codes.setRow(2);
  42. codes.deleteRow();
  43. codes.deleteRow(5);
  44. codes.appendRow();
  45. codes.setValue("XXXX", "COMP_CODE");
  46. codes.setValue("Does not exist", "COMP_NAME");
  47. codes.appendRows(2);
  48. codes.setValue("YYYY", "COMP_CODE");
  49. codes.setValue("Does not exist either", "COMP_NAME");
  50. codes.nextRow();
  51. codes.setValue("ZZZZ", "COMP_CODE");
  52. codes.setValue("Nor does this", "COMP_NAME");
  53. for (int i = 0; i < codes.getNumRows(); i++) {
  54. codes.setRow(i);
  55.            System.out.println(codes.getString("COMP_CODE") + '\t' +
  56.                               codes.getString("COMP_NAME"));
  57.          }
  58.        }
  59.        catch (Exception ex) {
  60.          ex.printStackTrace();
  61.          System.exit(1);
  62.        }
  63.        try {
  64.          codes.firstRow();
  65.          for (int i = 0; i < codes.getNumRows(); i++, codes.nextRow()) {
  66.            function = this.createFunction("BAPI_COMPANYCODE_GETDETAIL");
  67.            if (function == null) {
  68.              System.out.println("BAPI_COMPANYCODE_GETDETAIL" +
  69.                                 " not found in SAP.");
  70.              System.exit(1);
  71.            }
  72.            function.getImportParameterList().
  73.              setValue(codes.getString("COMP_CODE"), "COMPANYCODEID");
  74.            mConnection.execute(function);
  75.            JCO.Structure returnStructure =
  76.              function.getExportParameterList().getStructure("RETURN");
  77.            if (! (returnStructure.getString("TYPE").equals("") ||
  78.                   returnStructure.getString("TYPE").equals("S") ||
  79.                   returnStructure.getString("TYPE").equals("W")) ) {
  80.              System.out.println(returnStructure.getString("MESSAGE"));
  81.            }
  82.            JCO.Structure detail =
  83.              function.getExportParameterList().
  84.              getStructure("COMPANYCODE_DETAIL");
  85.            System.out.println(detail.getString("COMP_CODE") + '\t' +
  86.                               detail.getString("COUNTRY") + '\t' +
  87.                               detail.getString("CITY"));
  88.         }
  89.       }
  90.       catch (Exception ex) {
  91.         ex.printStackTrace();
  92.         System.exit(1);
  93.       }
  94.       mConnection.disconnect();
  95.     }
  96.     public JCO.Function createFunction(String name) throws Exception {
  97.       try {
  98.         IFunctionTemplate ft =
  99.           mRepository.getFunctionTemplate(name.toUpperCase());
  100.         if (ft == null)
  101.           return null;
  102.         return ft.getFunction();
  103.      }
  104.      catch (Exception ex) {
  105.        throw new Exception("Problem retrieving JCO.Function object.");
  106.      }
  107.    }
  108.    public static void main (String args[]) {
  109.      Bapi2 app = new Bapi2();
  110.    }
  111. }
End of the code.