java与SAP rfc建立数据传递

阅读更多

1:首先下载和SAP创建链接的jar包jco3.jar,然后跟指定SAP服务器建立连接,想要灵活性的话可以写个配置文件

package hfagro.mdm.core.utils;




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

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 wy 
 */
public class SapConnect {
	private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";
	static{
		Properties connectProperties = new Properties();

		
		//test
		connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "192.168.1.58");
		connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  "00");        
		connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "300");       
		connectProperties.setProperty(DestinationDataProvider.JCO_USER,   "OA_RFC");  
		connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "15235456");     
		connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   "ZH");        
		//connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3");  
		//connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10");    
		connectProperties.setProperty(DestinationDataProvider.JCO_SAPROUTER,"/H/218.92.167.235/H/");  
 
		
		
		
		createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
	}
	
	
	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){
			System.out.println("Create Data file fault, error msg: " + e.toString());
			throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
		}
	}
	
	
	public static JCoDestination connect(){
		JCoDestination destination =null;
		try {
			destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
		} catch (JCoException e) {
			System.out.println("Connect SAP fault, error msg: " + e.toString());
		}
		return destination;
	}
	
}

 2:进行数据传递

package hfagro.mdm.core.utils;

import java.util.ArrayList;
import java.util.List;

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

import hfagro.mdm.core.master.dto.AllViewInfo;

/**
 * ��sap��ȡ�ý�����
 * @author kmd
 * 2013-06-05
 *
 */
public class test1 {


	public static void Sapback(List list){
		JCoFunction function = null;

		JCoDestination destination = SapConnect.connect();
		AllViewInfo allViewInfo =null;
		try {
//			function = destination.getRepository().getFunction("ZFUN_QM_QMEL_INBOUND");
			function = destination.getRepository().getFunction("ZFUN_MDM_MATERIAL_CRT");

			JCoTable jtable = function.getTableParameterList().getTable("I_INPUT");
			allViewInfo = new AllViewInfo();
			jtable.setValue("MATERIAL", allViewInfo.getMaterial());
			jtable.setValue("MATL_TYPE", allViewInfo.getMatlType());
			jtable.setValue("MATL_DESC_ZH", allViewInfo.getMatlDescZh());
			List viewCodeList = new ArrayList();
			for(int i=0;i 
  

 

  • jco.jar.rar (3.1 MB)
  • 下载次数: 0

你可能感兴趣的:(java)