数据库连接池以及ApacheCommonDBUtils工具类使用

jefxff 153,579 2020-05-03

连接池

  • 普通的数据库连接在打开和关闭连接时比较损耗性能, 而且是和数据库的直接交互, 连接池就是一个维护数据库连接的管理员, 手中经常有数据库的连接, 当需要连接的时候, 直接从管理员手中拿, 用完了之后还给管理员, 而且不需要直接和数据库交互
  • 常见的连接池: Tomcat-dbcp, dbcp, c3p0, druid
  • 数据源(javax.sql.Datasource)包含了数据池, 数据源可以管理数据池
  • 以前需要Class.forName()加载驱动, 通过DriverManager.getConnection()直接从数据库获取连接; 而用连接池的核心就是: 将连接的指向改了, 现在指向的是数据源而不是数据库
  • 数据库访问的核心 --> pstmt/stmt -> Connection -> 1. 直连数据库 2. 数据源 (ds.getConnection())

Tomcat-dbcp: 不常用, 了解

  • 类似于 jndi, 在Context.xml中配置下面的MySQL Resource
  • Content.xml中MySQL的配置
	<Resource
		<!-- name指定Resource的JNID名字 -->
		name="jdbc/mysql"
		<!-- 指定Resource的管理者, 有两个可选: Container和Applocation;Container:由容器来创建Resource; Application: 由Web应用来创建和管理Resource -->
		auth="Container"
		<!-- type: 指定Resource的类型 -->
		type="javax.sql.DataSource"
		<!-- 指定连接池中, 处于活动状态的数据库连接的最大数量, 如果值为0, 标识不受限制 -->
		maxActive="100"
		<!-- 指定连接池中, 处于空闲状态的数据库连接的最大数量, 如果值为0, 标识不受限制  -->
		maxIdle="30"
		<!-- 指定连接池中,连接处于空闲状态的最长时间(单位为毫秒), 如果超出此最长时间将会抛出异常; 如果值为-1, 表示允许无限制等待 -->
		maxWait="10000"
		<!-- 指定数据库访问名 -->
		username="root"
		<!-- 指定数据库访问密码 -->
		password="password"
		<!-- 指定连接数据库的驱动程序的类名 -->
		driverClassName="com.mysql.cj.jdbc.Driver"
		<!-- 指定连接数据库的RUL -->
		url="jdbc:mysql://127.0.0.1:3306/blog?useSSL=false&amp;serverTimezone=UTC"
		/>
  • 在项目里面的web.xml中指定context.xml里面配置的数据源; 就是指定name,type, auth三个属性的值
  • 在DBUtils.java 中将之前通过DriverManager.getConnection(URL, NAME, PWD)获取连接改为通过 Context ctx = new InitialContext();先获取Context.xml; 再通过 DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");获取DataSource对象, 再通过DataSource对象获取连接 Connection conn = ds.getConnection();

dbcp 连接池

  • 需要引入jar包 commons-dbcp-1.4.jar, commons-pool.jar
  • 获取ds的核心类(二选一): BasicDataSource , BasicDataSourceFactory

BasicDataSource类的方式(硬编码方式)

  • 1.创建 BasicDataSource dbcp = new BasicDataSource();
  • 2.通过下面的常用方法设置各种属性即可拿到 dbcp
  • 3.通过 dbcp.getConnrction()获取数据库连接即可

常用方法

  • void serDriverClassName(String driverClassName) : 设置连接数据库的驱动名
  • void setUrl(String url) : 设置连接数据库的URL
  • void setUsername(Stirng Username) : 设置连接数据库的用户名
  • void setPassword(Stirng Password) : 设置连接数据库的密码
  • void setInitialiSize(int initialSize) : 设置初始化时, 连接数据池中的连接数量
  • void setMaxActive(int maxActive) : 设置连接池中, 处于活动状态的数据库连接最大数量
  • void setMinIdle(int minIdle) : 设置连接池中, 处于空闲状态的数据库连接的最小数量
  • Collection getConnection() : 从连接池中获取一个数据库连接

BasicDataSourceFactory 配置方式(dbcpconfig.properties配置文件; 全部是k=v)

  • 编写dbcpconfig.properties配置文件(格式k=v中间不要由空格)
  • 通过创建 Properties 类的实例, 调用该实例的load()方法将配置文件加载进来; 需要注意的是load()方法的参数是流的形式
    • 创建 Properties 实例: Properties props = new Properties();
    • 将字符串编程输入流: InputStream input = new DBCPDemo().getClass().getClassLoader().getResourceAsStream("dbcpconfig.properties");
    • 调用load()方法: props.load(input);
  • 通过 DataSource dbcp = BasicDataSourceFactory.createDataSource(prop); 方法即可创建dbcp
  • dbcpconfig.properties 示例
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/blog?useSSL=false&serverTimezone=UTC
username=root
password=password
initialSize=10

C3P0 连接池

  • 核心类 ComboPooledDataSource
  • C3P0是将DBCP的两种方式(硬编码和配置文件)合二为一; 通过ComboPooledDataSource的构造参数来区分, 无参数的就是硬编码; 而有参数的就是通过配置文件的方式
  • 无参数构造方法步骤:
    • 创建 ComboPooledDataSource c3p0 = new ComboPooledDataSource(); 对象
    • 通过 c3p0的set方法设置Driver,Url,User,Passwd等
    • return c3p0; 即可
    • 连接时 通过 c3p0.getConnection()即可
  • 有参数构造方法, 配置文件(c3p0-config.xml)
    • 在IDE的src目录中编写c3p0-config.xml文件, 注意的点: Url中出现分号(;)时, 应该使用(amp;)这种形式
    • 使用时直接 return new ComboPooledDataSource("jefxff"); 即可返回 c3p0
  • c3p0-config.xml 示例
	<?xml version="1.0" encoding="UTF-8" ?>
	<c3p0-config>
	    <!-- 默认 -->
	    <default-config>
	        <!-- 如果要深究某个xml中可以设置那些属性, 就找相关类的属性, 或者setter()方法 -->
	        <property name="user">root</property>
	        <property name="password">password</property>
	        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
	        <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/blog?useSSL=false&amp;serverTimezone=UTC</property>
	        <property name="checkoutTimeout">30000</property>
	    </default-config>

	    <!-- 自定义 -->
	    <named-config name="jefxff">
	        <property name="user">root</property>
	        <property name="password">password</property>
	        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
	        <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/blog?useSSL=false&amp;serverTimezone=UTC</property>
	        <property name="checkoutTimeout">20000</property>
	    </named-config>
	</c3p0-config>

C3P0的核心类ComboPooledDataSource常用方法

  • public ComboPooledDataSource() : 无参构造方法(硬编码发方式)
  • public ComboPooledDataSource(String configName) : 加载配置文件的构造方法
  • void setDriverClass(String driverClass) : 设置数据库连接的驱动
  • void setJdbcUrl(String jdbcUrl) : 设置连接数据库的URL
  • void setUser(Stirng User) : 设置数据库连接的用户名
  • void setPassword(String password) : 设置数据库连接的密码
  • void setMaxPoolSize(int maxPoolSize) : 设置连接池的最大连接数目
  • void setMinPoolSize(int minPoolSize) : 设置连接池的最小连接数目
  • void setInitiaPoolSize(int initiaPoolSize) : 设置初始化时, 连接池中的连接数量
  • Connection getConnection() : 从连接池中获取一个数据库连接, 该方法由ComboPooledDataSource 的父类 AbstractPoolBackedDataSource提供

连接池总结

  • 硬编码: 获取某个连接池数据源的对象 ds = new XxxDataSource(); ds.setXxx(); return ds;
  • 配置文件: 编写配置文件, ds = new XxxDataSource(); 加载配置文件; return ds;

连接池代码示例

package xyz.xmcs.DataSourceUtils;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author jefxff
 * @date 2020/3/11 - 16:17
 * DataSource 工具类, 可以通过该类获取 Tomcat-dbcp , DBCP, C3P0 类型连接池的 DataSource,
 *      通过该 DataSource 即可创建数据库的连接 Connection
 */
public class DataSourceUtils {

    /**
     * 此方法第一步: 需要配置tomcat目录下的context.xml, 添加 <Resource 标签
     * 第二步: 需要在项目中的web.xml中配置 resource-ref 标签
     * @return
     * @throws NamingException
     */
    public static DataSource getTomcatDbcpBySet() throws NamingException {
        // 1. 相当于获取Context.xml
        Context ctx =  new InitialContext();
        // 2. 通过 lookup()方法去web.xml中找配置的<res-res-name字段的值,
        //      但是要注意 字段名字前面要添加 "java:comp/env/"
        return (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
    }

    /**
     * 1. 此方法需要两个jar包: commons-dbcp-1.4.jar, commons-pool.jar
     * 2. 通过创建 BasicDataSource 的实例, 然后通过该实例调用setter方法通过硬编码的方式,
     *      将Driver, url, name, pwd 等值通过setter方法指定
     * @return
     */
    public static DataSource getDbcpBySet(){

        BasicDataSource dbcp = new BasicDataSource();
        dbcp.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dbcp.setUrl("jdbc:mysql://localhost:3306/blog?useSSL=false&serverTimezone=UTC");
        dbcp.setUsername("root");
        dbcp.setPassword("password");
        dbcp.setInitialSize(20);
        dbcp.setMaxActive(10);
        return dbcp;
    }

    /**
     * 1. 此方法是通过编写 dbcpconfig.properties 配置文件, 将Driver, url, name, pwd 参数通过K=V的方式
     *      写在配置文件中
     * 2. 通过new DbcpUtils().getClass().getClassLoader().getResourceAsStream("配置文件"); 将配置文件加载为输入流
     * 3. 通过 Properties实例.load(input)加载配置文件
     * 4. 再通过 BasicDataSourceFactory.createDataSource(properties); 来获取DBCP对象
     * @return
     */
    public static DataSource getDbcpByProperties() throws Exception {
        Properties prop = new Properties();
        InputStream input=  new DataSourceUtils().getClass().getClassLoader().getResourceAsStream("dbcpconfig.properties");
        prop.load(input);
        return  BasicDataSourceFactory.createDataSource(prop);
    }

    /**
     * 1. 此方法需要两个jar包, c3p0-0.9.5.2.jar, mchange-commons-java-0.2.11.jar
     * 2. 此方法是通过获取 ComboPooledDataSource 的无参对象, 通过该对象的setter方法以硬编码的方式指定Driver, url, name, pwd 等
     * @return
     */
    public static DataSource getC3p0BySet() throws PropertyVetoException {
        ComboPooledDataSource c3p0 = new ComboPooledDataSource();
        c3p0.setDriverClass("com.mysql.cj.jdbc.Driver");
        c3p0.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/blog?useSSL=false&serverTimezone=UTC");
        c3p0.setUser("root");
        c3p0.setPassword("password");
        return c3p0;
    }

    /**
     * 1. 此方法需要编写 c3p0-config.xml文件, 指定default的配置, 以及自己的配置
     * 2. 在使用是通过创建有参数的 ComboPooledDatasource 对象, 参数中指定c3p0-config.xml配置的name属性
     * @return
     */
    public static DataSource getC3p0ByXml(){
        return new ComboPooledDataSource("jefxff");
    }
}

Apache Commons DBUtils 工具类使用

Apache DButils

  • Apache DButils 是对Java JDBC的简单封装, 主要好处就是方便返回查询的结果
  • 下载 commons-dbutils-1.7-bin.zip 文件并解压, 主要使用 commons-dbutils-1.7.jar 文件, 包括几个重点类: DbUtils, QueryRunner, ResultSetHandler
  • DbUtils: 打开关闭连接, 提交事务
  • 关键类 QueryRunner : 增删改查方法的基础, 所有的增删改查都必须创建该类的实例, 通过该类的实例来调用query或者update方法来执行增删改查; 其中该实例参数可以是一个DataSource实例, 又DS实例后是自动提交事务
    • updte() : 第一个参数是SQL语句, 第二个参数是Object[] 类型的数组的参数; 有很多重载的方法
    • query() : 有很多重载的方法, 第一个参数是SQL语句, 第二个参数是ResultSetHandler接口的实现类, 不同的实现类代表返回不同的结果形式, 第三个参数是Object[] 类型的数组的参数;
  • ResultSetHandler接口 : 有很多的实现类, 一个实现类对应于一种不同 的查询结果类型

通过 ResultSetHandler 接口的实现类来实现查询

  • ArrayHandler实现类: 返回结果集中的第一行数据, 并用Object[]接收
  • ArrayListhandler实现类: 返回结果集的多行数据, 接收类型是 List<Obkect[]>
  • BeanHandler实现类: 返回结果集的第一行数据, 并将结果集放在Bean里,即对象Student里面. (反射会通过无参构造来创建对象)
  • BeanListHandler实现类: 返回结果集的多行数据,接收类型为 List
  • BeanMapHandler实现列: 返回结果集的多行数据, 接收类型为 Map<>(如果是Oracle数据库, Java中对应的Oracle默认的数值类型 BigDecimal(数字通过 new BigDecimal(int) 方法转换为BigDecimal))
  • MapHandler实现类: 返回一行以Map形式包装的数据
  • MapListHandler实现类: 返回多行以List<\map>形式包装的数据
  • KeyedHandler: 返回多行数据, 并且给每个添加数据添加字段
  • ColumnListHandler : 把结果集的 某一列保存在List中
  • ScalarHandler: 查询单值结果型

Apache DButils 代码示例

package xyz.xmcs.Dao.Impl;

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 xyz.xmcs.Dao.IArticlesDal;
import xyz.xmcs.Entity.Articles;
import xyz.xmcs.Utils.DataSourceUtils;
import java.sql.SQLException;
import java.util.List;

public class ArticlesDaoImpl implements IArticlesDal {
    // 创建QueryRunner对象, 参数传入DataSource实例, 即以配置文件的方式创建的DataSource
    static QueryRunner runner = new QueryRunner(DataSourceUtils.getC3p0ByXml());

    // update的操作都是一样的, 所以提取出来单独作为一个方法处理
    private static boolean checkUpdate(String sql, Object[] params) {
        try {
        	// 增删改的方法通过 QueryRunner实例调用update()方法来执行
        	// 接受两个参数, 第一个参数是sql语句, 第二个参数是sql语句需要的参数
            int count = runner.update(sql, params);
            if(count > 0){
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean addArticles(Articles articles){
        String sql = "insert into articles values(?,?,?,?,?,?,?)";
        Object[] params = {articles.getId(),articles.getAuthor(),articles.getTitle(),articles.getContent(),
            articles.getCreatedate(),articles.getUser_id(),articles.getCategory_id()};
         return checkUpdate(sql, params);
    }

    

    @Override
    public boolean isExist(String title) {
        return queryArticlesByName(title)==null?false:true;
    }


    @Override
    public Articles queryArticlesById(int id) {
        String sql = "select * from articles where id = ?";
        Object[] params = {id};
        try {
        	// 查询的方法通过 QueryRunner实例调用query()方法来执行
        	// 第一个参数是sql语句, 第二个参数是ResultSetHandler实现类, 代表不同的返回值类型, 第三个参数是sql语句的参数
            Articles article = runner.query(sql, new BeanHandler<>(Articles.class), params);
            return article;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }


    @Override
    public List<Articles> queryAllArticles() {
        String sql = "select * from articles";
        try {
        	// 查询的方法通过 QueryRunner实例调用query()方法来执行
        	// 第一个参数是sql语句, 第二个参数是ResultSetHandler实现类, 代表不同的返回值类型, 第三个参数是sql语句的参数
            List<Articles> list = runner.query(sql, new BeanListHandler<>(Articles.class));
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int getTotalArticlesCount() {
        String sql = "select count(1) from articles";
        try {
        	// 查询的方法通过 QueryRunner实例调用query()方法来执行
        	// 第一个参数是sql语句, 第二个参数是ResultSetHandler实现类, 代表不同的返回值类型
        	// ScalarHandler 实现类代表的是返回单值性的查询, 返回的类型是 long 类型的值
            long query = runner.query(sql, new ScalarHandler<>());
            return (int)query;
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }
}

# Java # mysql