java rfc调用_Java中连接SAP进行RFC调用

1、首先创建Java Project。 2、把sapjco.jar添加到该Project的Java项目构建路径中(添加外部Jar)。 3、在资源管理器中把librfc32.dll、sapjcorfc.dll放到Project的根目录下。 4、编写Jco程序: 方法一: public class SapJco { static JCO.Client mConnection = null; static JCO.Repository mRepository; /**   * @param args   */ public static void main(String[] args) {   // TODO Auto-generated method stub   try {    mConnection = JCO.createClient("200",// SAP client      "******", // userid      "*******", // password      "EN", // language (null for the default language)      "IP地址", // application server host name      "10"); // system number    mConnection.connect();    JCO.Function function = SapJco.createFunction("RFC函数名");    JCO.Table codes = null;    codes = function.getTableParameterList().getTable("ITAB");         codes.appendRow();     codes.setValue("9220", "WERK");     codes.setValue("90", "LINE");     codes.setValue("12345", "AUFNR");     codes.setValue("12", "SERIALNO");     codes.setValue("ss", "WERKNAME");     codes.setValue("x1", "LINETXT");     codes.setValue("33", "XINGHAO");     codes.setValue("21", "NUM1");     codes.setValue("093000", "BEGINTIME");     codes.setValue("103000", "ENDTIME");     codes.setValue("", "DEL");    mConnection.execute(function);        System.out.println(mConnection.getAttributes());   } catch (Exception ex) {    ex.printStackTrace();    System.exit(1);   } finally {    mConnection.disconnect();   } } public static JCO.Function createFunction(String name) throws Exception {   mRepository = new JCO.Repository("ARAsoft", mConnection);   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.");   } } } 方法二: 把logon.properties文件放到编译后文件所在目录下, 文件内容如下: jco.client.client=200 jco.client.user=***** jco.client.passwd=**** jco.client.ashost=IP地址 jco.client.sysnr=10 Java文件内容 public class TestJco { static final String POOL_NAME = "Pool"; public static void main(String[] arg){   JCO.Pool pool = JCO.getClientPoolManager().getPool(TestJco.POOL_NAME);   JCO.Client mConn = null;   OrderedProperties logonProperties = null;   try{    logonProperties =     OrderedProperties.load("logon.properties");   }   catch(Exception ie){    System.out.println("不能读取配置文件!");   }       if (pool == null) {      JCO.addClientPool(TestJco.POOL_NAME, // pool name      5, // maximum number of connections      logonProperties); // properties   }   mConn = JCO.getClient(TestJco.POOL_NAME);      JCO.Repository mRepository;   mRepository = new JCO.Repository("ARAsoft", mConn);      try {    IFunctionTemplate ft =     mRepository.getFunctionTemplate("BAPI_COMPANYCODE_GETLIST");        JCO.Function fun = ft.getFunction();        mConn.execute(fun);       }   catch (Exception ex) {    System.out.println("生成RFC Funtion错误!");   }       System.out.println(mConn.getAttributes());   JCO.releaseClient(mConn); } }

你可能感兴趣的:(java,rfc调用)