码迷,mamicode.com
首页 > 数据库 > 详细

java 利用c3p0管理数据库连接池

时间:2017-04-03 00:28:50      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:执行   throws   exce   public   etc   form   exception   for   prepare   

数据库连接池类,用于获取数据库连接。利用单例模式保证所有的连接都只通过一个连接池管理。

package com.mousewheel.dbcon;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DBPool {
    
    private static DBPool instance;

    private ComboPooledDataSource dataSource;
    
    static{
        instance = new DBPool();
    }

    private DBPool() {
        try {
            dataSource = new ComboPooledDataSource();
            Properties prop = new Properties();
            InputStream in = DBPool.class.getClassLoader().getResourceAsStream("db.properties");
            prop.load(in);

            dataSource.setDriverClass(prop.getProperty("jdbcdriver"));
            dataSource.setJdbcUrl(prop.getProperty("url"));
            dataSource.setUser(prop.getProperty("username"));
            dataSource.setPassword(prop.getProperty("password"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static DBPool getInstance(){
        return instance;
    }
    
    public Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }
}

DBUtil类,包装DBPool,简化使用

package com.mousewheel.dbcon;

import java.sql.Connection;
import java.sql.SQLException;

public class DbUtil {
    public static Connection getConnection() throws SQLException {
        DBPool pool = DBPool.getInstance();
        return pool.getConnection();
    }
}

 

测试代码,和不使用连接池的情况做对比

package com.mousewheel.dbcon;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import java.util.Properties;
import java.io.InputStream;

import com.mousewheel.dbcon.DbUtil;


 class App {
    public static void main(String[] args) {
        // use c3p0 connection pool
        try {
            for (int i = 0; i < 100; i++) {
                long beginTime = System.currentTimeMillis();
                Connection con = DbUtil.getConnection();
                // 执行查询语句
                String sql = "select * from student";
                PreparedStatement ps = con.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    // System.out.println(rs.getString(1));
                }
                con.close();
                long endTime = System.currentTimeMillis();
                System.out.println(String.format("第%s次,%s", i, (endTime - beginTime)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // jdbc without using conection pool
        try {
            for (int i = 0; i < 100; i++) {
                long beginTime = System.currentTimeMillis();
                Properties prop = new Properties();
                InputStream in = App.class.getClassLoader().getResourceAsStream("db.properties");
                prop.load(in);
                Class.forName(prop.getProperty("jdbcdriver"));

                Connection con = DriverManager.getConnection(prop.getProperty("url"), prop.getProperty("username"),
                        prop.getProperty("password"));

                String sql = "select * from student";
                PreparedStatement ps = con.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    // System.out.println(rs.getString(1));
                }
                con.close();
                long endTime = System.currentTimeMillis();
                System.out.println(String.format("第%s次,%s", i, (endTime - beginTime)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

java 利用c3p0管理数据库连接池

标签:执行   throws   exce   public   etc   form   exception   for   prepare   

原文地址:http://www.cnblogs.com/mousewheel/p/6660238.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!