色粉盒撒娇 发表于 2015-9-18 11:09:19

Call SAP RFC by JCO

  Recently I try to call SAP RFC by .net, although i can connect to SAP successfully, but failed on passing table parameter with multi-columns to RFC. So today try to do that by java. Just log it for use when i want it. The below code is just for testing.
  
  ====================Code Begin==============================
  package sapref;
import com.sap.mw.jco.*;
import java.io.FileInputStream;
import java.io.InputStream;
  import java.util.Properties;
  
public class JavaCallRFC {
    private Properties p = new Properties();
  private static final String POOL_NAME = "MYCONN";
  
    public static JCO.Pool pool;
    public JCO.Repository mRepository;
    public JCO.Client client;
  public JavaCallRFC() {
      try {
            InputStream inStream;
            inStream = new FileInputStream("test.properties");
            p.load(inStream);
            pool = JCO.getClientPoolManager().getPool(POOL_NAME);
            if (pool == null) {
                JCO.addClientPool(POOL_NAME, 100, p);
            }
            client = JCO.getClient(POOL_NAME);
            //We have two methods to create Repository
            //User Pool when create Repository
            mRepository = new JCO.Repository("SAPJcorep", POOL_NAME);
            
      } catch (Exception e) {
            System.out.println(e.getMessage());
      }
    }
    //Create function using Repository and IFunctionTemplate
    public JCO.Function createFunction(String name) throws Exception {
      try {
            IFunctionTemplate ft;
            ft = mRepository.getFunctionTemplate(name.toUpperCase());
            if (ft == null) {
                return null;
            } else {
                return ft.getFunction();
            }
      } catch (Exception ex) {
            ex.printStackTrace();
            return null;
      }
    }
  
    public static void main(String[] args) {
      JavaCallRFC javaCallRFC = new JavaCallRFC();
      JCO.Function function;
      try {
            function = javaCallRFC.createFunction("RFC_READ_TABLE");
            if (function == null) {
                System.out.println("Can't create function!");
                return;
            }
            JCO.ParameterList pl = function.getImportParameterList();
            //pass single value parameter to RFC
            pl.setValue("ZTAB_EMPLOYEE", "QUERY_TABLE");
            pl.setValue(";", "DELIMITER");
            //pass table parameter to RFC
            JCO.ParameterList itabList = function.getTableParameterList();
            JCO.Table table = itabList.getTable("FIELDS");
            table.appendRow();
            table.setValue("NAME", 0);
            table.setValue(10, 1);
            table.setValue(10, 2);
            table.appendRow();
            table.setValue("PHONE", 0);
            table.setValue(20, 1);
            table.setValue(20, 2);
            table.appendRow();
            table.setValue("EMAIL", 0);
            table.setValue(30, 1);
            table.setValue(30, 2);
            function.setTableParameterList(itabList);
            //execute the fucntion
            javaCallRFC.client.execute(function);
            JCO.ParameterList outParam = function.getTableParameterList();
            //Get return Table from RFC
            JCO.Table outTable = outParam.getTable("DATA");
            for (int i = 0; i < outTable.getNumRows(); i++) {
                outTable.setRow(i);
                System.out.print(outTable.getString(0));
                System.out.println();
            }
      } catch (Exception e) {
            e.printStackTrace();
      }
    }
}
====================Code End==============================
  =======Properties file content========
  jco.client.client=//Client
jco.client.user=//UserName
  jco.client.passwd=//Password
jco.client.lang=//language
jco.client.sysnr=//system number
jco.client.ashost=//application server
jco.client.mshost=//message server
jco.client.group=// Group
jco.client.r3name=//R3 Name
  
  
页: [1]
查看完整版本: Call SAP RFC by JCO