Java中串連SAP進行RFC調用

來源:互聯網
上載者:User

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);
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.