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<viewCodeList.size();i++){
if("K".equals(viewCodeList.get(i))){
function.getImportParameterList().setValue("A_BASIC_VIEW", "X");
}
if("E".equals(viewCodeList.get(i))){
function.getImportParameterList().setValue("A_BASIC_VIEW", "X");
}
}
function.getImportParameterList().setValue("A_BASIC_VIEW", "X");
for(int i=0;i<list.size();i++){
jtable.appendRow();
jtable.setRow(i);
jtable.setValue("MATERIAL", list.get(i));
}
function.execute(destination);
} catch (JCoException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
LoadBorrowMoneyBalance();
List list = new ArrayList();
list.add("000200000052");
Sapback(list);
list.add("0000000020");
//Sapback(list);
//System.out.println("12:34:56789".substring(0, 5));
String a = "12345_123";
int b = a.lastIndexOf("_");
System.out.println(a.substring(0,b));
System.out.println(a.substring(b+1));
}
}
本文介绍如何使用Java实现与SAP系统的连接,并通过示例代码演示了如何配置连接参数及执行基本的数据交互操作。
1815

被折叠的 条评论
为什么被折叠?



