JAVA调用SAP端RFC接口

JAVA调用SAP端接口 
1、这里我们不讲述SAP端接口的开发,sap端接口已经写好了,java端只是调用sap的接口名;

2、调用过程中所需要的包及配置文件,在“java连接sap接口包sapjco3”中可以直接下载使用。

本博文中提供两个例子:

1、检查SAP商品SN码;

2、查询SAP采购单 。

SAPConn.java即JAVA与sap连接代码 

package com.pcmall;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;

import org.apache.log4j.Logger;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.DestinationDataProvider;

/**
 * 与SAP连接配置
 * @author jay
 */
public class SAPConn {
	private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";
	static{
		Properties connectProperties = new Properties();
		connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "xxx.xxx.xxx.xxx");//服务器
		connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  "xxx");        //系统编号
		connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "xxx");       //SAP集团
		connectProperties.setProperty(DestinationDataProvider.JCO_USER,   "xxx");  //SAP用户名
		connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "xxx");     //密码
		connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   "zh");        //登录语言
		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){
			log.error("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);
		} catch (JCoException e) {
			log.error("Connect SAP fault, error msg: " + e.toString());
		}
		return destination;
	}
	private static Logger log = Logger.getLogger(SAPConn.class); // 初始化日志对象
}

检查SAP商品SN码

JAVA调用SAP端RFC接口_第1张图片

调用接口代码如下

package com.pcmall;

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoTable;

public class CheckSnFromSAP {
	public static void main(String[] args) {
		JCoFunction function = null;
		JCoDestination destination = SAPConn.connect();
		String result="";//调用接口返回状态
		String message="";//调用接口返回信息
		try {
			//调用ZRFC_GET_REMAIN_SUM函数
			function = destination.getRepository().getFunction("ZFMPOS_SN_CHECK");
			JCoParameterList input = function.getImportParameterList();
			//发出扫码仓库
			input.setValue("ZSNWERKS", "1000");
			//发出扫码库位
			input.setValue("ZSNLGORT", "0001");
			//采购凭证号
			input.setValue("EBELN", "1");
			
			//获取传入表参数SN_ITEM
			JCoTable SN_ITEM = function.getTableParameterList().getTable("SN_ITEM");
			SN_ITEM.appendRow();//增加一行
			//给表参数中的字段赋值,此处测试,就随便传两个值进去
			//商品编码
			SN_ITEM.setValue("MATNR", "1");
			//商品序列号
			SN_ITEM.setValue("ZSERIAL", "1");
			function.execute(destination);
			result= function.getExportParameterList().getString("RESULT");//调用接口返回状态
			message= function.getExportParameterList().getString("MESSAGE");//调用接口返回信息
			System.out.println("调用返回状态--->"+result+";调用返回信息--->"+message);
			SN_ITEM.clear();//清空本次条件,如果要继续传入值去或者还要循环,那得将之前的条件清空
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}
测试输出结果如下,说明SAP接口已经调通

JAVA调用SAP端RFC接口_第2张图片


查询SAP采购单 

JAVA调用SAP端RFC接口_第3张图片

调用接口代码如下

package com.pcmall;

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoTable;

public class GetPoFromSAP {
	public static void main(String[] args) {
		JCoFunction function = null;
		JCoDestination destination = SAPConn.connect();
		String result="";//调用接口返回状态
		String message="";//调用接口返回信息
		try {
			//调用ZRFC_GET_REMAIN_SUM函数
			function = destination.getRepository().getFunction("ZFMPOS_PO_QUERY");
			JCoParameterList input = function.getImportParameterList();
			//采购凭证号
			input.setValue("EBELN", "4500004135");
			//单据类型
			input.setValue("BSART", "TR");
			//制单日期从
			input.setValue("AEDATF", "");
			//制单日期到
			input.setValue("AEDATT", "");
			//出入库标识
			input.setValue("INOUT", "I");
			//地点库位权限值
			input.setValue("AUTHORITY", "");
			
			function.execute(destination);
			result= function.getExportParameterList().getString("RESULT");//调用接口返回状态
			message= function.getExportParameterList().getString("MESSAGE");//调用接口返回信息
			
			if(result.equals("E")){
				System.out.println("调用返回状态--->"+result+";调用返回信息--->"+message);
				return;
			}else{
				System.out.println("调用返回状态--->"+result+";调用返回信息--->"+message);
				JCoParameterList tblexport = function.getTableParameterList();
				//JCoParameterList tblexport = function.getTableParameterList().getTable("QUERY_H");
				String msg = tblexport.toXML();
				System.out.println("调用返回表XML--->"+msg);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}

测试输出结果如下,说明SAP接口已经调通

JAVA调用SAP端RFC接口_第4张图片
具体的XML文本内容如下,发现返回有两张表,分别为QUERY_H、QUERY_I主从表,,得到该XML内容,则JAVA端可以对此解析,得到所需要业务信息。




  
    
      4500004135
      04
      ZDN
      1
      2014-10-19
      1000
      
      
      
      中国
      上海
      南京
      中国上海南京1000新地址
      210000
      888888881
      
      王五(新)
      
      
      0
      
      
      
      0
      
      IBD
    
  
  
    
      4500004135
      00001
      3
      1000
      木头若愚
      0001
      仓库
      1000
      木头若愚
      0002
      坏件库
      20141019
      1
      
      
      0
    
    
      4500004135
      00002
      2
      1000
      木头若愚
      0001
      仓库
      1000
      木头若愚
      0002
      坏件库
      20141019
      1
      
      
      0
    
    
      4500004135
      00003
      1
      1000
      木头若愚
      0001
      仓库
      1000
      木头若愚
      0002
      坏件库
      20141019
      1
      
      
      2
    
  

你可能感兴趣的:(SAP,Java)