2012年8月3日 星期五

Java - 如何透過jco 連接SAP 程式

這一篇主要是一個共用的類別來連結,下一篇會提供可以動態將SAP 吐出來的表格原封不動的塞入自己Database的表格,當然,你這個自己的表格內容要和SAP 的一樣,可以少,但是不可以多。詳細內容是下一篇會介紹。


package com.deu.report.util;

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.ext.DestinationDataProvider;

public class ConnectSAPServer {
     //SAP服務器IP地址 
   // static  String DEI_SAP_CONN_SERVER =   ; 
    //static  String T11 =  "172.test.test.test" ; 
     
    static  String DE_SAP_CONN =  "DE_SAP_CONN" ; 
    static  String DE_TEST_SAP_CONN =  "DE_TEST_SAP_CONN" ; 
     
    static { 
        Properties connectProperties =  new  Properties(); 
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "172.test.test.test");  
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,   "00" );         //系統編號 
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT,  "025" );        //SAP集團 
        connectProperties.setProperty(DestinationDataProvider.JCO_USER,    "test" );   //SAP用戶名 
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD,  "test" );      //密碼 
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG,    "EN" );         //登錄語言 
        connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY,  "3" );   //最大連接數 
        connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10" );      //最大連接線程 
     
        createDataFile(DEI_SAP_CONN,  "jcoDestination" , connectProperties); 
        
        connectProperties =  new  Properties(); 
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "172.16.test.test");  
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,   "00" );         //系統編號 
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT,  "025" );        //SAP集團 
        connectProperties.setProperty(DestinationDataProvider.JCO_USER,    "test" );   //SAP用戶名 
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD,  "test" );      //密碼 
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG,    "EN" );         //登錄語言 
        connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY,  "3" );   //最大連接數 
        connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10" );      //最大連接線程 
     
        createDataFile(DE_TEST_SAP_CONN,  "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 DEI only !" ); 
                fos.close(); 
            } catch  (Exception e){ 
                throw  new  RuntimeException( "Unable to create the destination file "  + cfg.getName(), e); 
            } 
        } 
    } 
 
    public  static  JCoDestination ConnectDE(){ 
        JCoDestination destination = null ;    
        try  { 
            destination = JCoDestinationManager.getDestination( DE_SAP_CONN ); 
        }  catch  (JCoException e) { 
            e.printStackTrace();
        } 
         
        return  destination; 
    } 
    public  static  JCoDestination ConnectDETest(){ 
        JCoDestination destination = null ;    
        try  { 
            destination = JCoDestinationManager.getDestination( DE_TEST_SAP_CONN ); 
        }  catch  (JCoException e) { 
            e.printStackTrace();
        } 
         
        return  destination; 
    } 
}

沒有留言:

張貼留言