DBCP 数据库连接池配置

1.准备相关jar包和配置文件(可选)

commons-dbcp-1.2.2.jar   #dbcp 数据库连接池必需的jar包

commons-pool-1.5.2.jar   #dbcp 数据库连接池必需的jar包

config.properties   #dbcp 数据库连接池可选的配置文件

mysql-connector-java-5.0.8-bin.jar  #Mysql 数据库驱动

 

有两种配置方法:

第一种:

配置文件:

#数据库驱动

driverClassName=com.mysql.jdbc.Driver

#连接参数
url=jdbc:mysql://localhost:3306/test

#用户
username=root

#密码
password=123456

#初始化连接池数量
initialSize=10

#最大数量
maxActive=50

#最大空闲数量
maxIdle=20

#最小空闲数量
minIdle=5

#超时等待时间
maxWait=60000

#数据库编码
connectionProperties=useUnicode=true;characterEncoding=utf8

#自动事务提交
defaultAutoCommit=true
#事务级别
defaultTransactionIsolation=READ_COMMITTED

初始化代码:

package xgn.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;

public class JDBCHelper {
	private static Properties config=new Properties();
	private static BasicDataSource ds;
	static{
		InputStream in=JDBCHelper.class.getClassLoader().getResourceAsStream("config.properties");
		try {
			config.load(in);
			//1. 使用配置文件初始化BasicDataSource
			ds= (BasicDataSource) BasicDataSourceFactory.createDataSource(config);	
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public static Connection getConnection(){
		try{
			return ds.getConnection();
		}catch(Exception ex){
			throw new RuntimeException(ex);
		}
	}
	
	private static boolean Destory(Connection cn,Statement st,ResultSet rs){
		boolean ret=true;
		if(cn!=null){
			try{
				cn.close();
			}catch(Exception e){
				throw new RuntimeException(e);
			}finally{
				ret=false;
			}
		}
		if(st!=null){
			try{
				st.close();
			}catch(Exception e){
				throw new RuntimeException(e);
			}finally{
				ret=false;
			}
		}
		if(rs!=null){
			try{
				rs.close();	
			}catch(Exception e){
				throw new RuntimeException(e);
			}finally{
				ret=false;
			}
		}
		return ret;
	}
	
}


第二种方法手工初始化连接池

package xgn.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;

public class JDBCHelper {
	private static Properties config=new Properties();
	private static BasicDataSource ds;
	static{
		InputStream in=JDBCHelper.class.getClassLoader().getResourceAsStream("config.properties");
		try {
			config.load(in);
			//1. 使用配置文件初始化BasicDataSource
			//ds= (BasicDataSource) BasicDataSourceFactory.createDataSource(config);	
			//2.手工初始化BasicDataSource
			ds=new BasicDataSource();
			ds.setDriverClassName("com.mysql.jdbc.Driver");
			ds.setDefaultCatalog("test");
			ds.setUrl("jdbc:mysql://127.0.0.1:3306/test");
			ds.setUsername("root");
			ds.setPassword("123456");
			ds.setInitialSize(20);
			ds.setMaxActive(30);
			ds.setMinIdle(10);
			ds.setMaxIdle(20);
			ds.setMaxWait(1000);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public static Connection getConnection(){
		try{
			return ds.getConnection();
		}catch(Exception ex){
			throw new RuntimeException(ex);
		}
	}
	
	private static boolean Destory(Connection cn,Statement st,ResultSet rs){
		boolean ret=true;
		if(cn!=null){
			try{
				cn.close();
			}catch(Exception e){
				throw new RuntimeException(e);
			}finally{
				ret=false;
			}
		}
		if(st!=null){
			try{
				st.close();
			}catch(Exception e){
				throw new RuntimeException(e);
			}finally{
				ret=false;
			}
		}
		if(rs!=null){
			try{
				rs.close();	
			}catch(Exception e){
				throw new RuntimeException(e);
			}finally{
				ret=false;
			}
		}
		return ret;
	}
	
}


使用:

package xgn.servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import xgn.utils.JDBCHelper;

/**
 * Servlet implementation class servlet1
 */
public class servlet1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public servlet1() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("in");
		try{
			Connection cn= JDBCHelper.getConnection();
			Connection cn1=JDBCHelper.getConnection();
			Connection cn2=JDBCHelper.getConnection();
			Connection cn3=JDBCHelper.getConnection();
			Connection cn41=JDBCHelper.getConnection();
			PreparedStatement st=cn.prepareStatement("select * from account");
			ResultSet rs=st.executeQuery();
			while(rs.next()){
				System.out.println(rs.getString("account"));
			}
			cn.close();
		}catch(Exception ex){
			throw new RuntimeException(ex);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}


 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值