Druid工具类

4 篇文章 0 订阅
2 篇文章 0 订阅

基本要求

  • 提供一个静态代码块来加载配置文件,初始化连接池对象
  • 获取连接的方法:通过数据库连接池获取连接
  • 释放资源
  • 获取连接池的方法

配置文件

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/city
username=root
password=root
# 初始化连接数
initialSize=6
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000
minIdle=3

工具类代码


import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DruidUtils {

    private static DataSource dataSource;

    static {
        Properties properties = new Properties();
        ClassLoader classLoader = DruidUtils.class.getClassLoader();
        InputStream inputStream = classLoader.getResourceAsStream("druid.properties");

        try {
            properties.load(inputStream);
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static DataSource getDataSource() {
        return dataSource;
    }

    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
        if (resultSet != null) {
            close(resultSet);
        }
        if (preparedStatement != null) {
            close(preparedStatement);
        }
        if (connection != null) {
            close(connection);
        }
    }

    public static void close(Connection connection, Statement statement) {
        if (statement != null) {
            close(statement);
        }
        if (connection != null) {
            close(connection);
        }
    }

    public static void close(Connection connection, Statement statement, ResultSet resultSet) {
        if (resultSet != null) {
            close(resultSet);
        }
        if (connection != null) {
            close(connection);
        }
        if (statement != null) {
            close(statement);
        }
    }

    public static void close(Statement statement) {
        try {
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void close(Connection connection) {
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void close(PreparedStatement preparedStatement) {
        try {
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void close(ResultSet resultSet) {
        try {
            resultSet.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

工具类测试

package jdbc;

import druid.DruidUtils;
import jdbcutils.JDBCUtil;

import java.sql.*;
import java.util.Scanner;

public class JDBCDemo {
    public static void main(String[] args) {
        Connection connection = DruidUtils.getConnection();
        String sql = "SELECT * FROM users";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();

            while(resultSet.next()) {
                String name = resultSet.getString("name");
                String password = resultSet.getString("password");
                System.out.println(name + "  " + password);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        DruidUtils.close(connection, preparedStatement, resultSet);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值