package org.erp.xy.util;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
import com.sap.conn.jco.AbapException;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoField;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoStructure;
import com.sap.conn.jco.ext.DestinationDataProvider;
public class SAPJCO {
static String ABAP_AS1 = "ABAP_AS_WITHOUT_POOL1";
static {
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST,
"********");IP
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "00");
connectProperties
.setProperty(DestinationDataProvider.JCO_CLIENT, "500");
connectProperties.setProperty(DestinationDataProvider.JCO_USER,
"Q****")账号
// 注:密码是区分大小写的,要注意大小写
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD,
"********");密码
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "en");
// 需要将属性配置保存属性文件,该文件的文件名为 ABAP_AS_WITHOUT_POOL.jcoDestination,
// JCoDestinationManager.getDestination()调用时会需要该连接配置文件,后缀名需要为jcoDestination
createDataFile(ABAP_AS1, "jcoDestination", connectProperties);
}
// 基于上面设定的属性生成连接配置文件
static void createDataFile(String name, String suffix, Properties properties) {
File cfg = new File(name + "." + suffix);
if (!cfg.exists()) {
try {
FileOutputStream fos = new FileOutputStream(cfg, false);
properties.store(fos, "for tests only !");
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void connectWithoutPool() throws JCoException {
// 到当前类所在目录中搜索 ABAP_AS_WITHOUT_POOL.jcoDestination
// 属性连接配置文件,并根据文件中的配置信息来创建连接
JCoDestination destination = JCoDestinationManager
.getDestination(ABAP_AS1);// 只需指定文件名(不能带扩展名jcoDestination名,会自动加上)
System.out.println("Attributes:");
// 调用destination属性时就会发起连接,一直等待远程响应
System.out.println(destination.getAttributes());
}
/* public static void main(String[] args) throws JCoException {
connectWithoutPool();
JCoDestination destination = JCoDestinationManager
.getDestination(ABAP_AS1);
System.out.println("1111111111111111"+destination);
JCoFunction function = destination.getRepository().getFunction(
"ZTMXT");
if (function == null)
throw new RuntimeException(
"RFC_SYSTEM_INFO not found in SAP.");
try {
JCoParameterList parameterList = function
.getImportParameterList();//传参
parameterList.setValue("ZTXMSZ", "1000004");
parameterList.setValue("ZCBZX1", "1000fvc000");
parameterList.setValue("ZSL", "20");
function.execute(destination);
JCoParameterList exportParam = function.getExportParameterList();
String fhxx = exportParam.getString("ZFHXX");
System.out.println(fhxx);
String fhxx1 = exportParam.getString("ZFHXX1");
System.out.println(fhxx1);
String fhxx2 = exportParam.getString("ZFHXX2");
System.out.println(fhxx2);
} catch (AbapException e) {
System.out.println(e.toString());
return ;
}
//if(function!=null) {
System.out.println("函数存在");
// JCoStructure exportStructure = function.getExportParameterList()
// .getStructure("RFCSI_EXPORT");
// System.out.println("System info for "
// + destination.getAttributes().getSystemID() + ":\n");
// for (int i = 0; i < exportStructure.getMetaData().getFieldCount(); i++) {
// System.out.println(exportStructure.getMetaData().getName(i) + ":\t"
// + exportStructure.getString(i));
// }
// System.out.println();
// System.out.println("The same using field iterator: \nSystem info for "
// + destination.getAttributes().getSystemID() + ":\n");
// for (JCoField field : exportStructure) {
// System.out.println(field.getName() + ":\t" + field.getString());
// }
// System.out.println();
// System.out.println("RFCPROTO:\t"+exportStructure.getString(0));
// System.out.println("RFCPROTO:\t"+exportStructure.getString("RFCPROTO"));
// System.out.println(exportStructure);
// }
else {
System.out.println("函数不存在");
throw new RuntimeException(
"RFC_SYSTEM_INFO not found in SAP.");
}
if (function == null)
throw new RuntimeException(
"RFC_SYSTEM_INFO not found in SAP.");
try {
function.execute(destination);
} catch (AbapException e) {
System.out.println(e.toString());
return ;
}
JCoStructure exportStructure = function.getExportParameterList()
.getStructure("ZFYCKM");
System.out.println("System info for "
+ destination.getAttributes().getSystemID() + ":\n");
for (int i = 0; i < exportStructure.getMetaData().getFieldCount(); i++) {
System.out.println(exportStructure.getMetaData().getName(i) + ":\t"
+ exportStructure.getString(i));
}
System.out.println();
// JCo still supports the JCoFields, but direct access via getXX is more
// efficient as field iterator 也可以使用下面的方式来遍历
System.out.println("The same using field iterator: \nSystem info for "
+ destination.getAttributes().getSystemID() + ":\n");
for (JCoField field : exportStructure) {
System.out.println(field.getName() + ":\t" + field.getString());
}
System.out.println();
//*********也可直接通过结构中的字段名或字段所在的索引位置来读取某个字段的值
System.out.println("RFCPROTO:\t"+exportStructure.getString(0));
System.out.println("RFCPROTO:\t"+exportStructure.getString("RFCPROTO"));
}*/
}