JAVA鏈接RFC
public class SapLink {
/*
* 与SAP连接配置
*
* @author jay
*/
private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";
static {
Properties connectProperties = new Properties();
try {
connectProperties = PropertiesLoaderUtils.loadAllProperties("sap.properties");
System.out.println(connectProperties);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/*
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "192.168.116.27");// 服务器
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "00"); // 系统编号
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "120"); // SAP集团
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "G037901"); // SAP用户名
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "init123"); // 密码
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "ZF"); // 登录语言:ZH EN
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3"); // 最大连接数
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10"); // 最大连接线程*/
createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
}
/*
* 创建SAP接口属性文件。
*
* @param name
* ABAP管道名称
* @param suffix
* 属性文件后缀
* @param properties
* 属性文件内容
*/
private static void createDataFile(String name, String suffix, Properties properties) {
File cfg = new File(name + "." + suffix);
if (cfg.exists()) {
cfg.deleteOnExit();
}
try {
FileOutputStream fos = new FileOutputStream(cfg, false);
properties.store(fos, "for tests only !");
fos.close();
} catch (Exception e) {
System.out.println("Create Data file fault, error msg: " + e.toString());
throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
}
}
/*
* * 获取SAP连接
*
* @return SAP连接对象
*/
public static JCoDestination connect() {
JCoDestination destination = null;
try {
destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
System.out.println("destination===="+destination);
} catch (JCoException e) {
e.printStackTrace();
System.out.println("Connect SAP fault, error msg: " + e.toString());
}
return destination;
}
}
/**
鏈接測試test
*/
public static void main(String[] args) {
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
JCoFunction function = null;
JCoDestination destination = SapLink.connect();
try {
//调用ZFI_RFC002函数
function = destination.getRepository().getFunction("BAPI_MATERIAL_STOCK_REQ_LIST");
// System.out.println(function);
function.getImportParameterList().setValue("MATERIAL", "G09-6-30002-K01");// 参数
function.getImportParameterList().setValue("PLANT", "2050");// 参数
function.getImportParameterList().setValue("MATERIAL", "G09-6-30004-K01");// 参数
function.getImportParameterList().setValue("PLANT", "2050");// 参数
function.execute(destination);
// 遍历RFC返回的表对象
JCoTable tb = function.getTableParameterList().getTable("MRP_IND_LINES");
System.out.println(tb.getNumRows());
System.out.println(tb);
if(tb.getNumRows()!=0){
for (int i = 0; i < tb.getNumRows(); i++) {
tb.setRow(i);
System.out.println(tb.getValue("REC_REQD_QTY"));
System.out.println((String)tb.getValue("MRP_ELEMENT_IND"));
Map<String ,Object> map = new HashMap<String, Object>();
map.put("REC_REQD_QTY", tb.getValue("REC_REQD_QTY"));//資產編號
map.put("MRP_ELEMENT_IND", tb.getValue("MRP_ELEMENT_IND"));//資產子編號
/* map.put("BUKRS", (String) tb.getValue("BUKRS"));//公司代碼
map.put("WAERS", (String) tb.getValue("WAERS"));//幣別碼
map.put("GJAHR", (String) tb.getValue("GJAHR"));//會計年度
map.put("AKTIV", (Date) tb.getValue("AKTIV"));//資本化日期
map.put("DEAKT", (Date) tb.getValue("DEAKT"));//處分日期
map.put("ANSWL", (Object) tb.getValue("ANSWL"));//資產價值
map.put("KANSW", (Object) tb.getValue("KANSW"));//取得成本
map.put("KNAFA", (Object) tb.getValue("KNAFA"));//累計折舊
map.put("NAFAG", (Object) tb.getValue("NAFAG"));//當年折舊NAFAP
map.put("NAFAP", (Object) tb.getValue("NAFAP"));//一般折舊*/
list.add(map);
System.out.println(list);
}
}else{
System.out.println("無數據");
}
}catch (Exception e) {
e.printStackTrace();
}
}
/**
配置文件
*/
#jco.client.ashost=####
jco.client.client=800
jco.client.sysnr=00
jco.client.lang=ZF
jco.client.user=*****
jco.client.passwd=*****
jco.destination.pool_capacity=3
jco.destination.peak_limit=10
jco.client.destination=ABAP_AS_WITH_POOL
jco.destination.auth_type=CONFIGURED_USER
propertiesProvider=com.sap.conn.jco.rt.PropertyFileDestinationDataProvider
jco.client.mshost=####
jco.client.msserv=3601
jco.client.group=Lotes_All