package com.softeem.day5.utils;
import com.alibaba.druid.pool.DruidDataSource;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.*;
/**
* jdbc、连接池技术、泛型、反射、可变数组参数,静态块,新特性,面向对象
*
* @author mrchai
* @date 2022/8/5 10:39
*/
public class DBUtils {
/**
* 声明连接数据库必须的字符串常量
*/
public static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
public static final String URL = "jdbc:mysql://127.0.0.1:3306/j2207_exp";
public static final String USER = "root";
public static final String PASSWORD = "123456";
/**连接池相关参数配置 初始连接数,最大活动连接数,最长等待时间*/
public static final int INIT_SIZE = 10;
public static final int MAX_SIZE = 20;
public static final long MAX_WAIT = 10000L;
/**声明Druid数据源*/
public static DruidDataSource dataSource;
static {
try {
//创建数据源并执行初始化操作
dataSource = new DruidDataSource();
dataSource.setDriverClassName(DRIVER_CLASS);
dataSource.setUrl(URL);
dataSource.setUsername(USER);
dataSource.setPassword(PASSWORD);
dataSource.setInitialSize(INIT_SIZE);
dataSource.setMaxActive(MAX_SIZE);
dataSource.setMaxWait(MAX_WAIT);
//执行初始化
dataSource.init();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
/**
* 获取连接
*
* @return
*/
public static Connection getConn() {
try {
//使用数据源获取标准连接对象
return dataSource.getConnection();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return null;
}
/**
* 关闭资源
*
* @param rs
* @param stat
* @param conn
*/
public static void close(ResultSet rs, Statement stat, Connection conn) {
try {
if (Objects.nonNull(rs)) {
rs.close();
}
if (Objects.nonNull(stat