JDBC-- 连接池
添加jar包 druid.jar
准备工作
- 新建Java项目;
- 在项目下新建lib目录;
- 将MySQL驱动jar包和Druid jar包拷贝到lib目录下;
- 选中lib目录右键Add as Library–单击OK。
- 关键词: DruidDataSource datasource = new DruidDataSource();
连接池的创建和使用
package com.sdut;
import com.alibaba.druid.pool.DruidDataSource;
import java.sql.Connection;
import java.sql.SQLException;
/**
* 创建连接池,使用连接池
*/
public class MyTest1 {
public static void main(String[] args) throws SQLException {
//创建连接池
DruidDataSource dataSource = new DruidDataSource();
//设置连接池参数
//设置四大参数
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/mydbjdbc?useSSL=false");
dataSource.setUsername("root");;
dataSource.setPassword("1234");
dataSource.setInitialSize(10);
dataSource.setMaxActive(100);
dataSource.setMinIdle(5);
//通过连接池获取连接
Connection conn = dataSource.getConnection();
System.out.println(conn);
//关闭连接-- 将连接归还到连接池
conn.close();
}
}
工具类的升级 — 使用连接池
步骤
1.配置JdbcUtil类
package com.sdut.util;
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 JdbcUtil {
//使用连接池来保存四大参数
private static DataSource dataSource;
static {
try {
//读取配置文件到输入流
InputStream in = JdbcUtil.class.getResourceAsStream("/jdbc.properties");
Properties properties = new Properties();
//加载配置文件中的数据到Properties中
properties.load(in);
//创建连接池
dataSource = DruidDataSourceFactory.createDataSource(properties);//配置文件的内容key键是固定的一点不能改!!
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection() {
Connection connection = null;
try {
connection = dataSource.getConnection();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return connection;
}
//释放资源
public static void close(Connection connection, Statement statement, ResultSet rSet) {
try {
if(rSet != null) {
rSet.close();
}
if(statement != null) {
statement.close();
}
if(connection != null) {
connection.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
2.配置配置文件 注意:配置文件的格式固定 前面的key值不能修改!!!
#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydbjdbc?useSSL=false
username=root
password=1234
#初始化连接数量
initialSize=10
#最大连接数量
maxActive=50
#最小空闲连接
minIdle=5
3.使用连接池
package com.sdut;
import com.sdut.util.JdbcUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class MyTest {
public static void main(String[] args) throws Exception {
String sql = " select * from `tb_stu` where `sgender` =?";
//获取连接
Connection conn = JdbcUtil.getConnection();
//获取prepareStatement
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, "male");
//发送SQL语句
ResultSet rset = statement.executeQuery();
//处理结果
while(rset.next()) {
int sid = rset.getInt("sid");
String sname = rset.getString("sname");
int sage = rset.getInt("sage");
String sgender = rset.getString("sgender");
System.out.println(sid + "---" + sname + "---" + sage + "---" + sgender);
}
//释放资源
JdbcUtil.close(conn, statement, rset);
}
}