iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >java怎么使用Jco连接SAP
  • 582
分享到

java怎么使用Jco连接SAP

2023-07-05 14:07:34 582人浏览 泡泡鱼
摘要

这篇“java怎么使用Jco连接SAP”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“java怎么使用Jco连接SAP”文章吧

这篇“java怎么使用Jco连接SAP”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“java怎么使用Jco连接SAP”文章吧。

java使用Jco连接SAP

JCO为我们提供了另外一种连接的方法:

DestinationDataProvider,通过它我们就可以将一个连接变量信息存放在内存里。

import java.util.HashMap;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.DataProviderException;import com.sap.conn.jco.ext.DestinationDataEventListener;import com.sap.conn.jco.ext.DestinationDataProvider; public class CustomDestinationDataProvider{        static class MyDestinationDataProvider implements DestinationDataProvider    {        private DestinationDataEventListener eL;        private HashMap<String, Properties> secureDBStorage = new HashMap<String, Properties>();         // 实现接口:获取连接配置属性        public Properties getDestinationProperties(String destinationName)        {            try            {                //read the destination from DB                Properties p = secureDBStorage.get(destinationName);                 if(p!=null)                {                    //check if all is correct, for example                    if(p.isEmpty())                        throw new DataProviderException(DataProviderException.Reason.INVALID_CONFIGURATION, "destination configuration is incorrect", null);                     return p;                }                 return null;            }            catch(RuntimeException re)            {                throw new DataProviderException(DataProviderException.Reason.INTERNAL_ERROR, re);            }        }         public void setDestinationDataEventListener(DestinationDataEventListener eventListener)        {            this.eL = eventListener;        }         public boolean supportsEvents()        {            return true;        }         //implementation that saves the properties in a very secure way 添加连接配置属性        void changeProperties(String destName, Properties properties)        {            synchronized(secureDBStorage)            {                if(properties==null)                {                    if(secureDBStorage.remove(destName)!=null)                        eL.deleted(destName);                }                else                {                    secureDBStorage.put(destName, properties);                    eL.updated(destName); // create or updated                }            }        }    } // end of MyDestinationDataProvider     //business logic    void executeCalls(String destName)    {        JCoDestination dest;        try        {            dest = JCoDestinationManager.getDestination(destName);            dest.ping();            System.out.println("Destination " + destName + " works");        }        catch(JCoException e)        {            e.printStackTrace();            System.out.println("Execution on destination " + destName+ " failed");        }    }         static Properties getDestinationPropertiesFromUI()    {        //adapt parameters in order to configure a valid destination        Properties connectProperties = new Properties();        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "***********");  //IP        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  "01");               //系统编号        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "620");              //客户端编号        connectProperties.setProperty(DestinationDataProvider.JCO_USER,   "user");             //用户名        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "123456");           //密码        connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   "ZH");               //语言        return connectProperties;    }     public static void main(String[] args)    {        //初始化配置信息        MyDestinationDataProvider myProvider = new MyDestinationDataProvider();         //reGISter the provider with the JCo environment;        //catch IllegalStateException if an instance is already registered        try        {            com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(myProvider);        }        catch(IllegalStateException providerAlreadyRegisteredException)        {            //somebody else registered its implementation,            //stop the execution            throw new Error(providerAlreadyRegisteredException);        }         //连接池名,名字随意取        String destName = "ABAP_AS";        CustomDestinationDataProvider test = new CustomDestinationDataProvider();         //set properties for the destination and ...        myProvider.changeProperties(destName, getDestinationPropertiesFromUI());        //... work with it        //连接测试        test.executeCalls(destName);    } }

然后可以用如下的代码来 call rfc。

import java.util.concurrent.CountDownLatch; import com.sap.conn.jco.*; import static com.chunqiu.modules.sap.CustomDestinationDataProvider.getDestinationPropertiesFromUI; public class StepClient{    static String ABAP_AS = "ABAP_AS";    static String ABAP_AS_POOLED = "ABAP_AS";    static String ABAP_MS = "ABAP_AS";         public static void step1Connect() throws JCoException    {        JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS);        System.out.println("Attributes:");        System.out.println(destination.getAttributes());        System.out.println();         destination = JCoDestinationManager.getDestination(ABAP_MS);        System.out.println("Attributes:");        System.out.println(destination.getAttributes());        System.out.println();    }         public static void step2ConnectUsingPool() throws JCoException    {        JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);        destination.ping();        System.out.println("Attributes:");        System.out.println(destination.getAttributes());        System.out.println();    }         public static void step3SimpleCall() throws JCoException    {        //JCoDestination is the logic address of an ABAP system and ...        JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);        // ... it always has a reference to a metadata repository        //从对象仓库中获取 RFM 函数        JCoFunction function = destination.getRepository().getFunctionTemplate("ZFM_FI_TAXPLATFORM_PRICE").getFunction();        System.out.println("function================="+function);         if(function == null)            throw new RuntimeException("ZFM_FI_TAXPLATFORM_PRICE not found in SAP.");         //JCoFunction is container for function values. Each function contains separate        //containers for import, export, changing and table parameters.        //To set or get the parameters use the apiS setValue() and getXXX().        // 设置import 参数        JCoParameterList importParam = function.getExportParameterList();        importParam.setValue("ZFIS_MBLNR", "123456789");         try        {            //execute, i.e. send the function to the ABAP system addressed            //by the specified destination, which then returns the function result.            //All necessary conversions between Java and ABAP data types            //are done automatically.            function.execute(destination);        }        catch(AbapException e)        {            //System.out.println(e.toString());            return;        }         System.out.println("STFC_CONNECTION finished:");        System.out.println(" Echo: " + function.getExportParameterList().getString("ECHOTEXT"));        System.out.println(" Response: " + function.getExportParameterList().getString("RESPTEXT"));        System.out.println();    }         public static void step3WorkWithStructure() throws JCoException    {        JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);        JCoFunction function = destination.getRepository().getFunctionTemplate("ZFM_FI_TAXPLATFORM_PRICE").getFunction();        if(function == null)            throw new RuntimeException("ZFM_FI_TAXPLATFORM_PRICE not found in SAP.");         try        {            function.execute(destination);        }        catch(AbapException e)        {            System.out.println(e.toString());            return;        }         //从返回数据中解析        JCoStructure exportStructure = function.getExportParameterList().getStructure("RFCSI_EXPORT");        System.out.println("System info for " + destination.getAttributes().getSystemID() + ":\n");         /    public static void step4WorkWithTable() throws JCoException    {        JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);        JCoFunction function = destination.getRepository().getFunction("ZFM_FI_TAXPLATFORM_PRICE");        if(function == null)            throw new RuntimeException("ZFM_FI_TAXPLATFORM_PRICE not found in SAP.");         try        {            function.execute(destination);        }        catch(AbapException e)        {            System.out.println(e.toString());            return;        }         JCoStructure returnStructure = function.getExportParameterList().getStructure("RETURN");        if (! (returnStructure.getString("TYPE").equals("")||returnStructure.getString("TYPE").equals("S"))  )        {           throw new RuntimeException(returnStructure.getString("MESSAGE"));        }         JCoTable codes = function.getTableParameterList().getTable("COMPANYCODE_LIST");        for (int i = 0; i < codes.getNumRows(); i++)        {            codes.setRow(i);            System.out.println(codes.getString("COMP_CODE") + '\t' + codes.getString("COMP_NAME"));        }         //move the table cursor to first row        codes.firstRow();        for (int i = 0; i < codes.getNumRows(); i++, codes.nextRow())        {            function = destination.getRepository().getFunction("BAPI_COMPANYCODE_GETDETAIL");            if (function == null)                throw new RuntimeException("BAPI_COMPANYCODE_GETDETAIL not found in SAP.");             function.getImportParameterList().setValue("COMPANYCODEID", codes.getString("COMP_CODE"));             //We do not need the addresses, so set the corresponding parameter to inactive.            //Inactive parameters will be  either not generated or at least converted.            function.getExportParameterList().setActive("COMPANYCODE_ADDRESS",false);             try            {                function.execute(destination);            }            catch (AbapException e)            {                System.out.println(e.toString());                return;            }             returnStructure = function.getExportParameterList().getStructure("RETURN");            if (! (returnStructure.getString("TYPE").equals("") ||                   returnStructure.getString("TYPE").equals("S") ||                   returnStructure.getString("TYPE").equals("W")) )            {                throw new RuntimeException(returnStructure.getString("MESSAGE"));            }             JCoStructure detail = function.getExportParameterList().getStructure("COMPANYCODE_DETAIL");             System.out.println(detail.getString("COMP_CODE") + '\t' +                               detail.getString("COUNTRY") + '\t' +                               detail.getString("CITY"));        }//for    }         public static void step4SimpleStatefulCalls() throws JCoException    {        final JCoFunctionTemplate incrementCounterTemplate, getCounterTemplate;         JCoDestination destination = JCoDestinationManager.getDestination(ABAP_MS);        incrementCounterTemplate = destination.getRepository().getFunctionTemplate("Z_INCREMENT_COUNTER");        getCounterTemplate = destination.getRepository().getFunctionTemplate("Z_GET_COUNTER");        if(incrementCounterTemplate == null || getCounterTemplate == null)            throw new RuntimeException("This example cannot run without Z_INCREMENT_COUNTER and Z_GET_COUNTER functions");         final int threadCount = 5;        final int loops = 5;        final CountDownLatch startSignal = new CountDownLatch(threadCount);        final CountDownLatch doneSignal = new CountDownLatch(threadCount);         Runnable worker = new Runnable()        {            public void run()            {                startSignal.countDown();                try                {                    //wait for other threads                    startSignal.await();                     JCoDestination dest = JCoDestinationManager.getDestination(ABAP_MS);                    JCoContext.begin(dest);                    try                    {                        for(int i=0; i < loops; i++)                        {                            JCoFunction incrementCounter = incrementCounterTemplate.getFunction();                            incrementCounter.execute(dest);                        }                        JCoFunction getCounter = getCounterTemplate.getFunction();                        getCounter.execute(dest);                         int remoteCounter = getCounter.getExportParameterList().getInt("GET_VALUE");                        System.out.println("Thread-" + Thread.currentThread().getId() +                                " finished. Remote counter has " + (loops==remoteCounter?"correct":"wrong") +                                " value [" + remoteCounter + "]");                    }                    finally                    {                        JCoContext.end(dest);                    }                }                catch(Exception e)                {                    System.out.println("Thread-" + Thread.currentThread().getId() + " ends with exception " + e.toString());                }                 doneSignal.countDown();            }        };         for(int i = 0; i < threadCount; i++)        {            new Thread(worker).start();        }         try        {            doneSignal.await();        }        catch(Exception e)        {        }     }          public static void rfcCall() throws JCoException    {        //JCoDestination is the logic address of an ABAP system and ...        JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);        // ... it always has a reference to a metadata repository        //从对象仓库中获取 RFM 函数        JCoFunction function = destination.getRepository().getFunctionTemplate("ZFM_FI_TAXPLATFORM_PRICE").getFunction();         if(function == null)            throw new RuntimeException("ZFM_FI_TAXPLATFORM_PRICE not found in SAP.");         try {             //如果传如参数是内表的形式的话就以如下代码传入sap系统            JCoTable T_ACCDOCUMENT = function.getTableParameterList().getTable("IT_MBLNR");            T_ACCDOCUMENT.appendRow();//增加一行            //给表参数中的字段赋值,此处测试,就随便传两个值进去            T_ACCDOCUMENT.setValue("MBLNR", "5001916414");            //执行调用函数            function.execute(destination);            //获取传入参数返回状态表            JCoTable statusTable = function.getTableParameterList().getTable("IT_MBLNR");//得到sap返回的参数,你就把他当作C语言的结构体理解就可以了             for(int i = 0; i < statusTable.getNumRows(); i++) {                statusTable.setRow(i);                //这里获取sap函数传出内表结构的字段                String rc = statusTable.getString("RCODE");                String mblnr= statusTable.getString("MBLNR");//物料凭证编号 记住这里MBLNR一定是大写的,不然得不到值               if(("02").equals(rc)){                    System.out.println("物料凭证编号:"+mblnr+"  合同编号未维护");                }else if(("03").equals(rc)){                    System.out.println("物料凭证编号:"+mblnr+"  付款条件为空或不一致");                }else if(("04").equals(rc)){                    System.out.println("物料凭证编号:"+mblnr+"  质检未通过");                }else if(("05").equals(rc)){                    System.out.println("物料凭证编号:"+mblnr+"  物料凭证已被冲销");                }else if(("06").equals(rc)){                    System.out.println("物料凭证编号:"+mblnr+"  物料凭证移动类型有误");                }else if(("07").equals(rc)){                    System.out.println("物料凭证编号:"+mblnr+"  物料凭证移动不存在");                }else {                    JCoTable exportTable = function.getTableParameterList().getTable("ET_DATA");//得到sap返回的参数,你就把他当作c语言的结构体理解就可以了                   System.out.println(exportTable);                    //有时候sap那边只是返回一个输出参数,sap比方说你这边输入一个物料号,想得到sap那边的物料描述,这是sap方是不会返回一个内表给你的                    //而是只是返回一个输出参数给你这时你就要用到下面的方法来得到输出参数                    //paramList = function.getExportParameterList();                    //paramList.getString("rfc返回字段字段名称");                    for(int j = 0; j < exportTable.getNumRows(); j++) {                        exportTable.setRow(i);                        //这里获取sap函数传出内表结构的字段                        String BUKRS = exportTable.getString("BUKRS");//记住这里BUKRS一定是大写的,不然得不到值                        System.out.println("公司代码<<<<<<<<<<<<<<<"+BUKRS);                         String LIFNR = exportTable.getString("LIFNR");                        System.out.println("供应商编号<<<<<<<<<<<<<<<"+LIFNR);                         String NAME1 = exportTable.getString("NAME1");                        System.out.println("供应商全称<<<<<<<<<<<<<<<"+NAME1);                         String SORTL = exportTable.getString("SORTL");                        System.out.println("供应商简称<<<<<<<<<<<<<<<"+SORTL);                         //得到了sap数据,然后下面就是你java擅长的部分了,想封装成什么类型的都由你                    }                }            }           } catch (Exception e1) {            // TODO Auto-generated catch block            e1.printStackTrace();        } finally {            destination = null;        }    }      public static void main(String[] args) throws JCoException    {        //建立连接        CustomDestinationDataProvider.MyDestinationDataProvider myProvider = new CustomDestinationDataProvider.MyDestinationDataProvider();         //register the provider with the JCo environment;        //catch IllegalStateException if an instance is already registered        try        {            com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(myProvider);        }        catch(IllegalStateException providerAlreadyRegisteredException)        {            //somebody else registered its implementation,            //stop the execution            throw new Error(providerAlreadyRegisteredException);        }         String destName = "ABAP_AS";        CustomDestinationDataProvider test = new CustomDestinationDataProvider();         //set properties for the destination and ...        myProvider.changeProperties(destName, getDestinationPropertiesFromUI());        //... work with it        test.executeCalls(destName);         //step1Connect();        //step2ConnectUsingPool();        //step3SimpleCall();        //step3WorkWithStructure();        //step4WorkWithTable();        //step4SimpleStatefulCalls();        //测试访问        rfcCall();    }}

测试结果

java怎么使用Jco连接SAP

以上就是关于“java怎么使用Jco连接SAP”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注编程网精选频道。

--结束END--

本文标题: java怎么使用Jco连接SAP

本文链接: https://www.lsjlt.com/news/352526.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • java怎么使用Jco连接SAP
    这篇“java怎么使用Jco连接SAP”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“java怎么使用Jco连接SAP”文章吧...
    99+
    2023-07-05
  • java使用Jco连接SAP过程
    目录java使用Jco连接SAP测试结果总结java使用Jco连接SAP JCO为我们提供了另外一种连接的方法: DestinationDataProvider,通过它我们就可以将一...
    99+
    2023-03-22
    java使用Jco连接SAP Jco连接SAP java Jco SAP
  • VB.NET中怎么连接SAP
    VB.NET中怎么连接SAP,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。以下见代码示例:Imports System  Imports&n...
    99+
    2023-06-17
  • 使用Java怎么连接MySQL8.0 JDBC
    这篇文章给大家介绍使用Java怎么连接MySQL8.0 JDBC,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。一.导入jar包              2.导入    在项目文件夹下新建一个名为lib的文件夹    ...
    99+
    2023-06-14
  • 使用Java怎么连接Mysql 8.0.18
    这篇文章给大家介绍使用Java怎么连接Mysql 8.0.18,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。1.官网下载mysql-server.安装教程参考:mysql 8.0.18...
    99+
    2024-04-02
  • 怎么把SAP Kyma和SAP Cloud for Customer连接起来
    这篇文章主要讲解了“怎么把SAP Kyma和SAP Cloud for Customer连接起来”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么把SAP Kyma和SAP Cloud fo...
    99+
    2023-06-04
  • SAP ECC系统连接SAP PI系统怎么配置
    这篇文章主要介绍了SAP ECC系统连接SAP PI系统怎么配置的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SAP ECC系统连接SAP PI系统怎么配置文章都会有所收获,下面我们一起来看看吧。ECC连接PI...
    99+
    2023-06-05
  • 怎么配置SAP Analytics Cloud到SAP Cloud for Customer的连接
    这篇文章给大家介绍怎么配置SAP Analytics Cloud到SAP Cloud for Customer的连接,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。SAP Analytics Cloud同其他SAP产品或...
    99+
    2023-06-03
  • 怎么使用Eclipse连接SAP云平台上的HANA数据库
    本篇文章为大家展示了怎么使用Eclipse连接SAP云平台上的HANA数据库,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。 SAP云平台(Cloud Platfo...
    99+
    2024-04-02
  • 怎么在IDEA中使用java连接MySQL
    怎么在IDEA中使用java连接MySQL?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。工具:IntelliJ IDEAMySQL8.0.18MySQL连接驱动:mysql...
    99+
    2023-06-14
  • SAP接口编程之JCoDestination怎么使用
    JCoDestination是SAP Java Connector(JCo)库中的一个类,用于表示到SAP系统的连接。您可以使用JC...
    99+
    2023-10-26
    SAP
  • 怎么用ABAP连接SAP上的ABAP编程环境
    这篇文章主要介绍“怎么用ABAP连接SAP上的ABAP编程环境”,在日常操作中,相信很多人在怎么用ABAP连接SAP上的ABAP编程环境问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用ABAP连接SAP上...
    99+
    2023-06-04
  • Java数据库连接池Tomcat怎么使用
    这篇文章主要介绍“Java数据库连接池Tomcat怎么使用”,在日常操作中,相信很多人在Java数据库连接池Tomcat怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java数据库连接池Tomcat怎...
    99+
    2023-06-25
  • java中怎么使用swing连接数据库
    在Java中使用Swing连接数据库,需要借助JDBC(Java Database Connectivity)技术。以下是一个简单的...
    99+
    2023-08-25
    java swing 数据库
  • 怎么在Java中使用JDBC连接数据库
    本篇文章给大家分享的是有关怎么在Java中使用JDBC连接数据库,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。一、使用JDBC连接数据库1.使用JDBC-ODBC桥驱动程序连接...
    99+
    2023-06-06
  • PostgreSQL怎么连接JAVA接口
    本篇文章为大家展示了PostgreSQL怎么连接JAVA接口,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。安装在我们开始之前,我们需要在Java程序中使用PostgreSQL,确保PostgreSQ...
    99+
    2023-06-17
  • java怎么连接mysql
    java连接mysql的方法:1.下载mysql的jar驱动文件;2.配置好jdbc,新建类,写入连接代码;3.运行代码,当连接成功后,控制台会打印成功连接的信息。java连接mysql的方法:下载mysql的jar驱动文件,下载地址htt...
    99+
    2024-04-02
  • mysql怎么使用连接池
    mysql使用连接池的示例:手动配置连接池。    public void demo1(){       &n...
    99+
    2024-04-02
  • linux连接redis怎么使用
    要在Linux系统上连接Redis,您可以使用Redis的命令行工具redis-cli。以下是如何使用redis-cli连接到Red...
    99+
    2024-04-09
    linux redis
  • 怎么使用Node连接mongodb
    本篇内容主要讲解“怎么使用Node连接mongodb”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用Node连接mongodb”吧!Mongoose 是一个 Node.js 包,提供了一个...
    99+
    2023-07-05
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作