数据库连接池-Druid工具类的编写调优及使用示例(超全的注释就怕你看不懂)

1. 数据库连接池

它其实就是一个容器(集合),用来存放数据库连接的容器,当系统初始化完成后,这个容器就会被创建,并且这个容器会申请一些连接对象(申请连接对象的数量可以在配置文件中修改),当用户访问数据库的时候直接从容器中获取连接对象,用户访问完成后在将连接对象放回到容器中,这样就实现了复用(传统的方式是:用的时候创建连接对象,用完后销毁,这样效率不高)

  • 使用数据库连接池的优点是:
    1. 节约资源
    2. 访问数据库的时候更加高效

2. Druid数据库连接池的使用步骤

  1. 导入jar包

  2. 定义配置文件:

    • 配置文件是:*.properties,名称不限可以随意起但是必须以.properties结尾。
  3. 写代码:

    1. 加载配置文件
    2. 获取连接池对象
    3. 获取连接

3. 使用实例(注释非常详细!!!)

1. druid.properties配置文件

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8
username=root
password=itcast
# 初始化的连接数量
initialSize=5
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000

2. Druid连接池工具类-JDBCUtils

/**
* Druid连接池的工具类
 */

public class JDBCUtils {

// 1. 定义一个成员变量 DataSource
private static DataSource dataSource;

static {

    try {
        // 1. 加载配置文件
        Properties properties = new Properties();
        properties.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
        // 2. 获取DataSource
        try {
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

/**
 * 获取连接的方法
 */
public static Connection getConnection() throws SQLException {
    // 从连接池中取一个连接对象
    return dataSource.getConnection();
}


/**
 *  释放资源
 *  执行DML语句的时候需要关闭 statement 和 connection
 * @param statement
 * @param connection
 */
public static void close(Statement statement , Connection connection){
    if(statement != null){
        try {
            statement.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    if(connection != null){
        try {
            connection.close();      // 归还到连接池中
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

}

/**
 * 释放资源
 * 执行DQL语句的时候需要关闭 resultSet statement 和 connection
 * @param resultSet
 * @param statement
 * @param connection
 */
public static void close(ResultSet resultSet,Statement statement , Connection connection){
    if(resultSet != null){
        try {
            resultSet.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

/*        if(statement != null){
        try {
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    if(connection != null){
        try {
            connection.close();      // 归还到连接池中
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }*/
	// 我们发现上面两个关闭的过程和DML关闭的过程一样,所以为了代码的简洁可以直接调用方法关闭
    close(statement,connection);
}

/**
 * 获取连接池的方法
 */
public static DataSource getDataSource(){
    return dataSource;
}
}

3. Druid连接池工具类使用示例

/**
 * 使用新的工具类
 */

public class DruidUtilsDemo {
	public static void main(String[] args) {
    /**
     * 完成添加的操作 给 accout 表添加一条记录
     */
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    try {
        // 1. 获取连接
        connection = JDBCUtils.getConnection();
        // 2. 定义sql
        String sql = "insert into account value(null,?,?)";
        // 3. 获取
        preparedStatement = connection.prepareStatement(sql);
        // 4. 给?赋值
        preparedStatement.setString(1,"小白白");
        preparedStatement.setDouble(2,3000);
		// 执行sql,返回值是影响的行数
        int count = preparedStatement.executeUpdate();
        System.out.println(count);
    } catch (SQLException e) {
        e.printStackTrace();
    }finally {
        // 6. 释放资源
        JDBCUtils.close(preparedStatement,connection);
    }
	}
}

如果看不太明白的话,建议参考我的另一篇博客–完整的JDBCUtils和登录案例,以及解决SQL注入问题。最基础的JDBC工具类的编写,引导你自主思考,自主完成JDBCUtils类的编写。

猜你喜欢

转载自blog.csdn.net/qq_45796486/article/details/114640043