zoukankan      html  css  js  c++  java
  • JCO demo

    1.Connect Directly

    import java.io.File;
    import java.io.FileOutputStream;
    import java.util.Properties;
    
    import com.sap.conn.jco.JCoException;
    import com.sap.conn.jco.JCoDestination;
    import com.sap.conn.jco.JCoDestinationManager;
    import com.sap.conn.jco.ext.DestinationDataProvider;
    
    /**
     * ConnectNoPool
     * need creating property configuration files without pool
     */
    public class ConnectNoPool {
        static String ABAP_AS = "ABAP_AS_WITHOUT_POOL";
        static{
             //In SAP,the GUI connection properties have two types,
            /*
            *1.Custom Application Server
             *-Application Server is "JCO_ASHOST"
             *-Instance Number is "JCO_SYSNR"
             *-Client is "JCO_CLIENT"
             *-And the User Name & Password
             */
            Properties connectProperties = new Properties();
            connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST,
            "10.1.3.5");
            connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, 
            "00");
            connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, 
            "400");
            connectProperties.setProperty(DestinationDataProvider.JCO_USER, 
            "account");
            connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD,
            "password");
             /*
              *2.Group/Server Selection
              *-System ID is "JCO_R3NAME"
              *-Message Server is "JCO_MSHOST" and "JCO_MSSERV",
              * EG,"abc,aaa,bbb:ccc","JCO_MSHOST" = "abc,aaa,bbb","JCO_MSSERV" = "ccc"
              *-Group/Server is "JCO_GROUP"
              *-And the User Name & Password
              */
            connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "en");
            createDataFile(ABAP_AS,"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{
            JCoDestination destination = JCoDestinationManager
            .getDestination(ABAP_AS);
            System.out.println("Attributes:");
            System.out.println(destination.getAttributes());
        }
        public static void main(String[] args) throws JCoException{
            connectWithoutPool();
        }
    }
    

    2.Connect With Pool

    import java.io.File;
    import java.io.FileOutputStream;
    import java.util.Properties;
    
    
    import com.sap.conn.jco.JCoException;
    import com.sap.conn.jco.JCoDestination;
    import com.sap.conn.jco.JCoDestinationManager;
    import com.sap.conn.jco.ext.DestinationDataProvider;
    
    /**
     * ConnectNoPool
     * need creating property configuration files with pool
     */
    public class ConnectWithPool {
        static String ABAP_AS_POOL = "ABAP_AS_WITH_POOL";
        static{
            Properties connectProperties = new Properties();
           /*
            *Set connettion
            */
            /*
            The different between "WITH POOL" and "NO POOL" are the below things:
                JCO_PEAK_LIMIT:maximum connections can be created simultaneously,
                "0" means no restirct 
                the default "JCO_PEAK_LIMIT" is "JCO_POOL_CAPACITY",
                it will be set to "JCO_POOL_CAPACITY" automatically if less than it
                whtn "JCO_POOL_CAPACITY" is null,"JCO_PEAK_LIMIT" will be set to "0"
            */
          connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT,
          "10");
          /* JCO_POOL_CAPACITY   
          idle connections,if"0",will have no pool effect,default value is "1"
          */
          connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, 
          "3");
    
        createDataFile(ABAP_AS_POOL,"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 connectWithPool() throws JCoException{
            JCoDestination destination = JCoDestinationManager
            .getDestination(ABAP_AS_POOL);
            System.out.println("Attributes:");
            System.out.println(destination.getAttributes());
        }
        public static void main(String[] args) throws JCoException{
            connectWithPool();
        }
    }
    

    3.No Config Files Connection(with or without connection pool)

    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.ext.DestinationDataEventListener;
    import com.sap.conn.jco.ext.DestinationDataProvider;
    import com.sap.conn.jco.ext.Environment;
    /**
     * CustomSAPDestinationDataProvider
     * No need for creating property configuration files
     */
    public class CustomSAPDestinationDataProvider {
    
        static class MyDestinationDataProvider implements DestinationDataProvider{
            private DestinationDataEventListener eL;
            private HashMap<String,Properties> destinations;
            private static MyDestinationDataProvider provider = 
            new MyDestinationDataProvider();
            private MyDestinationDataProvider () {
                if (provider == null) {
                    destinations = new HashMap<String,Properties>();
                }
            }
    
            public static MyDestinationDataProvider getInstance(){
                return provider;
            }
            
            public Properties getDestinationProperties(String destinationName) {
    
                if (destinations.containsKey(destinationName)) {
             
                      return destinations.get(destinationName);
             
                    } else {
             
                   throw new RuntimeException("Destination " + destinationName
             
                      + " is not available");
             
                    }
             
            }
    
            public void setDestinationDataEventListener(
                DestinationDataEventListener eventListener) {
    
                this.eL = eventListener;
             }
    
            public boolean supportsEvents() {
                return true;
            }
            void addDestination(String destinationName, Properties properties) {
    
                synchronized (destinations) {
             
                   destinations.put(destinationName, properties);
             
                    }
             
            } 
        }
    
        public static void main(String[] args) throws Exception{
            MyDestinationDataProvider myProvider 
            = MyDestinationDataProvider.getInstance();
    
            Environment.registerDestinationDataProvider(myProvider);
    
            //TEST 01:NO POOL
            String destinationName_1 = "ABAP_AS";
            System.out.println("The destination - "+destinationName_1);
            Properties connectProperties_1 = new Properties();
             /*
            *Set connettion
            */
            myProvider.addDestination(destinationName_1, connectProperties_1);
            JCoDestination DES_ABAP_AS = JCoDestinationManager
            .getDestination(destinationName_1);
            try {
                DES_ABAP_AS.ping();
                System.out.println("Destination - " + destinationName_1 + " is ok");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("Destination - " + destinationName_1 + " is invalid");
    
            }
    
            //TEST 02:WITH POOL
    
            String destinationName_2 = "ABAP_AS_POOL";
            System.out.println("The destination - "+destinationName_2);
            Properties connectProperties_2 = new Properties();
             /*
            *Set connettion
            */
            connectProperties_2.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT,
            "10");
            connectProperties_2.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, 
            "3");
            myProvider.addDestination(destinationName_2, connectProperties_2);
            JCoDestination DES_ABAP_AS2 = JCoDestinationManager
            .getDestination(destinationName_2);
            try {
    
                DES_ABAP_AS2.ping();
                System.out.println("Destination - " + destinationName_2 + " is ok");
         
            } catch (Exception ex) {
                ex.printStackTrace();
                System.out.println("Destination - " + destinationName_2
                   + " is invalid");
            }
        }
        
    }
    

    4.Access Structure By RFC In SAP With The Connection Above

    // import CustomSAPDestinationDataProvider.*;
    import com.sap.conn.jco.JCoException;
    import java.util.Properties;
    import com.sap.conn.jco.ext.Environment;
    import com.sap.conn.jco.ext.DestinationDataProvider;
    import com.sap.conn.jco.JCoDestination;
    import com.sap.conn.jco.JCoDestinationManager;
    import com.sap.conn.jco.JCoFunction;
    import com.sap.conn.jco.AbapException;
    import com.sap.conn.jco.JCoStructure;
    /**
     * AccessStructure
     */
    public class AccessStructure {
    
        public static void accessSAPStructure() throws JCoException {
            //Inner Class
            CustomSAPDestinationDataProvider.MyDestinationDataProvider myProvider 
            = CustomSAPDestinationDataProvider.MyDestinationDataProvider.getInstance();
            Environment.registerDestinationDataProvider(myProvider);
            
            String destinationName_1 = "ABAP_AS";
            Properties connectProperties_1 = new Properties();
             /*
            *Set connettion
            */
            myProvider.addDestination(destinationName_1, connectProperties_1);
            JCoDestination destination = JCoDestinationManager
            .getDestination(destinationName_1);
            JCoFunction function = destination.getRepository().getFunction(
            "RFC_SYSTEM_INFO");
            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("RFCSI_EXPORT");
            System.out.println("System info for "
            + destination.getAttributes().getSystemID() + ":
    ");
            for (int i = 0; i < exportStructure.getMetaData().getFieldCount(); i++) {
                System.out.println(exportStructure.getMetaData().getName(i) + ":	"
                + exportStructure.getString(i));
            }
            System.out.println();
            System.out.println("RFCPROTO:	"+exportStructure.getString(0));
            System.out.println("RFCPROTO:	"+exportStructure.getString("RFCPROTO"));
         }
         public static void main(String[] args) throws JCoException {
             accessSAPStructure();
         }
    }
    

    5.Access Table by BAPI In SAP With The Connection Above

    import com.sap.conn.jco.JCoException;
    import java.util.Properties;
    import com.sap.conn.jco.ext.Environment;
    import com.sap.conn.jco.ext.DestinationDataProvider;
    import com.sap.conn.jco.JCoDestination;
    import com.sap.conn.jco.JCoDestinationManager;
    import com.sap.conn.jco.JCoFunction;
    import com.sap.conn.jco.AbapException;
    import com.sap.conn.jco.JCoStructure;
    import com.sap.conn.jco.JCoTable;
    /**
     * AccessStructure
     */
    public class AccessTable {
    
        public static void accessSAPTable() throws JCoException {
            //Inner Class
            CustomSAPDestinationDataProvider.MyDestinationDataProvider myProvider 
            = CustomSAPDestinationDataProvider.MyDestinationDataProvider.getInstance();
            Environment.registerDestinationDataProvider(myProvider);
            
            String destinationName_1 = "ABAP_AS";
            Properties connectProperties_1 = new Properties();
             /*
            *Set connettion
            */
            myProvider.addDestination(destinationName_1, connectProperties_1);
            JCoDestination destination = JCoDestinationManager
            .getDestination(destinationName_1);
            //Company list FM
            JCoFunction function = destination.getRepository().getFunction(
            "BAPI_COMPANYCODE_GETLIST");
            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;
            }
            JCoStructure returnStructure = function.getExportParameterList()
            .getStructure("RETURN");
    
            if (!(returnStructure.getString("TYPE").equals("")
             || returnStructure.getString("TYPE").equals("S"))) {
                throw new RuntimeException(returnStructure.getString("MESSAGE"));
            }
            JCoTable itab = function.getTableParameterList().getTable(
            "COMPANYCODE_LIST");
            System.out.println("Company Codes:");
            for (int i = 0; i < itab.getNumRows(); i++) {//Traverse the table
                itab.setRow(i);
                System.out.println(itab.getString("COMP_CODE") + '	'
                   + itab.getString("COMP_NAME"));
            }
            //move the table cursor to first row
            itab.firstRow();
            //Get the company details FM
            System.out.println("Company Details:");
            for (int i = 0; i < itab.getNumRows(); i++,itab.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",
                itab.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
                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 details = function.getExportParameterList()
                .getStructure("COMPANYCODE_DETAIL");
                System.out.println(details.getString("COMP_CODE") + '	' 
                + details.getString("COUNTRY") + '	'
                + details.getString("CITY"));
            }
         }
         public static void main(String[] args) throws JCoException {
            accessSAPTable();
         }
    }
    
  • 相关阅读:
    倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何把FBD功能块转换成ST语言
    揭秘一个操作灰色关键词牟取暴利的案例
    [原创汉化] 价值990美元的顶级专业数据恢复软件O&O DiskRecovery 11(技术员版)汉化绿色版
    list集合如何对里面的元素进行排序
    jquery datables ajax分页后的点击事件无效是怎么回事
    阿里云自动快照有什么用,如何设置?
    php后台管理员权限相关表结构
    服务器上装了安全狗后远程链接不上怎么解决
    Ehcache配置参数简介
    Spring+EhCache缓存实例
  • 原文地址:https://www.cnblogs.com/aurora-cj/p/11025706.html
Copyright © 2011-2022 走看看