DBCP(DataBase connection pool),数据库连接池。是 apache 上的一个 java 连接池项目,也是 tomcat 使用的连接池组件。单独使用dbcp需要2个包:commons-dbcp.jar,commons-pool.jar由于建立数据库连接是一个非常耗时耗资源的行为,所以通过连接池预先同数据库建立一些连接,放在内存中,应用程序需要建立数据库连接时直接到连接池中申请一个就行,用完后再放回去。

Tomcat 的连接池正是采用该连接池来实现的。该数据库连接池既可以与应用服务器整合使用,也可由应用程序独立使用。

首先导入2个jar包

  • commons-dbcp.jar
  • commons-pool.jar

    这里给出一个我经常用的一个jar包下载的地址 jar包集中地

建项目导包,开搞!下面直接贴出配置和 代码。

##database.properties配置文件
url=jdbc:mysql://localhost:3306/libweb
username=root
password=
driverClassName=com.mysql.jdbc.Driver
initialSize=5
maxActive=50
maxIdle=20
minIdle=5
maxWait=60000
connectionProperties=userUnicode=true;characterEncodeing=UTF8
defaultAutoCommit=true
defaultReadOnly=
defaultTransactionIsolation=READ_UNCOMMITTED
package dbex.mysql;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory; /**
* @ClassName: ApacheDBCP
* @Description:apache dbcp的学习使用,使用dbcp的数据库工具类
* @author penny
* @date 2017年12月2日 下午10:29:44
*
*/
public class ApacheDBCP { /**
* 在Java中每一个数据库连接池实现是java.sql.DataRosurce接口,这里的dbcp也不例外
*/
private static DataSource ds=null;
static{
InputStream ins = ApacheDBCP.class.getResourceAsStream("../../database.properties");
Properties pro = new Properties();
try {
System.out.println(ApacheDBCP.class.toString()+":加载配置文件 ");
pro.load(ins);
ds=BasicDataSourceFactory.createDataSource(pro);
} catch (Exception e) {
e.printStackTrace();
}
} /**
*
* @Title: getConnection
* @Description: 获取连接
* @param @return 连接对象conn
* @param @throws SQLException
* @throws
*/
public static Connection getConnection() throws SQLException{
return ds.getConnection();
} public void release(Connection conn,PreparedStatement ppst ,ResultSet rs){
if(conn!=null){
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if(ppst!=null){
try {
ppst.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if(rs!=null){
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* @throws SQLException
* @Title: main
* @Description: 测试 dbcp
* @param @param args
* @throws
*/
public static void main(String[] args) throws SQLException {
Connection conn=null;
PreparedStatement ppst =null;
ResultSet rs= null; conn =ApacheDBCP.getConnection();
System.out.println(conn);
ppst = conn.prepareStatement("select * from user");
rs=ppst.executeQuery();
while(rs.next()){
System.out.println(rs.getInt(1)+"\t"+rs.getString(2));
}
} }

最新文章

  1. .Net使用Redis详解之ServiceStack.Redis(七)
  2. LINQ之路 9:LINQ to SQL 和 Entity Framework(上)
  3. Extjs tree 更改图标
  4. Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe
  5. ORACLE CUP相关
  6. 13. 用Roberts、Sobel、Prewitt和Laplace算子对一幅灰度图像进行边缘检测。观察异同。
  7. Python3基础 type获取变量的类型
  8. CSS之切出横幅
  9. gtest框架使用
  10. HDU 2082-找单词(母函数)
  11. 机器学习:形如抛物线的散点图在python和R中的非线性回归拟合方法
  12. unset与unlink
  13. Life In Changsha College- 第二次冲刺
  14. python网络数据采集(伴奏曲)
  15. Android Acitivy切换平移动画效果实现
  16. 《Python神经网络编程》的读书笔记
  17. SQL Server捕获发生The query processor ran out of internal resources and could not produce a query plan...错误的SQL语句
  18. 【Mongo】安装并配置副本集
  19. Beanstalkd消息队列 -- php类Pheanstalk使用
  20. 登录界面,body上有背景图,点击输入框时,弹出的手机键盘会把背景图顶变形,而且会把footer顶上去

热门文章

  1. 8.3 Customizing Git - Git Hooks
  2. socket_timeout
  3. export命令import命令
  4. centos Dockerfile
  5. ISO/IEC 9899:2011 条款6.4.6——标点符号
  6. 【PHP】解决数据库查询出来的中文内容显示为问号“??”
  7. 运行时给java对象动态的属性赋值
  8. python调用HTMLTestRunner+unittest实现一次执行多个测试类,并生成与每个测试类对应的测试报告,具体看代码,附上整个project代码
  9. Apache2.4的三种模式
  10. 【设计思路】Booking优化