Java Connector(JCO)环境的搭建:Step by step to download and configure JCO in your laptop

我的ABAP系统有个函数名叫ZDIS_GET_UPSELL_MATERIALS,输入一个customer ID和product ID,会输出为这对客户和product组合维护的一组Upsell product ID和描述信息。

测试如下:



下面是使用Java消费该函数的代码:

package jco;

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.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoRepository;
import com.sap.conn.jco.JCoTable;
import com.sap.conn.jco.ext.DestinationDataProvider; /**
* basic examples for Java to ABAP communication
* See help: https://help.sap.com/saphelp_nwpi711/helpdata/en/48/70792c872c1b5ae10000000a42189c/frameset.htm
*/
public class StepByStepClient
{
static String DESTINATION_NAME = "ABAP_AS_WITHOUT_POOL";
static public final String ABAP_DURATION = "abapLayerDuration";
static public final String UPSELL_PRODUCT = "upsellProducts";
static public final String PRODUCT_ID = "productID";
static public final String PRODUCT_TEXT = "productText"; static private Properties prepareProperty(){
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "ldcixcd.wdf.sap.corp");
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "00");
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "111");
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "WANGJER");
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "Saptest1");
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "en");
createDestinationDataFile(DESTINATION_NAME, connectProperties);
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3");
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10");
createDestinationDataFile(DESTINATION_NAME, connectProperties);
return connectProperties;
} static public void main(String[] arg) {
createDestinationDataFile(DESTINATION_NAME, prepareProperty()); JCoDestination destination = null;
try {
destination = JCoDestinationManager.getDestination(DESTINATION_NAME);
JCoRepository repo = destination.getRepository();
JCoFunction stfcConnection = repo.getFunction("ZDIS_GET_UPSELL_MATERIALS"); JCoParameterList imports = stfcConnection.getImportParameterList(); String customerID = "1000040";
String materialID = "11"; imports.setValue("IV_CUSTOMER_ID", customerID);
imports.setValue("IV_MATERIAL_ID", materialID); stfcConnection.execute(destination); JCoParameterList exports = stfcConnection.getExportParameterList(); // int result = exports.getInt("EV_RESULT");
int abapDuration = exports.getInt("EV_DURATION"); StringBuilder sb = new StringBuilder();
sb.append("{ \"" + ABAP_DURATION + "\": " + abapDuration + ","); sb.append("\"" + UPSELL_PRODUCT + "\":["); JCoTable codes = exports.getTable("ET_MATERIALS"); int row = codes.getNumRows();
System.out.println("Total rows: " + row); System.out.println("ABAP duration: " + abapDuration); for( int i = 0; i < row; i++){
codes.setRow(i);
System.out.println(codes.getString("MATERIAL_ID") + '\t' + codes.getString("MATERIAL_TEXT"));
sb.append("{\"" + PRODUCT_ID + "\":" + codes.getString("MATERIAL_ID") + ","
+ "\"" + PRODUCT_TEXT + "\":\"" + codes.getString("MATERIAL_TEXT") + "\"");
if( i < row - 1){
sb.append("},");
}
else{
sb.append("}");
}
}
sb.append("]}"); System.out.println("Final json: " + sb.toString()); } catch (JCoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } static private void createDestinationDataFile(String destinationName, Properties connectProperties)
{
File destCfg = new File(destinationName+".jcoDestination");
try
{
FileOutputStream fos = new FileOutputStream(destCfg, false);
connectProperties.store(fos, "for tests only !");
fos.close();
}
catch (Exception e)
{
throw new RuntimeException("Unable to create the destination files", e);
}
}
}

为简单起见没有使用Google的gson库进行Json的序列化。

执行结果:

要获取更多Jerry的原创技术文章,请关注公众号"汪子熙"或者扫描下面二维码:

使用Java connector消费ABAP系统的函数的更多相关文章

  1. 使用Excel调用ABAP系统的函数

    效果:在excel里创建一个按钮,开发一些VB script,可以连接指定的ABAP系统并执行系统里的ABAP function module. 在这里例子里执行ABAP系统的函数TH_USER_LI ...

  2. 在SAP云平台的CloudFoundry环境下消费ABAP On-Premise OData服务

    我的前一篇文章 使用Java+SAP云平台+SAP Cloud Connector调用ABAP On-Premise系统里的函数介绍了在SAP云平台的Neo环境下如何通过SAP Cloud Conne ...

  3. 使用Java+SAP云平台+SAP Cloud Connector调用ABAP On-Premise系统里的函数

    最近Jerry接到一个原型开发的任务,需要在微信里调用ABAP On Premise系统(SAP CRM On-Premise)里的某些函数.具体场景和我之前的公众号文章 Cloud for Cust ...

  4. JAVA中获取当前系统时间及格式转换

    JAVA中获取当前系统时间   一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; publi ...

  5. J2EE: JCA (Java Connector Architecture) [转]

    JCA (J2EE 连接器架构,Java Connector Architecture)是对J2EE标准集的重要补充.因为它注重的是将Java程序连接到非Java程序和软件包中间件的开发.连接器特指基 ...

  6. kafka_2.11-0.8.2.1+java 生产消费程序demo示例

      Kafka学习8_kafka java 生产消费程序demo示例 kafka是吞吐量巨大的一个消息系统,它是用scala写的,和普通的消息的生产消费还有所不同,写了个demo程序供大家参考.kaf ...

  7. OSGI 面向Java的动态模型系统

    OSGI (面向Java的动态模型系统) OSGi(Open Service Gateway Initiative)技术是Java动态化模块化系统的一系列规范.OSGi一方面指维护OSGi规范的OSG ...

  8. Java中的Random()函数-----转载

    Java中的Random()函数 (2013-01-24 21:01:04) 转载▼ 标签: java random 随机函数 杂谈 分类: Java 今天在做Java练习的时候注意到了Java里面的 ...

  9. 第39篇-Java通过JNI调用C/C++函数

    在某些情况下,Java语言需要通过调用C/C++函数来实现某些功能,因为Java有时候对这些功能显的无能为力,如想使用X86_64 的 SIMD 指令提升一下业务方法中关键代码的性能,又或者想要获取某 ...

随机推荐

  1. javascript数组对象

    constructor属性 返回数组对象原型 var arr = [1,2,3,4,5]; arr.constructor //输出 function Array() { [native code] ...

  2. HDU - 5451 Best Solver(循环节+矩阵快速幂)

    Best Solver The so-called best problem solver can easily solve this problem, with his/her childhood ...

  3. HDU - 6082 度度熊与邪恶大魔王(背包变式)

    度度熊与邪恶大魔王 度度熊为了拯救可爱的公主,于是与邪恶大魔王战斗起来. 邪恶大魔王的麾下有n个怪兽,每个怪兽有a[i]的生命值,以及b[i]的防御力. 度度熊一共拥有m种攻击方式,第i种攻击方式,需 ...

  4. 当我们聊kubernetes operator时,我们在聊些什么

    不聊什么 在开始聊operator前,先说说这篇文章里我们不聊什么.我们这里不聊operator的具体实现,不聊operator的由来历史,不聊operator的hello world.如果想了解这些 ...

  5. Scipy的应用

    首先总体概括一下Scipy的用处 >>> #Scipy依赖于numpy>>> #Scipy提供了真正的矩阵>>> #Scipy包含的功能:最优化, ...

  6. sql server 2012 profiler打开2016的profiler

    软件环境:1.本机是sql server 2012,远程服务器是sql server 20162.本机是Windows 7,安装不了sql server 2016 问题场景1:本机sql server ...

  7. jzoj6002. 【PKUWC2019模拟2019.1.15】Permutation (组合数)

    题面 题解 设\(lim=(n-1)/2\)(这里是下取整),那么\(x\)位置的值最大不能超过\(lim\),而\(y\)处的值不能小于\(y\),于是有\[Ans=\sum_{i=1}^{lim} ...

  8. uoj#38. 【清华集训2014】奇数国(线段树+数论)

    传送门 不难看出就是要先求区间积,再求这个区间积的\(\varphi\) 因为\(\varphi(x)=x\times\frac{p_1-1}{p_1}\times\frac{p_2-1}{p_2}\ ...

  9. JavaScript进阶 - 第5章 小程序,大作用(函数)

    5-1什么是函数 函数的作用,可以写一次代码,然后反复地重用这个代码. 如:我们要完成多组数和的功能. var sum;   sum = 3+2; alert(sum);   sum=7+8 ; al ...

  10. idea 添加yuicompressor压缩js/css

    打开idea 点击file->Settings 出现如下界面 argumets项填写 : -jar F:\yui\yuicompressor-2.4.8.jar $FilePath$ -o $F ...