Druid 数据库连接池的简单使用:
1.引入相关jar包:
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
2.编写druid.properties配置文件
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/xxx?useUnicode=true&characterEncoding=utf-8
username = xxx
password = xxx
3.测试
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;
public class TestDruid {
public static void main(String[] args) throws Exception {
// 1.加载配置文件
Properties properties = new Properties();
InputStream resourceAsStream = TestDruid.class.getClassLoader().getResourceAsStream("druid.properties");
properties.load(resourceAsStream);
// 2.获取连接池对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
// 3.获取连接
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
}
4.测试成功结果
5.编写工具类
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* Druid连接池工具类
*/
public class DruidUtils {
private static DataSource dataSource;
static {
try {
// 1.加载配置文件
Properties properties = new Properties();
properties.load(DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
// 2.获取连接池对象
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取连接
*/
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
/**
* 释放资源
*/
public static void close(Statement statement, Connection connection){
// 直接调用下面那个重载方法来简化代码
close(null,statement,connection);
}
/**
* 重载close()
*
* @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();
}
}
}
/**
* 获取连接池
*/
public static DataSource getDataSource(){
return dataSource;
}
}
6.测试工具类
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 查询user表中所有记录
*/
public class DruidDemo {
public static void main(String[] args){
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
// 1.获取连接
connection = DruidUtils.getConnection();
// 2.定义SQL
String sql = "select * from user";
// 3.获取preparedStatement对象
preparedStatement = connection.prepareStatement(sql);
// 4.执行sql
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
System.out.println(resultSet.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
// 5.释放资源
DruidUtils.close(preparedStatement,connection);
}
}
}
7.测试结果