数据库链接 ——> 执行完毕 ——> 释放
但是 上述过程十分耗费资源,因此出现了池化技术
预先准备好一些资源,预备链接
相当于一个池子,链接的时候把资源拿出来 ,不用了放回去继续预备
实现接口DataSource
使用数据库连接池之后,就不用编写数据库连接的代码
数据库连接代码:
public class BaseDao {
/*
* 1.加载数据库
* 2.获取数据库连接
* 3.编写查询语句
* 4.释放资源
*/
private static String driver; //数据库驱动器
private static String url ; //数据库连接语句
private static String username ; //数据库用户名
private static String password ; //数据库密码
//1.利用静态代码块加载数据库 (利用类加载器在类初始化的时候直接加载配置文件)
static{
//
InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
}
//2.获取数据库连接
public static com.mysql.jdbc.Connection getConnection() {
com.mysql.jdbc.Connection connection = null;
try {
//加载驱动
Class.forName(driver);
connection = (com.mysql.jdbc.Connection) DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
//3.编写查询语句
public static ResultSet execute(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet,String sql,Object[] paras)
{
try {
preparedStatement = connection.prepareStatement(sql); //预编译sql语句
for(int i=0;i<paras.length;i++)
{
preparedStatement.setObject(i+1,paras[i]);
}
resultSet = preparedStatement.executeQuery();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return resultSet;
}
public static int execute(Connection connection,PreparedStatement preparedStatement,Object[] paras,String sql){
int updateRows = 0;
try {
preparedStatement = connection.prepareStatement(sql);
for(int i=0;i<paras.length;i++)
{
preparedStatement.setObject(i+1,paras[i]);
}
updateRows = preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return updateRows;
}
//4.释放资源
public static boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet)
{
boolean flag = true;
if(resultSet!=null)
{
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
flag = false;
}
}
if(preparedStatement!=null)
{
try {
preparedStatement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
flag = false;
}
}
if(connection!=null)
{
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
flag = false;
}
}
return flag ;
}
}
如果使用数据库连接池,上述代码中的加载数据库、获得连接就可以省略掉
工具类代码实例
public class JdbcUtils_DBCP {
private static DataSource dataSource = null ;
//1.利用静态代码块加载数据库 (利用类加载器在类初始化的时候直接加载配置文件)
static{
//数据连接池依旧需要读取配置文件
InputStream is = JdbcUtils_DBCP.class.getClassLoader().getResourceAsStream("DBCPconfig.properties");
Properties properties = new Properties();
try {
properties.load(is);
//创建数据源 : 通过工厂模式创建数据源对象
dataSource = BasicDataSourceFactory.createDataSource(properties);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
//2.获取数据库连接
public static com.mysql.jdbc.Connection getConnection() throws SQLException {
return (com.mysql.jdbc.Connection) dataSource.getConnection();
}
//3.释放资源
public static boolean closeResource(Connection connection)
{
boolean flag = true;
if(connection!=null)
{
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
flag = false;
}
}
return flag ;
}
}
DBCP配置文件
#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/stu_system?characterEncoding=utf-8&useSSL=false
username=root
password=180018ly
#
initialSize=10
#最大连接数量
maxActive=50
#
maxIdle=20
#
minIdle=5
#
maxWait=60000
#JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:[属性名=property;]
#注意:"user" 与 "password" 两个属性会被明确地传递,因此这里不需要包含他们。
connectionProperties=useUnicode=true;characterEncoding=gbk
#指定由连接池所创建的连接的自动提交(auto-commit)状态。
defaultAutoCommit=true
#driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。
#可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation=READ_UNCOMMITTED
ticle/details/90313176
显然开源数据库连接池在代码层面并没有简化太多,但是大大优化了运行效率
c3p0-0.9.1.2.jar
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driverproperty>
<property name="jdbcUrl">jdbc:mysql://localhost/stdproperty>
<property name="user">rootproperty>
<property name="password">rootproperty>
<property name="initialPoolSize">10property>
<property name="maxIdleTime">30property>
<property name="maxPoolSize">100property>
<property name="minPoolSize">10property>
<property name="maxStatements">200property>
default-config>
<named-config name="MySQL">
<property name="driverClass">com.mysql.jdbc.Driverproperty>
<property name="jdbcUrl">jdbc:mysql://localhost/stdproperty>
<property name="user">rootproperty>
<property name="password">rootproperty>
<property name="initialPoolSize">10property>
<property name="maxIdleTime">30property>
<property name="maxPoolSize">100property>
<property name="minPoolSize">10property>
<property name="maxStatements">200property>
named-config>
c3p0-config>
工具类代码实例
//代码版本配置
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("MySQL");
comboPooledDataSource.setDriverClass("");
comboPooledDataSource.setJdbcUrl();
comboPooledDataSource.setUser();
comboPooledDataSource.setPassword();
//....连接池大小等其他配置
//文件配置
package com.liu.util;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class C3p0_Utils {
private static DataSource dataSource = null ;
//1.利用静态代码块加载数据库 (利用类加载器在类初始化的时候直接加载配置文件)
static{
//创建数据源 : 通过工厂模式创建数据源对象
dataSource = new ComboPooledDataSource("MySQL"); //使用命名配置(xml文件自动读取)
}
//2.获取数据库连接
public static com.mysql.jdbc.Connection getConnection() throws SQLException {
return (com.mysql.jdbc.Connection) dataSource.getConnection();
}
//3.编写查询语句
public static ResultSet execute(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet, String sql, Object[] paras)
{
try {
preparedStatement = connection.prepareStatement(sql); //预编译sql语句
for(int i=0;i<paras.length;i++)
{
preparedStatement.setObject(i+1,paras[i]);
}
resultSet = preparedStatement.executeQuery();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return resultSet;
}
public static int execute(Connection connection,PreparedStatement preparedStatement,Object[] paras,String sql){
int updateRows = 0;
try {
preparedStatement = connection.prepareStatement(sql);
for(int i=0;i<paras.length;i++)
{
preparedStatement.setObject(i+1,paras[i]);
}
updateRows = preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return updateRows;
}
//4.释放资源
public static boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet)
{
boolean flag = true;
if(resultSet!=null)
{
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
flag = false;
}
}
if(preparedStatement!=null)
{
try {
preparedStatement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
flag = false;
}
}
if(connection!=null)
{
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
flag = false;
}
}
return flag ;
}
}
所有数据源都是基于DataSource接口