带连接池的DBManager

package com.cc.dbConnection;

import java.beans.PropertyVetoException;
import java.io.IOException;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DBManager {
    
        public static ComboPooledDataSource dataSource;  
        public static java.util.Properties config = new java.util.Properties();
        
        static{  
            try {  
                //装载配置文件  
                config.load(ComboPooledDataSource.class.getClassLoader()  
                        .getResourceAsStream("db.properties"));  
                //声明C3P0数据源对象  
                dataSource = new ComboPooledDataSource();  
                //设置数据库连接驱动  
                dataSource.setDriverClass(  
                        config.getProperty("driver"));  
                //设置数据连接URL  
                dataSource.setJdbcUrl(  
                        config.getProperty("url"));  
                //设置数据库连接用户账号  
                dataSource.setUser(  
                        config.getProperty("user"));  
                //设置数据库连接用户账号的密码  
                dataSource.setPassword(  
                        config.getProperty("password"));  
      
                //设置数据库连接池中的初始化连接对象数量  
                dataSource.setInitialPoolSize(Integer.valueOf(config.getProperty("initialPoolSize")));            
                //设置数据库连接池中的最小连接对象数量  
                dataSource.setMinPoolSize(Integer.valueOf(config.getProperty("minPoolSize")));  
                //设置数据库连接池中的最大连接对象数量  
                dataSource.setMaxPoolSize(Integer.valueOf(config.getProperty("maxPllosize")));  
                //当连接不够,每次新增连接数量  
                dataSource.setAcquireIncrement(10);  
                  
            } catch (IOException e) {  
                e.printStackTrace();  
            } catch(PropertyVetoException e){  
                e.printStackTrace();  
            }  
              
        }  
        //构造器
        public DBManager() {
            // TODO Auto-generated constructor stub
        }
        

}

其中properties文件在src根目录下。

得到连接:

Connection connection = DBManager.dataSource.getConnection();

 

 

package com.cc.dbConnection;

import java.beans.PropertyVetoException;
import java.io.IOException;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DBManager {
    
        public static ComboPooledDataSource dataSource;  
        public static java.util.Properties config = new java.util.Properties();
        
        static{  
            try {  
                //装载配置文件  
                config.load(ComboPooledDataSource.class.getClassLoader()  
                        .getResourceAsStream("db.properties"));  
                //声明C3P0数据源对象  
                dataSource = new ComboPooledDataSource();  
                //设置数据库连接驱动  
                dataSource.setDriverClass(  
                        config.getProperty("driver"));  
                //设置数据连接URL  
                dataSource.setJdbcUrl(  
                        config.getProperty("url"));  
                //设置数据库连接用户账号  
                dataSource.setUser(  
                        config.getProperty("user"));  
                //设置数据库连接用户账号的密码  
                dataSource.setPassword(  
                        config.getProperty("password"));  
      
                //设置数据库连接池中的初始化连接对象数量  
                dataSource.setInitialPoolSize(Integer.valueOf(config.getProperty("initialPoolSize")));            
                //设置数据库连接池中的最小连接对象数量  
                dataSource.setMinPoolSize(Integer.valueOf(config.getProperty("minPoolSize")));  
                //设置数据库连接池中的最大连接对象数量  
                dataSource.setMaxPoolSize(Integer.valueOf(config.getProperty("maxPllosize")));  
                //当连接不够,每次新增连接数量  
                dataSource.setAcquireIncrement(10);  
                  
            } catch (IOException e) {  
                e.printStackTrace();  
            } catch(PropertyVetoException e){  
                e.printStackTrace();  
            }  
              
        }  
        //构造器
        public DBManager() {
            // TODO Auto-generated constructor stub
        }
        

}

posted @ 2017-08-22 21:44  halo-漾  阅读(360)  评论(0编辑  收藏  举报