DBCP思维导图
DBCP
数据库连接池(Database Connection Pool)
1、为什么使用DBCP?
如果没有连接池,每一次业务都需要和数据库服务器建立一次连接,业务处理完断开连接。
如果有上万次业务就会有上万次的开关连接,频繁开关连接非常浪费资源。
使用数据库连接池,可以设置几个初始连接,如果有业务需要使用连接,则从连接池中直接获取;
如果连接池连接用光,则会等待连接归还后再获取连接。
2、如何使用DBCP
- 下载jar包
- 登录 maven.aliyun.com
- 查找commons-dbcp-1.4.jar
- 复制粘贴
<dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency>
- 使用步骤
- 创建数据源对象
BasicDataSource dataSource = new BasicDataSource(); - 设置数据库连接信息
dataSource.setDriverClassName(“com.mysql.jdbc.Driver”);
dataSource.setUrl(“jdbc:mysql://localhost:3306/db”);
dataSource.setUsername(“root”);
dataSource.setPassword(“123456”); - 设置策略信息
dataSource.setInitialSize(3); // 初始连接数量
dataSource.setMaxActive(5); // 最大连接数量 - 获取连接对象
Connection conn = dataSource.getConnection();
- 创建数据源对象
public class DBUnits {
private static BasicDataSource dataSource;
static{
// 读取配置文件信息
Properties prop = new Properties();
// 获取文件输入流
InputStream is = DBUnits.class.getClassLoader().getResourceAsStream("jdbc.properties");
try {
// 把文件加载到prop对象中
prop.load(is);
// 读取连接数据库信息
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
String initSize = prop.getProperty("initSize");
String maxSize = prop.getProperty("maxSize");
// 创建数据源对象
dataSource = new BasicDataSource();
// 设置连接数据库信息
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
// 设置策略信息
dataSource.setInitialSize(Integer.parseInt(initSize));
dataSource.setMaxActive(Integer.parseInt(maxSize));
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConn() throws SQLException{
return dataSource.getConnection();
}
public static void close(ResultSet rs,Statement stat,Connection conn){
try {
if(rs!=null){
rs.close();
}
if(stat!=null){
stat.close();
}
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
3、PreparedStatement 预编译sql执行对象
- 好处
- 代码可读性更高,不容易出错(因为statement拼接字符串又麻烦又易出错)
- 带有预编译效果,执行效率比state略高
- 可以避免sql注入
- sql中有变量就用PreparedStatement 没有变量就用Statement
- 因为预编译时,sql语句的逻辑已经固定,替换?进去的内容只能以值的形式体现,如果包含逻辑sql内容,则无效。
- 使用方式
- sql语句 “insert into user values(null,?,?)”
- 把?替换成真正的值
- 执行sql
public class TestStatement {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入姓名:");
String name = scan.nextLine();
System.out.println("请输入年龄:");
int age = scan.nextInt();
Connection conn = null;
PreparedStatement stat = null;
ResultSet rs = null;
try {
// 获取连接对象
conn = DBUnits.getConn();
// 创建预编译sql执行对象
String sql = "insert into user values(null,?,?)";
stat = conn.prepareStatement(sql);
// 把?替换成真正的值
stat.setString(1, name);
stat.setInt(2, age);
// 执行sql
stat.executeUpdate();
System.out.println("插入成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
DBUnits.close(rs, stat, conn);
}
}
}
来源:CSDN
作者:程序员阿文
链接:https://blog.csdn.net/awen6666/article/details/103619602