阿里Druid数据库连接工具类

这里写自定义目录标题

    • 1.依赖导入
    • 2.druid配置文件:
    • 3.数据库连接工具类

本处使用了阿里的连接池

1.依赖导入

    <!--mysql驱动-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.20</version>
    </dependency>
    <!--阿里连接池-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.23</version>
    </dependency>
    <!--日志依赖-->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.2</version>
    </dependency>

2.druid配置文件:

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://112.125.89.39/xiaomi?useSSL=false&characterEnding=utf8&serverTimezone=Asia/Shanghai
username=aa123456
password=123456
initialSize=10
maxActive=10
maxWait=60000
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
time-between-eviction-runs-millis=60000
#druid recycle
druid.removeAbandoned=true
druid.removeAbandonedTimeout=1000
druid.logAbandoned=false

3.数据库连接工具类

package utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @Description:  连接池工具类
 * @Author one world
 * @Date 2020/9/19 0012 11:02
 */
public class ConnUtil {
     
    private static InputStream in;
    private static DataSource dataSource;
    private static final Logger logger = LoggerFactory.getLogger(ConnUtil.class);

    static {
     
        /**
         * 加载配置文件,用阿里的工厂创建数据源
         */
        in = ConnUtil.class.getResourceAsStream("/druid.properties");
        Properties properties = new Properties();
        try {
     
            properties.load(in);
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
     
         throw new RuntimeException("连接工具类异常");
           logger.error("连接工具类异常",e);
        } catch (Exception e) {
     
        throw new RuntimeException("连接工具类异常");
            logger.error("连接工具类异常",e);
        }
    }

    /**
     * 数据库连接的获取
     *
     * @return Connection
     */
    public static Connection getConnection() {
     
        try {
     
            return dataSource.getConnection();
        } catch (SQLException e) {
     
           throw new RuntimeException("连接工具类异常");
            logger.error("连接工具类getConnection方法异常",e);
        }
        return null;
    }

    /**
     * 数据源获取
     *
     * @return
     */
    public static DataSource getDataSource() {
     
        return dataSource;
    }

    /**
     * 资源关闭
     *
     * @param conn
     * @param statement
     * @param rs
     */
    public static void close(Connection conn, Statement statement, ResultSet rs) {
     
        if (rs != null) {
     
            try {
     
                rs.close();
            } catch (SQLException e) {
     
            throw new RuntimeException("连接工具类异常");
                logger.error("连接工具类关闭结果集异常",e);
            }
        }
        if (statement != null) {
     
            try {
     
                statement.close();
            } catch (SQLException e) {
     
            throw new RuntimeException("连接工具类异常");
                logger.error("连接工具类关闭statement异常",e);
            }
        }
        if (conn != null) {
     
            try {
     
                conn.close();
            } catch (SQLException e) {
     
            throw new RuntimeException("连接工具类异常");
                logger.error("连接工具类关闭连接异常",e);
            }
        }
    }

}

你可能感兴趣的:(数据库,jdbc,mysql,数据库)