whystudyjava

连接池

c3p0连接池操作

  1. 导入c3p0和mysql驱动需要的包

  2. 在src目录下创建c3p0-config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <c3p0-config>
    <!-- This app is massive! -->
    <named-config name="mysql">
       <!--   连接MySQL基本的配置-->
       <property name="jdbcUrl">jdbc:mysql://localhost:3306/mvcproject?serverTimezone=Asia/Shanghai</property>
       <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
       <property name="user">root</property>
       <property name="password">root</property>

       <!--       若数据库中的连接数量不足时,向数据库申请的连接数量-->
       <property name="acquireIncrement">5</property>
       <!--       初始化数据库连接池时链接的数量-->
       <property name="initialPoolSize">10</property>
       <!--       数据库连接池中最小的数据库链接数-->
       <property name="minPoolSize">5</property>
       <!--       数据库连接池中最大的数据库链接数-->
       <property name="maxPoolSize">100</property>

       <!-- intergalactoApp adopts a different approach to configuring statement caching -->
       <!--       c3p0数据库连接池可维护的Statement数量-->
       <property name="maxStatements">2</property>
       <!--       每个连接同时可使用的Statement数量-->
       <property name="maxStatementsPerConnection">5</property>

    </named-config>
    </c3p0-config>
  3. 创建JdbcUtils

    import com.mchange.v2.c3p0.ComboPooledDataSource;

    import javax.sql.DataSource;
    import java.sql.Connection;
    import java.sql.SQLException;

    /**
    * jdbc工具类
    *
    * @author why
    */
    public class JdbcUtils {

       //数据库连接池,C3P0
       private static DataSource dataSource = null;
       static {//静态代码块只会被执行一次
           dataSource = new ComboPooledDataSource();
      }

       /**
        * 获取到mysql的数据库连接对象
        *
        * @return conn
        */
       public static Connection getConnection(){
           Connection conn = null;
           try {
               conn = dataSource.getConnection();
          } catch (SQLException e) {
               e.printStackTrace();
          }
           return conn;
      }
       
       /**
        * 通用的关闭数据库连接对象的方法
        * @param conn
        */
       public static void closeConn(Connection conn){
           if (conn != null){
               try {
                   conn.close();
              } catch (SQLException e) {
                   e.printStackTrace();
              }
          }
      }
    }
  4. 创建BaseDao

    package com.why.study2.dao;

    import com.why.study2.utils.JdbcUtils;
    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;

    import java.lang.reflect.ParameterizedType;
    import java.lang.reflect.Type;
    import java.sql.Connection;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;

    /**
    * 这是一个dao层中的基本类,在于被具体的dao类继承,不能直接new BasesDao()来直接使用
    *
    * @author why
    * @param <T>   针对要操作各章数据包映射到java工程里的java类
    */
    public class BaseDao<T> {

       QueryRunner queryRunner = new QueryRunner();

       private Class<T> Clazz;

       /**
        * 反射拿到T.class
        */
       public BaseDao() {
           //用BaseDao的构造器初始化aClass属性
           //拿到User类
           Type superType = this.getClass().getGenericSuperclass();//getGenericSuperclass()拿到调用者的父类的类型
           //拿到User.class
           if (superType instanceof ParameterizedType){
               ParameterizedType pt = (ParameterizedType) superType;
               Type[] tarry = pt.getActualTypeArguments();//返回类型数组,其第一个元素是我们需要的T.class
               if (tarry[0] instanceof Class){
                   Clazz = (Class<T>) tarry[0];
              }
          }
      }

       /**
        * 查询数据表,取出结果sql语句的结果集的第一条数据,封装成一个类对象返回,不支持事务
        * 用到dbutils工具类
        * @param sql
        * @param args
        * @return
        */
       public T get(String sql,Object... args){
           Connection conn = null;
           T entity = null;

           //拿到conn
           try {
               conn = JdbcUtils.getConnection();
               entity = queryRunner.query(conn,sql, new BeanHandler<T>(Clazz),args);
          } catch (Exception e) {
               e.printStackTrace();
          } finally {

分类:

技术点:

相关文章: