您的位置:首页 > 编程语言 > Java开发

java通过jco连接sap

2016-01-07 10:33 639 查看
首先我们需要用到的jar包为sapjco3.jar,以及其他相关配置文件下载如下:点击打开链接

windows环境下只需要把sapjco3.dll、sapjco3.jar文件放置在jdk安装目录的lib目录下:



若在linux环境下首先在/usr/local目录下新建一个sapjco的文件夹,将libsapjco.so文件放置在该目录下,并将jar包放置在jdk的lib目录下,然后修改/rtc目录下的profile配置:





至此环境jco环境搭建完毕,下面以连接sap的qas系统为例来说明java如何连接sap:



新建一个用来连接的java类:

public class ConnectionRFC {
public static String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";
static {
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "192.168.130.230"); //主机地址
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  "00");     	//系统编号
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "889");     	//客户端
connectProperties.setProperty(DestinationDataProvider.JCO_USER,   "CHIGOWB"); 	//用户名
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "igowb1");  	//密码
connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   "");    	  	//登入语言
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "2");  //最大空闲连接数
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT,    "20"); //最大活动链接数
createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
}

//在编译环境生成上述配置文件
static void createDataFile(String name, String suffix, Properties properties){
String webRoot=System.getProperty("user.dir");
webRoot =webRoot+File.separator+name+"."+suffix;
File cfg = new File(webRoot);
if(!cfg.exists()) {
try{
FileOutputStream fos = new FileOutputStream(cfg, false);
properties.store(fos, "for tests only !");
fos.close();
}catch (Exception e){
throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
}
}
}

}

所要连接的rfc如下:






连接rfc,填入导入字段即可获取导出的信息已经表数据:

// 获取图片信息
public List<Object> getImgInfo(String inuid,String itype){
List<Object> lo = new ArrayList<Object>();
List<Img> l = new ArrayList<Img>();
try {
JCoDestination destination = JCoDestinationManager
.getDestination(ConnectionRFC.ABAP_AS_POOLED);
JCoFunction function = destination.getRepository().getFunction(
"ZRFC_NGGET_IMAGE_EDIT");// 获取RFC
System.out.println("RFC connected");
// 填入所需的导入字段
if (function != null) {
JCoParameterList parameterList = function
.getImportParameterList();
parameterList.setValue("IUNID", inuid);
parameterList.setValue("ITYPE", itype);
}
// 执行调用
function.execute(destination);

JCoParameterList list = function.getTableParameterList();
// 获取表数据
JCoTable table = list.getTable("TBPICPATH");
// 循环表
if (!table.isEmpty()) {
// System.out.println(table.toString());
for (int i = 0; i < table.getNumRows(); i++) {
table.setRow(i);
// 获取字段
Img img = new Img();
img.setPIC_ID(table.getString("PIC_ID"));
img.setRELATION_ID(table.getString("RELATION_ID"));
img.setPICTURE_PATH(table.getString("PICTURE_PATH"));
img.setIMAGE_DESC(table.getString("IMAGE_DESC"));
img.setIMAGE_SORT_NUMBER(table.getString("IMAGE_SORT_NUMBER"));
l.add(img);
System.out.println("id:"+l.get(i).getPIC_ID()+",path:"+l.get(i).getPICTURE_PATH());
}
}else{
l = null;
}
lo.add(l);
JCoParameterList jpl = function.getExportParameterList();
String path = jpl.getString("OPICTURE_PATH");
System.out.println("OPICTURE_PATH:"+path);
lo.add(path);
} catch (JCoException e) {
e.printStackTrace();
}
return lo;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: