c3p0 的使用

C3P0 是一个开源的 JDBC 连接池它实现了数据源和 JNDI 绑定,支持 JDBC3 规范和 JDBC2 的标准扩展。目前使用它的开源项目有HibernateSpring等。

1、jar 包的导入

c3p0jar 包.PNG
前两个 jar 包是 c3p0 必须使用的包,最后一个包是 MySQL 连接驱动必须要使用的,只要是连接数据库都需要这个包。(只导入前两个连接数据库会失败)

2、c3p0-config.xml 文件

src 目录下建立 c3p0-config.xml 或者 c3p0.properties 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<c3p0-config>
<!-- 使用默认的配置读取连接池对象 -->
<default-config>
<!-- 连接参数 -->
<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">123456</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/</property>
<property name="user">root</property>
<property name="password">123456</property>

<!-- 连接池参数 -->
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">8</property>
<property name="checkoutTimeout">1000</property>
</named-config>
</c3p0-config>

3、连接数据库

1
2
3
4
5
6
7
8
public static void main(String[] args) throws SQLException {
//创建数据库连接对象
DataSource dataSource = new ComboPooledDataSource();
//获取连接对象
Connection connection = dataSource.getConnection();
//输出
System.out.println(connection);
}

Druid 的使用

DRUID 是阿里巴巴开源平台上一个数据库连接池实现,它结合了 C3P0、DBCP、PROXOOL 等 DB 池的优点,同时加入了日志监控,可以很好的监控 DB 池连接和 SQL 的执行情况。

1、jar 包导入

druid 导入.PNG

2、定义配置文件

  • properties形式的
  • 可以叫任意名称,可以放在任意目录下
1
2
3
4
5
6
7
8
9
10
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=123456
#初始化连接数
initialSize=5
#最大连接数
maxActive=10
#超时时间
maxWait=3000

3、连接数据库

1
2
3
4
5
6
7
8
9
10
11
public static void main(String[] args) throws Exception {
//加载配置文件
Properties properties = new Properties();
InputStream resourceAsStream = DruidTest.class.getClassLoader().getResourceAsStream("druid.properties");
properties.load(resourceAsStream);
//获取连接池对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
//获取连接
Connection connection = dataSource.getConnection();
System.out.println(connection);
}

4、JDBC 工具类的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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 JDBCUtils {
//定义成员变量 DataSource
private static DataSource dataSource;

static {
try {
//加载配置文件
Properties properties = new Properties();
properties.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
//获取连接池对象
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

//获取连接
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}

//释放资源
public static void close(Statement statement, Connection connection) {
/* if(statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}*/
//简化
close(null, statement, connection);
}

public static void close(ResultSet resultSet, Statement statement, Connection connection) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

//获取连接池方法
public static DataSource getDataSource() {
return dataSource;
}
}

5、工具类的测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public static void main(String[] args) {
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=null;
try {
//获取连接
connection=JDBCUtils.getConnection();
//sql语句
String sql="select * from users";
statement = connection.prepareStatement(sql);
resultSet=statement.executeQuery();
while (resultSet.next()){
int id=resultSet.getInt(1);
String user=resultSet.getString("user");
String passwd=resultSet.getString("passwd");
System.out.println(id+"--"+user+"--"+passwd);
/*输出
1--aaa--123456
2--bbb--123
3--ccc--111
*/
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtils.close(statement,connection);
}
}

JDBCTemplate 的使用

Spring 框架对 JDBC 的简单封装。提供了一个 JDBCTemplate 对象简化 JDBC 的开发

1、jar 包的导入

JDBCTemplate 包.PNG

2、JDBCTemplate 方法的操作

1、update()方法 :执行增删改。

1
2
3
4
5
6
7
8
9
10
11
public static void main(String[] args) {
//创建JdbcTemplate对象
JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());
//调用方法
String sql="update users set passwd='123' where id=?";
int num=template.update(sql,3);// ? 一一对应
if(num>0){
System.out.println("成功!");
}
}

2、queryForMap():把字段名作为 key,一条数据的值作为 value 封装成一个集合,不能同时查询多条数据。
3、queryForList():将查询的记录封装成 Map,再把 Map 放进 List 集合中。可以查询一条或者多条记录

1
2
3
4
5
6
7
8
9
10
11
public static void main(String[] args) {
//创建JdbcTemplate对象
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
//调用方法
String sql = "select * from users";

List<Map<String, Object>> maps = template.queryForList(sql);
for (Map<String, Object> form : maps) {
System.out.println(form);
}
}

4、query():将查询结果封装成 JavaBean 对象。(new BeanPropertyRowMapper< 类型 >(类型.class))

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class User {
private Integer id;
private String user;
private String passwd;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}
.........
.........
1
2
3
4
5
6
7
8
9
10
11
    public static void main(String[] args) {
//创建JdbcTemplate对象
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
//调用方法
String sql = "select * from users";

List<User> query = template.query(sql, new BeanPropertyRowMapper<User>(User.class));
for(User user:query){
System.out.println(user);
}
}

注意: 定义实体类的时候把基本数据类型全部转换为封装类 ,否则当某一个字段的值为空,将无法进行转换。
5、queryForObject:将查询结果封装成对象。