java通过jco连接sap

首先我们需要用到的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;
	}



  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值