基本要求
- 提供一个静态代码块来加载配置文件,初始化连接池对象
- 获取连接的方法:通过数据库连接池获取连接
- 释放资源
- 获取连接池的方法
配置文件
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);
}
}