博客参考:http://blog.youkuaiyun.com/jay_1989/article/details/51821069 (可以说我是基本照抄,所以本篇博客为转载)
其实在写这篇博客之前,已经有很多前辈写过类似的技术博客,
所以我这篇博客算是对我自己技术的一个记录。
首先。最基本的准备你需要有一下两个小东西(根据自己电脑装的jdk去下载)
- sapjco3.jar
- sapjco3.dll
sapjco3.dll,你可以将他放在WEB-INF\lib目录下。也可以将相对应位数的sapjco3.dll文件拷贝至system32文件下。
博主是将sapjco3.dll文件放在WEB-INF\lib
多的不说了,上代码
SAPConn.java即JAVA与sap连接代码
/**
* 与SAP连接配置
* @author changcheng
*/
public class SAPConn {
private static Logger log = Logger.getLogger(SAPConn.class);
private static final String ABAP_AS_POOLED = "ECD";//连接池名
static{
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "***.***.***.***"); //服务器开发机
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "*"); //系统编号
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "*"); //SAP集团 开发机
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "x"); //账户
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "*"); //密码
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "ZH"); //登录语言
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "30"); //最大连接数
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "100"); //最大连接线程
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();
}
/*
* Properties类是集合和IO技术结合的成果.可以把数据以Map集合的形式存入,也可以通过IO流技术读取或者输出.
* 该类的store方法,是一个输出方法. store(OutputStream out, String comments)
* 以适合使用 load(InputStream) 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。
*/
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);//获取SAP连接
} catch (JCoException e) {
log.error("Connect SAP fault, error msg: " + e.toString());
}
return destination;
}
}
SAPtest。连接sap获取到具体sap接口
public class SAPtest {
public static void main(String[] args) {
try {
//连接SAP,获取一个连接对象
JCoDestination lj = SAPConn.connect();
//获取到SAP的函数
JCoFunction function = lj.getRepository().getFunction("ZEPIC_ACCT_DOC_POST");
//将该函数的结构打印出来
System.out.println(SAPUtil.formatXML(function.toXML()));
//该入参没有放在结构表或者表格中,直接使用getImportParameterList方法
JCoParameterList imP = function.getImportParameterList();
//利用setValue将入参参数传入
imP.setValue("key", "value");
//入参结构表时,使用JCoStructure声明对象
//IS_ORDER表示该结构的名称
JCoStructure stu = function.getImportParameterList().getStructure("IS_ORDER");
stu.setValue("key", "value");
stu.setValue("key", "value");
stu.setValue("key", "value");
stu.setValue("key", "value");
stu.setValue("key", "value");
//入参表格 表格是具有多行数据的
JCoTable jCoTable = function.getTableParameterList().getTable("IT_ITEMS");
jCoTable.appendRow();//增加一行
jCoTable.setValue("key", "value");
//执行将数据传给sap
function.execute(lj);
//既然是传给,那sap也会给你返回信息,首先你的看清楚返回的时表格还是结构
//返回的是表格
//获取到该表格的名称
JCoTable jCoTable1 = function.getTableParameterList().getTable("ET_TTCLEAR");
for (int i = 0; i < jCoTable.getNumRows(); i++) {
//定位到当前行
jCoTable.setRow(i);
//然后将字段数据一一拿出
String str = jCoTable.getString("字段名称");//为了方便
}
//返回的时结构
//获取到该结构的名称
JCoStructure jCoStructure = function.getExportParameterList().getStructure("ES_RETURN");
//然后将数据拿出
String str1 = jCoStructure.getString("字段名称");
String str2 = jCoStructure.getString("字段名称");
/**
* 接下来就时用SAP返回给你的数据处理自己的业务了
*/
} catch (Exception e) {
e.printStackTrace();
}
}
}