c3p0
c3p0的配置
1. 使用c3p0数据库连接池实现连接
1.1. 需要引入c3p0 .jar 包
1.2. 新建配置文件[src 目录下面新建c3p0-config.xml]
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<named-config name="mvcapp">
<property name="user">root</property>
<property name="password"></property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/java</property>
<property name="acquireIncrement">5</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">10</property>
<property name="maxPoolSize">50</property>
<property name="maxStatements">20</property>
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>
JdbcUtils.java
负责连接数据库
package com.curtis.jdbc;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JdbcUtils {
private static DataSource dataSource = null;
static {
// 数据源只能被创建一次
// 这个mvcapp跟数据库连接池里面的对应
dataSource = new ComboPooledDataSource("mvcapp");
}
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
public static void releaseConnection(Connection connection) {
try {
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
DAO.java[带泛型的参数]
接口:数据库交互用到方法[查询所有记录、查询单条记录、增删改、查询单个值...]
package com.curtis.dao;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
public interface DAO<T> {
void batch(Connection connection, String sql, Object[]... args)
throws SQLException;
// 查询单个值
<E> E getValue(Connection connection, String sql, Object... args)
throws SQLException;
// 查询所有数据
List<T> getAllList(Connection connection, String sql, Object... args)
throws SQLException;
// 查询一条数据
T getOne(Connection connection, String sql, Object... args)
throws SQLException;
// insert update delete
void update(Connection connection, String sql, Object... args)
throws SQLException;
}
JdbcDaoImpl.java[带泛型的参数]
通过QueryRunner 实现DAO接口里面的方法
package com.curtis.dao;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
public class JdbcDaoImpl<T> implements DAO<T> {
private QueryRunner queryRunner = null;
private Class<T> clazz;
// 构造函数
@SuppressWarnings("unchecked")
public JdbcDaoImpl() {
queryRunner = new QueryRunner();
Type superClass = getClass().getGenericSuperclass();
if (superClass instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) superClass;
Type[] typeArgs = parameterizedType.getActualTypeArguments();
if (typeArgs != null && typeArgs.length > 0) {
if (typeArgs[0] instanceof Class) {
clazz = (Class<T>) typeArgs[0];
}
}
}
}
@Override
public void batch(Connection connection, String sql, Object[]... args)
throws SQLException {
queryRunner.batch(connection, sql, args);
}
@SuppressWarnings("unchecked")
@Override
public <E> E getValue(Connection connection, String sql, Object... args)
throws SQLException {
return (E) queryRunner
.query(connection, sql, new ScalarHandler(), args);
}
@Override
public List<T> getAllList(Connection connection, String sql, Object... args)
throws SQLException {
return queryRunner.query(connection, sql, new BeanListHandler<>(clazz),
args);
}
@Override
public T getOne(Connection connection, String sql, Object... args)
throws SQLException {
return queryRunner.query(connection, sql, new BeanHandler<>(clazz),
args);
}
@Override
public void update(Connection connection, String sql, Object... args)
throws SQLException {
queryRunner.update(connection, sql, args);
}
}
实体类继承JdbcDaoImpl.java,指明具体的实体
package com.curtis.table;
public class Student {
private int flow_id;
private int type;
private String id_card;
private String student_name;
private String location;
private int grade;
public int getFlow_id() {
return flow_id;
}
public void setFlow_id(int flow_id) {
this.flow_id = flow_id;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getId_card() {
return id_card;
}
public void setId_card(String id_card) {
this.id_card = id_card;
}
public String getStudent_name() {
return student_name;
}
public void setStudent_name(String student_name) {
this.student_name = student_name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
@Override
public String toString() {
return "Student [flow_id=" + flow_id + ", type=" + type + ", id_card="
+ id_card + ", student_name=" + student_name + ", location="
+ location + ", grade=" + grade + "]";
}
}
package com.curtis.table;
import com.curtis.dao.JdbcDaoImpl;
public class StudentDao extends JdbcDaoImpl<Student> {
}
单元测试类测试各个方法是否愉快
@Test
public void testGet() {
Connection connection = null;
try {
connection = JdbcUtils.getConnection();
String sql = "select flow_id,type,id_card,student_name,location,grade from student where flow_id=?";
Student student=studentDao.getOne(connection, sql, 1001);
System.out.println(student);
} catch (Exception e) {
e.printStackTrace();
} finally{
JdbcUtils.releaseConnection(connection);
}
}