Java数据库连接池类源码

环境:MyEclipse

 

数据库:mysql

 

首先新建Java项目,然后新建两个类,一个数据库连接池类,一个测试类。

 

分别命名ConnectionPool,ConnectionPoolTest。

 

然后新建一个配置文件,命名:dbpool.properties。

 

写入代码:

ConnectionPool.java

 

ContractedBlock.gif ExpandedBlockStart.gif Code
package webbook.util;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Vector;

ExpandedBlockStart.gifContractedBlock.gif
public class ConnectionPool {

    
private Vector<Connection> pool;

    
private String url;

    
private String username;

    
private String password;

    
private String driverClassName;

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
     * 连接池的大小,也就是连接池中有多少个数据库连接。
     
*/

    
private int poolSize = 1;

    
private static ConnectionPool instance = null;

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
     * 私有的构造方法,禁止外部创建本类的对象,要想获得本类的对象,通过<code>getIstance</code>方法。
     * 使用了设计模式中的单子模式。
     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
private ConnectionPool() {
        init();
    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
     * 连接池初始化方法,读取属性文件的内容 建立连接池中的初始连接
     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
private void init() {
        pool 
= new Vector<Connection>(poolSize);
        readConfig();
        addConnection();
    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
     * 返回连接到连接池中
     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public synchronized void release(Connection conn) {
        pool.add(conn);

    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
     * 关闭连接池中的所有数据库连接
     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public synchronized void closePool() {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for (int i = 0; i < pool.size(); i++{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try {
                ((Connection) pool.get(i)).close();
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 catch (SQLException e) {
                e.printStackTrace();
            }

            pool.remove(i);
        }

    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
     * 返回当前连接池的一个对象
     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static ConnectionPool getInstance() {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (instance == null{
            instance 
= new ConnectionPool();
        }

        
return instance;
    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
     * 返回连接池中的一个数据库连接
     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public synchronized Connection getConnection() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (pool.size() > 0{
            Connection conn 
= pool.get(0);
            pool.remove(conn);
            
return conn;
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 else {
            
return null;
        }

    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
     * 在连接池中创建初始设置的的数据库连接
     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
private void addConnection() {
        Connection conn 
= null;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for (int i = 0; i < poolSize; i++{

ExpandedSubBlockStart.gifContractedSubBlock.gif            
try {
                Class.forName(driverClassName);
                conn 
= java.sql.DriverManager.getConnection(url, username, password);
                pool.add(conn);

ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 catch (ClassNotFoundException e) {
                e.printStackTrace();
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 catch (SQLException e) {
                e.printStackTrace();
            }


        }

    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
     * 读取设置连接池的属性文件
     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
private void readConfig() {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try {
            String path 
= System.getProperty("user.dir"+ "\\dbpool.properties";
            FileInputStream is 
= new FileInputStream(path);
            Properties props 
= new Properties();
            props.load(is);
            
this.driverClassName = props.getProperty("driverClassName");
            
this.username = props.getProperty("username");
            
this.password = props.getProperty("password");
            
this.url = props.getProperty("url");
            
this.poolSize = Integer
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值