c3p0相关
1.创建c3p0的配置文件
2.配置文件中的内容为:
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!-- 去掉一头一尾的 默认配置方式 和需要用户重写的方式 , 同事复制下上面的4个name-->
<named-config name="helloc3p0">
<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">abc123</property>
<!-- 当连接池中连接数不够时,c3p0数据源一次性向数据库服务器申请的连接数 -->
<property name="acquireIncrement">5</property>
<!-- 初始化时数据库中连接池的数据 -->
<property name="initialPoolSize">10</property>
<!-- 连接池中维护的最少的连接数 -->
<property name="minPoolSize">5</property>
<!-- 连接池中维护的最多的连接数 -->
<property name="maxPoolSize">100</property>
<!-- 连接池中最多可以维护的Statement的个数 -->
<property name="maxStatements">100</property>
<!-- 每一个连接最多可以维护的Statement的个数 -->
<property name="maxStatementsPerConnection">2</property>
</named-config>
</c3p0-config>
3.JDBCUtils类
public class JDBCUtils {
private static ComboPooledDataSource cpds=null;
static{
//helloc3p0 是配置文件中的name属性值,要对应
cpds = new ComboPooledDataSource("helloc3p0");
}
public static Connection getConnection1() throws SQLException{
Connection conn = cpds.getConnection();
return conn;
}
/**
* 但是这种方式每次连接的时候还是每次都创建了一次连接了一次
* 并没有省事。可以将连接放到外面
* @return
* @throws SQLException
*/
/*public static Connection getConnection1() throws SQLException{
ComboPooledDataSource cpds = new ComboPooledDataSource("helloc3p0");
Connection conn = cpds.getConnection();
return conn;
}*/
dbutils相关
public class BaseDao<T> {
//创建dbutils类对象
QueryRunner qr=new QueryRunner();
private Class<T> type;
/**
* 构造
*/
public BaseDao(){
//getGenericSuperclass()获得带有泛型的父类,getSuperclass()获得该类的父类
////Type是 Java 编程语言中所有类型的公共高级接口。它们包括原始类型、参数化类型、数组类型、类型变量和基本类型。
Type genericSuperclass = this.getClass().getGenericSuperclass();
//ParameterizedType参数化类型,即泛型
ParameterizedType pt=(ParameterizedType)genericSuperclass;
//getActualTypeArguments获取参数化类型的数组,泛型可能有多个
Type[] typeArguments = pt.getActualTypeArguments();
this.type=(Class<T>) typeArguments[0];
}
/**
* 通用增删改方法
*/
public int update(String sql,Object...params){
int count=0;
Connection connection=null;
try {
//连接数据库
connection = JDBCUtils.getConnection();
//增删改
count = qr.update(connection, sql, params);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JDBCUtils.releaseConnection(connection);
}
return count;
}
/**
* 通用查询一行方法(T必须符合bean规范,否则部分变量会有null值现象)
*/
public T getBean(String sql,Object...params){
T t=null;
Connection connection = JDBCUtils.getConnection();
//System.out.println("connection:"+connection);
try {
t= qr.query(connection, sql, new BeanHandler<T>(type), params);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
// JDBCUtils.releaseConnection(connection);
}
return t;
}
/**
* 通用查询多行(T必须符合bean规范,否则部分变量会有null值现象)
*/
public List<T> getBeanList(String sql,Object...params){
List<T> list=new ArrayList<T>();
Connection connection=null;
connection = JDBCUtils.getConnection();
try {
list= qr.query(connection, sql, new BeanListHandler<T>(type), params);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JDBCUtils.releaseConnection(connection);
}
return list;
}
}