乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      數(shù)據(jù)庫(kù)連接JDBC連接池組件C3P0的使用方法

       博雅書(shū)屋lhs 2014-08-24
      方法一:
      package   C3P0; 
      import   java.sql.Connection; 
      import   java.sql.SQLException; 
      import   java.beans.PropertyVetoException; 
      import   com.mchange.v2.c3p0.ComboPooledDataSource; 
      public   class   DBPool{       
         private   static   DBPool   dbPool;       
         private   ComboPooledDataSource   dataSource;     

         static   {       
                 dbPool=new   DBPool();       
         }       
         
         public   DBPool(){       
                 try   {       
                         dataSource=new   ComboPooledDataSource();       
                         dataSource.setUser( "id ");       
                         dataSource.setPassword( "pw ");       
                         dataSource.setJdbcUrl( "jdbc:mysql://127.0.0.1:3306/test? 

      autoReconnect=true&useUnicode=true&characterEncoding=GB2312 "); 
                         dataSource.setDriverClass( "com.mysql.jdbc.Driver "); 
                         dataSource.setInitialPoolSize(2); 
                         dataSource.setMinPoolSize(1); 
                         dataSource.setMaxPoolSize(10); 
                         dataSource.setMaxStatements(50); 
                         dataSource.setMaxIdleTime(60);       
                 }   catch   (PropertyVetoException   e)   {       
                     throw   new   RuntimeException(e);       
                 }       
         }       

         public   final   static   DBPool   getInstance(){       
                 return   dbPool;       
         }       

         public   final   Connection   getConnection()   {       
                 try   {       
                         return   dataSource.getConnection();       
                 }   catch   (SQLException   e)   {       
                         throw   new   RuntimeException( "無(wú)法從數(shù)據(jù)源獲取連接 ",e);       
                 }       
         }     
         
         public   static   void   main(String[]   args)   throws   SQLException  
      Connection   con   =   null; 
      try  
      con   =   DBPool.getInstance().getConnection(); 
      }   catch   (Exception   e){ 
      }   finally  
      if   (con   !=   null) 
      con.close(); 


      }

      方法二:

      原來(lái)不知道使用c3p0 是如此的簡(jiǎn)單,我一直使用properties 文件去配置c3p0,但總是連接不上數(shù)據(jù)庫(kù),后來(lái)調(diào)試才發(fā)現(xiàn)ComboPooledDataSource 這個(gè)對(duì)象的屬性沒(méi)有被設(shè)置成功,我是先獲取了properties文件的內(nèi)容,封裝在一個(gè) Properties對(duì)象里面,然后直接調(diào)用 ComboPooledDataSource 的 setProperties(Properties  p) 方法來(lái)配置c3p0,程序是沒(méi)有報(bào)錯(cuò),但連不上數(shù)據(jù)庫(kù),調(diào)試發(fā)現(xiàn)屬性都沒(méi)有設(shè)置成功,只是properties這個(gè)屬性被設(shè)置了而已,結(jié)果我對(duì)每個(gè)屬性調(diào)用set方法后就連接上了。。。

      public final class ConnectionManager {
       private static ConnectionManager instance;

       public ComboPooledDataSource ds;
       private static String c3p0Properties = "c3p0.properties";

       private ConnectionManager() throws Exception {
        Properties p = new Properties();
        p.load(this.getClass().getResourceAsStream(c3p0Properties));
        ds = new ComboPooledDataSource();
        ds.setUser(p.getProperty("user"));
        ds.setPassword(p.getProperty("user"));
        ds.setJdbcUrl(p.getProperty("user"));
        ds.setDriverClass(p.getProperty("user"));
        ds.setInitialPoolSize(Integer.parseInt(p.getProperty("initialPoolSize")));
        ds.setMinPoolSize(Integer.parseInt(p.getProperty("minPoolSize")));
        ds.setMaxPoolSize(Integer.parseInt(p.getProperty("maxPoolSize")));
        ds.setMaxStatements(Integer.parseInt(p.getProperty("maxStatements")));
        ds.setMaxIdleTime(Integer.parseInt(p.getProperty("maxIdleTime")));
       }

       public static final ConnectionManager getInstance() {
        if (instance == null) {
         try {
          instance = new ConnectionManager();
         } catch (Exception e) {
          e.printStackTrace();
         }
        }
        return instance;
       }

       public synchronized final Connection getConnection() {
        try {
         return ds.getConnection();
        } catch (SQLException e) {
         e.printStackTrace();
        }
        return null;
       }

       protected void finalize() throws Throwable {
        DataSources.destroy(ds); // 關(guān)閉datasource
        super.finalize();
       }
      }

      如此就可以獲取connection來(lái)做jdbc操作了:
      Connection conn=ConnectionManager.getInstance().getConnection();
      記得使用完后調(diào)用close方法:
      conn.close();
      c3p0 的某些參數(shù)的配置以及意義見(jiàn)另外一篇文章http://kangzye.blog.163.com/blog/static/368192232010442162576/


        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約

        類(lèi)似文章 更多