package com.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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.JCoField;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoStructure;
import com.sap.conn.jco.JCoTable;
import com.sap.conn.jco.ext.DestinationDataProvider;
public class SAPService {
static String ABAP_AS = "ABAP_AS_WITHOUT_POOL";
static String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";
static String ABAP_MS = "ABAP_MS_WITHOUT_POOL";
public SAPService()
{
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("sap.properties");
Properties p = new Properties();
try {
p.load(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
}
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, p.getProperty("JCO_ASHOST"));
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, p.getProperty("JCO_SYSNR"));
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, p.getProperty("JCO_CLIENT"));
connectProperties.setProperty(DestinationDataProvider.JCO_USER, p.getProperty("JCO_USER"));
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, p.getProperty("JCO_PASSWD"));
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, p.getProperty("JCO_LANG"));
createDataFile(ABAP_AS, "jcoDestination", connectProperties);
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, p.getProperty("JCO_POOL_CAPACITY"));
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, p.getProperty("JCO_PEAK_LIMIT"));
createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
}
private 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)
{
throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
}
}
}
//内表
public String WorkWithTable()
{
try
{
//获取连接池
JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
//获取功能函数
JCoFunction function = destination.getRepository().getFunction("ZRFC_MARA_INFO");
if(function == null)
throw new RuntimeException("BAPI_COMPANYCODE_GETLIST not found in SAP.");
//给功能函数输入参数
JCoParameterList input = function.getImportParameterList();
input.setValue("MATNR","650-2M17002-00R0");
try
{
function.execute(destination); //函数执行
}
catch(AbapException e)
{
System.out.println(e.toString());
return e.toString()+"";
}
//获取输出
JCoTable rs = function.getTableParameterList().getTable("IT_MARA");
for (int i = 0; i < rs.getNumRows(); i++) {
System.out.println(rs.getString("MATNR"));
return rs.getString("MATNR");
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return "null";
}
//结构
public String WorkWithStructure()
{
try{
JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
JCoFunction function = destination.getRepository().getFunction("RFC_SYSTEM_INFO");
if(function == null)
throw new RuntimeException("BAPI_COMPANYCODE_GETLIST not found in SAP.");
try
{
function.execute(destination);
}
catch(AbapException e)
{
System.out.println(e.toString());
return null;
}
//返回结构
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();
}catch(Exception ex)
{
ex.printStackTrace();
}
return null;
}
public static void main(String[] args) {
SAPService sap=new SAPService();
sap.WorkWithTable();
sap.WorkWithStructure();
}
}
java 使用JCO3 连接SAP实例项目
5星 · 超过95%的资源 需积分: 33 55 浏览量
2012-03-19
14:42:25
上传
评论 1
收藏 1.04MB ZIP 举报
S3177
- 粉丝: 2
- 资源: 10
最新资源
- mongodb数据库基本操作.pdf
- C#,布尔可满足性问题(Boolean Satisfiability Problem)算法与源代码
- C#,回文分割问题(Palindrome Partitioning Problem)算法与源代码
- C#,煎饼排序问题(Pancake Sorting Problem)算法与源代码
- C#,排列组合的堆生成法(Heap’s Algorithm for generating permutations)算法与源代码
- C#,老鼠迷宫问题的回溯法求解(Rat in a Maze)算法与源代码
- 6693eeb8d683458a07938615fba9e68f.apk
- C#,数值计算,解微分方程的龙格-库塔二阶方法与源代码
- C#,数值计算,用割线法(Secant Method)求方程根的算法与源代码
- C#,子集和问题(Subset Sum Problem)的算法与源代码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈