1.JDBC的数据库连接池使用javax.sql.DataSource来表示,DataSource只是一个接口,该接口通常由第三方提供实现
2.C3P0数据库连接池,速度相对较慢,稳定性好,
3.DBCP数据库连接池,速度相对C3P0较好,但不稳定
4.Proxool数据库连接池,有监控连接池状态的功能,稳定性较C3P0差一点
5.BoneCP数据库连接池,速度快
6.Druid(德鲁伊)是阿里提供的数据库连接池,集合DBCP、C3P0、Proxool优点于一体的数据库连接池
package com.jh.datasource;
//演示C3P0的使用
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.jupiter.api.Test;
import java.beans.PropertyVetoException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
public class C3P0_ {
//方式一:相关参数,在程序中指定user、url、password
@Test
public void testC3P0_01() throws IOException, PropertyVetoException, SQLException {
//1.创建一个数据源对象
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
//2.通过配置文件mysql.properties获取相关连接的信息
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
//读取相关的值
String user = properties.getProperty("user");
String driver = properties.getProperty("driver");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
//给数据源comboPooledDataSource设置相关的参数,连接管理由comboPooledDataSource管理
comboPooledDataSource.setDriverClass(driver);
comboPooledDataSource.setJdbcUrl(url);
comboPooledDataSource.setUser(user);
comboPooledDataSource.setPassword(password);
//设置初始化连接数
comboPooledDataSource.setInitialPoolSize(10);
comboPooledDataSource.setMaxPoolSize(50);//最大连接数
//从DataSource接口实现
Connection connection = comboPooledDataSource.getConnection();
System.out.println("连接成功...");
}
//第二种方式 使用配置文件模板完成
@Test
public void testC3P0_02() throws SQLException {
//1.将c3p0提供了c3p0.config.xml拷贝到src目录下
//2.该文件指定了连接数据库和连接词池的相关参数
//3.创建一个数据源对象
ComboPooledDataSource tom = new ComboPooledDataSource("tom");
//4.从DataSource接口实现,连接
Connection connection = tom.getConnection();
System.out.println("配置文件模板...");
connection.close();
}
}
package com.jh.datasource; //测试druid的使用 import com.alibaba.druid.pool.DruidDataSourceFactory; import org.junit.jupiter.api.Test; import javax.sql.DataSource; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.util.Properties; public class Druid_ { @Test public void testDruid() throws Exception { //1.加入Druid的jar包 //2.加入配置文件 //3.创建配置文件 Properties properties = new Properties(); properties.load(new FileInputStream("src\\druid.properties")); //4.创建一个指定参数的数据库连接池 DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); //5.连接 Connection connection = dataSource.getConnection(); System.out.println("连接成功..."); //关闭资源 connection.close(); } }
package com.jh.datasource; //基于druid数据库连接池的工具类 import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.FileInputStream; 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 JDBCUtilsDruid { private static DataSource ds; static {//静态代码块完成ds的初始化 Properties properties = new Properties(); try { properties.load(new FileInputStream("src\\druid.properties")); //创建指定参数的连接池 ds = DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { throw new RuntimeException(e); } } //编写getConnection方法 public static Connection getConnection(){ try { return ds.getConnection(); } catch (SQLException e) { throw new RuntimeException(e); } } //关闭连接(在数据库连接池技术中,close是使用的connection连接对象放回连接池) public static void close(ResultSet resultSet, Statement statement,Connection connection){ try { if(resultSet != null) { resultSet.close(); } if (statement != null){ statement.close(); } if (connection != null){ connection.close(); } } catch (SQLException e) { throw new RuntimeException(e); } } }
package com.jh.datasource; import org.junit.jupiter.api.Test; import java.sql.*; public class JDBCUtilsDruid_use { @Test public void testSelect(){ //1.得到连接 Connection connection = null; //2.组织sql String sql = "select * from admin02 where id = ?"; PreparedStatement preparedStatement = null; ResultSet resultSet = null; //3.创建PreparedStatement的对象 try { connection = JDBCUtilsDruid.getConnection(); preparedStatement = connection.prepareStatement(sql); preparedStatement.setInt(1,1); resultSet = preparedStatement.executeQuery(); while (resultSet.next()){ int id = resultSet.getInt("id"); String username = resultSet.getString("username"); String password = resultSet.getString("password"); System.out.println(id + " " + username + " " + password); } } catch (SQLException e) { throw new RuntimeException(e); }finally { //6.关闭资源 JDBCUtilsDruid.close(resultSet,preparedStatement,connection); } } }