|
关于jco远程调用sap的例子也比较多,在此,本人针对自己的项目做一个比较简单完整的整理,希望对初次接触的人能有所帮助。
步骤主要有一下几点:
1.开发环境配置
1.1首先要把librfc32.dll,sapjcorfc.dll 这两个文件放到服务器的系统的c:\windows\system32目录下。
1.2下载 sap.jar;sapjco.jar;sappool.jar放到JAVA应用下.
2.连接池配置,由于调用sap里的相关函数,因此,连接池还是配置一个公共的比较好,不至于每次都配置。
配置文件名为:SAPLogon.properties
#配置client
jco.client.client=800
#sap账号
jco.client.user=zhangsan
#sap账号密码
jco.client.passwd=654321
#sap服务器IP地址,如果是外网访问,则在内网IP前加/H/外网IP/H/192.168.0.88
jco.client.ashost=192.168.0.88
#系统编号
jco.client.sysnr=00
#语言
jco.client.lang=en
3.编写java类
package com.thinkway.erp360.presentation.web.controller.sap;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import com.sap.mw.jco.IFunctionTemplate;
import com.sap.mw.jco.IRepository;
import com.sap.mw.jco.JCO;
import com.sap.mw.jco.JCO.PoolManager;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
import com.thinkway.erp360.business.domains.SalesOrderDeliver;
import com.thinkway.erp360.util.DataMethod;
public class Testrfc {
private static final String SAPHR_POOL_NAME = "SAP_Poolssss";
public static void getSAPPoolConnection() throws IOException {
PoolManager clientPoolManager = JCO.getClientPoolManager();
//JCO.Client mClient =null;
try{
JCO.Pool pool = clientPoolManager.getPool(SAPHR_POOL_NAME);
if (pool == null) {
Properties props = new Properties();
InputStream is = ZrfcSalesOrder.class.getResourceAsStream("SAPLogon.properties");
props.load(is);
JCO.addClientPool(SAPHR_POOL_NAME, // pool name
10, // maximum number of connections
props); // properties
}
//mConnection = JCO.getClient(SAPHR_POOL_NAME);
}catch (Exception ex) {
System.err.println(ex.getMessage());
}
finally{
//JCO.releaseClient(mConnection);
}
//return mClient;
}
public static void setConnection(){
try{
// mConnection = JCO.createClient("400",
// "rfcuser",
// "123456",
// "en",
// "192.168.10.88",
// "00");
}catch(Exception ex){
}
}
//查询接口:调用Z_ERP360_SALESORDER_GETDELIVER函数
public static void querySalesOrderGetlist(){//()throws IOException{
// setConnection();
DataMethod date = new DataMethod();
try {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
"ddMMyyyy");
java.text.SimpleDateFormat sdf1 = new java.text.SimpleDateFormat(
"ddMMyyyy");
JCO.Client client;
getSAPPoolConnection();
IRepository myRepository = JCO.createRepository(
"MYRepository", SAPHR_POOL_NAME);
System.out.println(myRepository.toString());
IFunctionTemplate ft = myRepository
.getFunctionTemplate("Z_ERP360_SALESORDER_GETDELIVER");
JCO.Function funGetList = ft.getFunction();
JCO.ParameterList input = funGetList
.getImportParameterList();
String today=(date.getCurrentDate()).toString(); //获取今天日期,并转换为String
//输入参数
input.setValue(sdf.parse("01012012"), "BDATE");
input.setValue(sdf.parse(today), "EDATE");
input.setValue("Z000", "IM_WERKS");
client = JCO.getClient(SAPHR_POOL_NAME);
client.execute(funGetList);
JCO.Table tDateRange = funGetList.getTableParameterList()
.getTable("T_DATA"); //SAP中的输出结构名称为T_DATA
System.out.println("tDateRange:" + tDateRange.getNumRows());
SalesOrderDeliver item = new SalesOrderDeliver();
for (int i = 0; i < tDateRange.getNumRows(); i++) {
tDateRange.setRow(i);
item.setAEDAT(tDateRange.getString("LFDAT"));
item.setBEZEI(tDateRange.getString("BEZEI"));
item.setBLDAT(tDateRange.getString("BLDAT"));
item.setBEZEI2(tDateRange.getString("BEZEI2"));
item.setWADAT(tDateRange.getString("WADAT"));
item.setKUNNR(tDateRange.getString("KUNNR"));
item.setLFIMG(tDateRange.getFloat("LFIMG"));
item.setARKTX(tDateRange.getString("ARKTX"));
item.setMATNR(tDateRange.getString("MATNR"));
//item.setMEINS(tDateRange.getString("MEINS"));
item.setPOSNR(tDateRange.getString("POSNR"));
item.setVBELN(tDateRange.getString("VBELN"));
item.setLFART(tDateRange.getString("LFART"));
item.setVKBUR(tDateRange.getString("VKBUR"));
item.setVKGRP(tDateRange.getString("VKGRP"));
item.setVKORG(tDateRange.getString("VKORG"));
item.setVRKME(tDateRange.getString("VRKME"));
item.setVTEXT(tDateRange.getString("VTEXT"));
item.setKODAT(tDateRange.getString("KODAT"));
System.out.println("Record ["
+ tDateRange.getString("VKORG") + "] 物料号= "
+ (tDateRange.getString("MATNR")).substring(10, 18) + " 供应商= "
+ tDateRange.getString("KUNNR") + ", 交货日期 "
+ tDateRange.getString("KODAT"));
}
} catch (Exception e) {
// TODO: handle exception
}
};
public void closeConnection(){
// mConnection.disconnect();
}
public static final void main(String[] args){
querySalesOrderGetlist();
}
}
4.SAPLogon.properties和业务类在同一包下,此时可以测试了。
5.为了更清楚,附件截图为SAP登陆账号,以及函数的一些截图,希望对初次接触的人有所帮助。 |
|
|