一、dbcp
相应的 包:
mysql-jdbc.jar
commons-dbcp.jar
commons-pool.jar
硬编码使用DBCP(在代码中添加配置)
@Test
public void test1() throws Exception {
BasicDataSource source = new BasicDataSource();
// 设置连接的信息
source.setDriverClassName("com.mysql.jdbc.Driver");
source.setUrl("jdbc:mysql://localhost:3306/jdbc-pool");
source.setUsername("root");
source.setPassword("123");
Connection connection = source.getConnection();
String sql = "select * from student";
Statement createStatement = connection.createStatement();
ResultSet executeQuery = createStatement.executeQuery(sql);
while (executeQuery.next()) {
System.out.println(executeQuery.getString(2));
}
connection.close(); // 回收
}
软编码使用DBCP(在项目中添加配置文件)
#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/day2
username=root
password=111
#<!-- 初始化连接 -->
initialSize=10
#最大连接数量
maxActive=50
#<!-- 最大空闲连接 -->
maxIdle=20
#<!-- 最小空闲连接 -->
minIdle=5
#<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
maxWait=6000
代码实现
@Test
public void testSoft() throws Exception {
BasicDataSourceFactory factory = new BasicDataSourceFactory();
Properties properties = new Properties();
// 配置文件添加到properties对象中 javase
properties.load(new FileInputStream("src/info.properties"));
// 生成连接池子 需要配置文件
DataSource source = factory.createDataSource(properties);
Connection connection = source.getConnection();
String sql = "select * from student";
Statement createStatement = connection.createStatement();
ResultSet executeQuery = createStatement.executeQuery(sql);
while (executeQuery.next()) {
System.out.println(executeQuery.getString(2));
}
connection.close(); // 回收
}
二、cp30
需要的包
c3p0-0.9.1.2.jar
mysql-connector-java-5.0.8.jar
配置文件(c3p0-config.xml)
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!-- 默认配置,如果没有指定则使用这个配置 -->
<default-config>
<!-- 基本配置 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc-pool?characterEncoding=utf-8</property>
<property name="user">root</property>
<property name="password">123</property>
<!--扩展配置 -->
<!-- 连接超过30秒报错 -->
<property name="checkoutTimeout">30000</property>
<!--30秒检查空闲连接 -->
<property name="idleConnectionTestPeriod">30</property>
<property name="initialPoolSize">10</property>
<!-- 30秒不适用丢弃 -->
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
</default-config>
<!-- 命名的配置 -->
<named-config name="bruce">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/day2</property>
<property name="user">root</property>
<property name="password">111</property>
<!-- 如果池中数据连接不够时一次增长多少个 -->
<property name="acquireIncrement">5</property>
<property name="initialPoolSize">20</property>
<property name="minPoolSize">10</property>
<property name="maxPoolSize">40</property>
</named-config>
</c3p0-config>
工具类:
/**
* 从连接池子中获取连接! C3P0的连接池子 0.获取连接池子对象 DBUtils 1.获取连接 2.关闭资源
*/
public class DataSourceUtils {
private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
/**
* 返回连接池对象方法
* @return c3p0连接池子
*/
public static ComboPooledDataSource getDataSource() {
return dataSource;
}
/**
* 连接池中获取连接的方法
* @return 连接
*/
public static Connection getConnection() {
Connection conn = null;
try {
conn = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
// 关闭资源
public static void close(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void close(Statement st) {
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet set) {
if (set != null) {
try {
set.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Connection conn, Statement st) {
close(conn);
close(st);
}
public static void close(Connection conn, Statement st, ResultSet rt) {
close(conn);
close(st);
close(rt);
}
}
c3p0对数据库进行操作:
@Test
public void test1() throws Exception {
// 1.创建C3P0连接池子
Connection connection = DataSourceUtils.getConnection();
Statement createStatement = connection.createStatement();
String sql = "select * from student;";
ResultSet resultSet = createStatement.executeQuery(sql);
while (resultSet.next()) {
System.out.println(resultSet.getString(1)+"--"+resultSet.getString(2));
}
DataSourceUtils.close(connection, createStatement, resultSet);
}
c3p0与dbcp的区别:
- dbcp没有自动回收空闲连接的功能
- c3p0有自动回收空闲连接功能
- dbcp需要手动设置配置文件(自己写Java代码度配置文件)
- c3p0不需要手动设置(自动读取配置文件)
三、druid
需要的包:
druid-1.1.10.jar
Druid 是目前比较流行的高性能的,分布式列存储的OLAP框架(具体来说是MOLAP)。它有如下几个特点:
-
亚秒级查询
druid提供了快速的聚合能力以及亚秒级的OLAP查询能力,多租户的设计,是面向用户分析应用的理想方式。
-
实时数据注入
druid支持流数据的注入,并提供了数据的事件驱动,保证在实时和离线环境下事件的实效性和统一性
-
可扩展的PB级存储
druid集群可以很方便的扩容到PB的数据量,每秒百万级别的数据注入。即便在加大数据规模的情况下,也能保证时其效性
-
多环境部署
druid既可以运行在商业的硬件上,也可以运行在云上。它可以从多种数据系统中注入数据,包括hadoop,spark,kafka,storm和samza等
-
丰富的社区
druid拥有丰富的社区,供大家学习
工具类:
public class DruidUtils {
// 声明连接池对象
private static DruidDataSource ds;
static {
// /实例化数据库连接池对象
ds = new DruidDataSource();
// 实例化配置对象
Properties properties = new Properties();
try {
// 加载配置文件内容
InputStream fis = new FileInputStream("src/db.properties");
properties.load(fis);
// 设置驱动类全称
ds.setDriverClassName(properties.getProperty("driverClassName"));
// 设置连接的数据库
ds.setUrl(properties.getProperty("url"));
// 设置用户名
ds.setUsername(properties.getProperty("username"));
// 设置密码
ds.setPassword(properties.getProperty("password"));
// 设置最大连接数量
ds.setMaxActive(Integer.parseInt(properties.getProperty("maxActive")));
} catch (IOException e) {
e.printStackTrace();
}
}
// 获取连接对象
public static Connection getConnection() {
try {
return ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
测试:
public class Druid_Test {
@Test
public void test1() {
List<Student> list = findAll();
for (Student student : list) {
System.out.println(student);
}
System.out.println("------------");
Student s = find("黎明", "12345");
System.out.println(s);
}
public List<Student> findAll() {
Connection conn = null;
PreparedStatement pstat = null;
ResultSet rs = null;
List<Student> students = new ArrayList<Student>();
try {
conn = DruidUtils.getConnection();
pstat = conn.prepareStatement("select * from student");
rs = pstat.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
students.add(new Student(id,username,password));
}
return students;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public Student find(String username, String password) {
Connection conn = null;
PreparedStatement pstat = null;
ResultSet rs = null;
Student student = null;
try {
conn = DruidUtils.getConnection();
pstat = conn.prepareStatement("select * from student where username=? and password=?");
pstat.setString(1, username);
pstat.setString(2, password);
rs = pstat.executeQuery();
if (rs.next()) {
int id = rs.getInt("id");
student=new Student(id,username,password);
}
return student;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}