添加工具类:
package utils;
import java.util.*;
import java.io.*;
@SuppressWarnings({"serial","rawtypes","unchecked"})
public class OrderedProperties extends java.util.Properties {
public ArrayList orderedKeys = new ArrayList();
public OrderedProperties() {
super();
}
public OrderedProperties(java.util.Properties defaults) {
super(defaults);
}
public synchronized Iterator getKeysIterator() {
return orderedKeys.iterator();
}
public static OrderedProperties load(String name)
throws IOException {
OrderedProperties props = null;
java.io.InputStream is =
OrderedProperties.class.getResourceAsStream(name);
if ( is != null ) {
props = new OrderedProperties();
props.load(is);
return props;
} else {
if ( ! name.startsWith("/") ) {
return load("/" + name);
} else {
throw new IOException("Properties could not be loaded.");
}
}
}
public synchronized Object put(Object key, Object value) {
Object obj = super.put(key, value);
orderedKeys.add(key);
return obj;
}
public synchronized Object remove(Object key) {
Object obj = super.remove(key);
orderedKeys.remove(key);
return obj;
}
}
再创建一个RfcManager的类(DataBean),用来读取src目录下的sap_conf.properties 来注册Provider,获取RFC Function和执行execute()方法。
sap_conf.properties
#LTJ 2018-05-02
#IP addr
#JCO_ASHOST=160.46.86.57:3620
#SYSTEM CODE
#JCO_SYSNR=30
#CLIENT
#JCO_CLIENT=100
#USER NAME
#JCO_USER=RFC_SCTS
#PASSWORD
#JCO_PASSWD=start567
#LANGUAGE
#JCO_LANG=EN
#MAX NOUSE LINK
#JCO_POOL_CAPACITY=50
#MAX ACTIVE LINK
#JCO_PEAK_LIMIT=30
jco.client.client=100
jco.client.user=RFC_SCTS
jco.client.passwd=start567
jco.client.ashost=tchdcs20.bmwbrill.cn
jco.client.sysnr=20
jco.client.lang=EN
jco.client.group=PUBLIC
jco.client.r3name=CHD
jco.client.msserv=3620
jco.client.mshost=tchdcs20.bmwbrill.cn
DataBean.java
package utils;
import com.sap.mw.jco.*;
import com.sap.vo.SctsAfcAsnCvVO;
import com.sap.vo.SctsAfcPrednCvVO;
import com.sap.vo.SctsJisAsnCvVO;
import com.sap.vo.SctsJitCallofCvVO;
import com.sap.vo.SctsLoadunitVO;
import com.sap.vo.SctsPackageVO;
import com.sap.vo.SctsPartVO;
import com.sap.vo.SctsPlantCalendarVO;
import com.sap.vo.SctsSupplierVO;
import com.sap.vo.SctsTransportRateVO;
import java.text.SimpleDateFormat;
import java.util.*;
@SuppressWarnings({"deprecation","unused"})
public class DataBean {
static final String POOL_NAME = "ConnectionPool";
static final String CHAR_ENCODING = "gbk";
static final String CHARSET_NAME = "8859_1";
//private JCO.Client aConnection;
private IRepository aRepository;
String encyptionInfo="";
public DataBean() {
createConnection();
retrieveRepository();
}
private void createConnection() {
try {
JCO.Pool pool = JCO.getClientPoolManager().getPool( POOL_NAME );
if ( pool == null ) {
OrderedProperties logonProperties = OrderedProperties.load( "/sap_conf.properties" );
JCO.addClientPool(POOL_NAME, // pool name
15, // maximum number of connections
logonProperties); // properties
}
}
catch ( Exception ex ) {
ex.printStackTrace();
}
}
public void releaseConnection( JCO.Client aConnection ) {
JCO.releaseClient( aConnection );
}
private void retrieveRepository() {
try {
aRepository = new JCO.Repository( "SAPRep", POOL_NAME );
}
catch ( Exception ex ) {
System.out.println( "Failed to retrieve SAP repository" );
}
}
public JCO.Function getFunction( String name ) {
try {
return aRepository.getFunctionTemplate( name.toUpperCase() ).getFunction();
}
catch ( Exception ex ) {
System.out.println(ex.getMessage());
return null;
}
}
/***
* @param functionName 程序名
* @param tableName 输出参数
* @return Map<Integer, List<Map<String,String>> >
*/
public Map<Integer, List<Map<String,String>> > getParameters(String functionName, String tableName){
Map<Integer, List<Map<String,String>> > map =new HashMap<>();
//使用jco连接
JCO.Client client = JCO.getClient(POOL_NAME);
try {
// 获取RFC对象
JCO.Function function = getFunction(functionName);
// 设置import 参数
JCO.ParameterList listParams = function.getImportParameterList();
listParams.setValue( "0010139910", "IV_LIFNR" );
listParams.setValue( "SY78", "IV_WERKS");
// 执行RFC
client.execute( function );
//获取table数据
JCO.Table tab = function.getTableParameterList().getTable(tableName);
for(int i = 0 ; i < tab.getNumRows() ;i ++) {
//设置行
tab.setRow(i);
String s = "" ;
List<Map<String,String>> list = new ArrayList<>();
for(int j = 0 ; j < tab.getNumColumns() ; j ++) {
Map<String,String> maps= new HashMap<>();
s = (String) tab.getField(j).getValue();
maps.put(tab.getName(j), s);
list.add(maps);
map.put(i, list);
}
}
System.out.println(map);
}
catch ( JCO.AbapException ex ) {
if ( ex.getKey().equalsIgnoreCase( "ERROR" ) ) {
System.out.println( "-----------There is no record in the table ZQC_QA_INTERFACE.-----------" );
}
else {
System.out.println( ex.getMessage() );
}
}
catch ( Exception ex ) {
System.out.println( "Caught an exception: \n" + ex );
}
finally {
JCO.releaseClient( client );
}
return map;
}
}
我这里用的springMVC,前端通过页面调用controller层调用DataBean.getParameters()方法即可获取到SAP的参数值了。