JCO简介
JCO是Java Connector的简称,它封装了JAVA-enabled RFC,实现了基于RFC的BAPI与JAVA接口。它提供结构化的BAPI调用,目前不支持面向对象的开发。 JCO使用
1)类导入
import com.sap.mw.jco.*;
2)建立R3连接,有两种方法:
a.持久连接
-
//申明连接
-
JCO.Client mConnection;
-
// 初始化连接
-
mConnection =
-
JCO.createClient("001", // SAP client
-
"<userid>", // userid
-
"****", // password
-
"EN", // language (null for the default language)
-
"<hostname>", // application server host name
-
"00"); // system number
-
//建立连接
-
try {
-
mConnection.connect();
-
}
-
catch (Exception ex) {
-
ex.printStackTrace();
-
System.exit(1);
-
}
-
//关闭连接
-
mConnection.disconnect();
-
复制代码
b.连接池方式
-
static final String POOL_NAME = "Pool";
-
JCO.Pool pool = JCO.getClientPoolManager().getPool(POOL_NAME);
-
if (pool == null) {
-
OrderedProperties logonProperties =
-
OrderedProperties.load("/logon.properties");
-
JCO.addClientPool(POOL_NAME, // pool name
-
5, // maximum number of connections
-
logonProperties); // properties
-
mConnection = JCO.getClient(POOL_NAME);
-
System.out.println(mConnection.getAttributes());
-
}
-
catch (Exception ex) {
-
ex.printStackTrace();
-
}
-
。。。。。。
-
finally {
-
JCO.releaseClient(mConnection);
-
}
-
复制代码
3)获得BAPI方法
-
JCO.Repository mRepository;
-
mRepository = new JCO.Repository("ARAsoft", mConnection);
-
IFunctionTemplate ft =
-
try{
-
mRepository.getFunctionTemplate(“BAPI_SALESORDER_GETSTATUS”);
-
}
-
catch (Exception ex) {
-
throw new Exception("Problem retrieving JCO.Function object.");
-
}
-
// Create a function from the template
-
jcoFunction = new JCO.Function(ft);
-
复制代码
4)设定输入参数
-
JCO.Field SalesDocumentField = jcoFunction.getImportParameterList().getField("SALESDOCUMENT");
-
SalesDocumentField.setValue(iSalesDocument);
-
复制代码
5)执行BAPI
-
mConnection.execute(jcoFunction);
复制代码
6)处理“return”参数
-
JCO.Structure jcoBapiReturn = jcoFunction.getExportParameterList().getStructure("RETURN");
-
-
if ((jcoBapiReturn.getField("TYPE").getValue()).toString().equals("E"))
-
throw new Exception();
-
复制代码
7) 获得返回值
-
JCO.Table jcoStatusInfo = jcoFunction.getTableParameterList().getTable("STATUSINFO");
复制代码
8)最后是异常的捕获与处理
JCO开发建议
基于JAVA语言的特征,建议以如下的方式开发JCO应用。
1)主要的类:
输入参数封装到java bean类中,并统一进行有效性检查。
针对所要调用的BAPI建立代理类,将BAPI的业务功能封装起来。
建立一个解释类,负责调用业务功能类,并将参数从java beans传递到业务功能类。
2)流程:
|