cancel
Showing results for 
Search instead for 
Did you mean: 

JCo tRFC

Former Member
0 Kudos

Hello

How can i use the Java Connector to call tRFC.

Use createTD() and confirmTD() ??

Is there any sample code or refernce guide??

Regards,

Kit

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

hi Chun

here is a sample code to call a RFC using JCo.

you may google and download the required libraries from the web. the downloads will have demo code with them too.


import com.sap.mw.jco.*;

public class TutorialBapi1 extends Object {
  JCO.Client mConnection;
  JCO.Repository mRepository;
  public TutorialBapi1() {
    try {
      // Change the logon information to your own system/user
      mConnection =
        JCO.createClient("001", // SAP client
          "<userid>",           // userid
          "****",               // password
          "EN",                 // language
          "<hostname>",         // application server host name
          "00");                // system number
      mConnection.connect();
      mRepository = new JCO.Repository("asdf", mConnection);
    }
    catch (Exception ex) {
      ex.printStackTrace();
      System.exit(1);
    }
    JCO.Function function = null;
    JCO.Table codes = null;
    try {
      function = this.createFunction("BAPI_COMPANYCODE_GETLIST");
      if (function == null) {
        System.out.println("BAPI_COMPANYCODE_GETLIST" +
                           " not found in SAP.");
        System.exit(1);
      }
      mConnection.execute(function);
      JCO.Structure returnStructure =
        function.getExportParameterList().getStructure("RETURN");
      if (! (returnStructure.getString("TYPE").equals("") ||
             returnStructure.getString("TYPE").equals("S")) ) {
        System.out.println(returnStructure.getString("MESSAGE"));
        System.exit(1);
      }
      codes =
        function.getTableParameterList().getTable("COMPANYCODE_LIST");
      for (int i = 0; i < codes.getNumRows(); i++) {
        codes.setRow(i);
        System.out.println(codes.getString("COMP_CODE") + 't' +
                           codes.getString("COMP_NAME"));
      }
    }
    catch (Exception ex) {
      ex.printStackTrace();
      System.exit(1);
    }
    try {
      codes.firstRow();
      for (int i = 0; i < codes.getNumRows(); i++, codes.nextRow()) {
        function = this.createFunction("BAPI_COMPANYCODE_GETDETAIL");
        if (function == null) {
          System.out.println("BAPI_COMPANYCODE_GETDETAIL" +
                             " not found in SAP.");
          System.exit(1);
        }
        function.getImportParameterList().
          setValue(codes.getString("COMP_CODE"), "COMPANYCODEID");
        function.getExportParameterList().setActive(false, "COMPANYCODE_ADDRESS");
        mConnection.execute(function);
        JCO.Structure returnStructure =
          function.getExportParameterList().getStructure("RETURN");
        if (! (returnStructure.getString("TYPE").equals("") ||
               returnStructure.getString("TYPE").equals("S") ||
               returnStructure.getString("TYPE").equals("W")) ) {
          System.out.println(returnStructure.getString("MESSAGE"));
        }
        JCO.Structure detail =
          function.getExportParameterList().
          getStructure("COMPANYCODE_DETAIL");
        System.out.println(detail.getString("COMP_CODE") + 't' +
                           detail.getString("COUNTRY") + 't' +
                           detail.getString("CITY"));
      }
    }
    catch (Exception ex) {
      ex.printStackTrace();
      System.exit(1);
    }
    mConnection.disconnect();
  }
  public JCO.Function createFunction(String name) throws Exception {
    try {
      IFunctionTemplate ft = mRepository.getFunctionTemplate(name.toUpperCase());
      if (ft == null)
        return null;
      return ft.getFunction();
    }
    catch (Exception ex) {
      throw new Exception("Problem retrieving JCO.Function object.");
    }
  }
  public static void main (String args[]) {
    TutorialBapi1 app = new TutorialBapi1();
  }
}

Regards

ak

Former Member
0 Kudos

Thank you for reply. The above code is about how to call a normal RFC which is not my need. I need the sample code that make use of createTD() and confirmTD() to call tRFC( Transactional ).