-
1.概念:
其实就是一个容器(集合),存放数据库连接的容器。当系统初始化好后,容器被创建,容器中会申请一些连接对象,当用户来访问数据库时,从容器中获取连接对象,对象访问完之后,会将连接对象归还给容器。
-
2.好处:
1.节约资源 2.用户访问高效
-
3.实现:
-
1.标准接口:DataSource javax.sql包下的
1.方法: *获取连接:getConnection() *归还连接:Connection.close().如果连接对象Connection是从连接池中获取的,那么调用Connection.close()方法,则不会再关闭连接了。而是归还连接
2.一般不去实现它,由数据库厂商来实现
1.C3P0:数据库连接池技术 2.Druid:数据库连接池实现技术,由阿里巴巴提供
4.C3P0:数据库连接池技术
步骤:
1.导入两个jar包 c3p0-0.9.2.1.jar mchange-commons-java-0.2.3.4.jar
2.定义配置文件:
*名称:c3p0.properties或者c3p0-config.xml
*路径:直接放在src目录下
3.创建核心对象 数据库连接池对象 comboPooledDataSource
4.获取连接:getConnection
配置文件:
<c3p0-config>
<!-- 使用默认的配置读取连接池对象 -->
<default-config>
<!-- 连接参数 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/db1</property>
<property name="user">root</property>
<property name="password">password</property>
<!-- 连接池参数 -->
<!--初始化申请的连接数量-->
<property name="initialPoolSize">5</property>
<!--最大的连接数量-->
<property name="maxPoolSize">10</property>
<!--超时时间-->
<property name="checkoutTimeout">3000</property>
</default-config>
<named-config name="otherc3p0">
<!-- 连接参数 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="user">root</property>
<property name="password">root</property>
<!-- 连接池参数 -->
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">8</property>
<property name="checkoutTimeout">1000</property>
</named-config>
</c3p0-config>
package C3P0;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class C3p0Demo {
public static void main(String[] args) throws SQLException {
//1.获取DataSource,使用默认配置
DataSource dataSource = new ComboPooledDataSource();
//2.获取连接
for (int i = 1; i <=11 ; i++) {
Connection connection = dataSource.getConnection();
System.out.println(i+":"+connection);
if(i == 5){
connection.close();//归还连接到连接池中
}
}
}
}
5.Druid:数据库连接池实现技术,由阿里巴巴提供
1.步骤:
1.导入jar包 druid-1.0.9.jar
2.定义配置文件:
*是properties形式的
*可以叫任意名称,可以放在任意目录下
3.获取数据连接池对象:通过工厂来获取 DruidDataSourceFactory
4.获取连接:getConnection
2.定义工具类
1.定义一个类JDBCUtils
2.提供静态代码块加载配置文件,初始化连接池对象
3.提供方法
1.获取连接方法:通过数据库连接池获取连接
2.释放资源
3.获取连接池的方法
配置文件:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db1
username=root
password=password
initialSize=5
maxActive=10
maxWait=3000
maxIdle=8
minIdle=3
package util;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCDruidUtils {
private static DataSource ds;
static {
try {
Properties pro = new Properties(); pro.load(JDBCDruidUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
public static void close(Statement statement, Connection connection){
/*if(statement != null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(connection != null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}*/
close(null,statement,connection);
}
public static void close(ResultSet resultSet,Statement statement, Connection connection){
if(resultSet != null){
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(statement != null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(connection != null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static DataSource getDataSource(){
return ds;
}
}
package Druid;
import util.JDBCDruidUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DruidJDBCTest {
public static void main(String[] args) {
try {
Connection connection = JDBCDruidUtils.getConnection();
String sql = "insert into admin values(null,?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,"zhaoliu");
preparedStatement.setString(2,"123");
preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}