当前位置:网站首页>DBCP使用
DBCP使用
2022-04-23 05:41:00 【hanyc..】
配置文件:
#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true
username=root
password=123456
#初始化连接
initialSize=10
#最大连接数量
maxActive=50
#最大空闲连接
maxIdle=20
#最小空闲连接
minIdle=5
#超时等待时间以毫秒为单位 6000毫秒/1000等于60秒
maxWait=60000
#JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:【属性名=property;】
#注意:user 与 password 两个属性会被明确地传递,因此这里不需要包含他们。
connectionProperties=useUnicode=true;characterEncoding=UTF8
#指定由连接池所创建的连接的自动提交(auto-commit)状态。
defaultAutoCommit=true
#driver default 指定由连接池所创建的连接的只读(read-only)状态。
#如果没有设置该值,则“setReadOnly”方法将不被调用。(某些驱动并不支持只读模式,如:Informix)
defaultReadOnly=
#driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。
#可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation=READ_COMMITTED
工具类提取:
package com.hyc.study05.utils;
import com.hyc.study02.utils.JDBCUtils;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class DBCPUtils {
public static DataSource dataSource = null;
static {
try {
InputStream inputStream = JDBCUtils.class.getClassLoader().getResourceAsStream("dbcp.properties");
Properties properties = new Properties();
properties.load(inputStream);
//创建数据源 工厂模式-->创建
dataSource = BasicDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();//从数据源中获取连接
}
//释放连接
public static void release(Connection connection, Statement statement, ResultSet resultSet) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
主要是在获取数据库连接方面与单纯使用JDBC有点区别,使用数据库连接池之后,就不需要编写连接数据库的代码。(连接池主要是实现了DataSource接口)
测试:
package com.hyc.study05;
import com.hyc.study05.utils.DBCPUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
public class TestDBCP {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//3、获取数据库连接
connection = DBCPUtils.getConnection();
//使用?占位符替代参数
String sql = "INSERT INTO `users`(`id`,`NAME`,`PASSWORD`,`email`,`birthday`) " +
"VALUES (?,?,?,?,?);";
//4、获取执行sql的对象
//预编译sql,先写sql,然后不执行
preparedStatement = connection.prepareStatement(sql);
//手动给参数赋值
preparedStatement.setInt(1, 4);
preparedStatement.setString(2, "hyc");
preparedStatement.setString(3, "123456");
preparedStatement.setString(4, "[email protected]");
//java.sql.Date 是数据库的Date ,只包含年月日信息 ,它是java.util.Date(包含年月日和时分秒信息)的子类
preparedStatement.setDate(5, new java.sql.Date(new Date().getTime()));
//5、执行sql语句
//6、返回执行结果集
int num = preparedStatement.executeUpdate();
if (num > 0) {
System.out.println("插入数据成功!");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//7、释放连接
DBCPUtils.release(connection, preparedStatement, resultSet);
}
}
}
版权声明
本文为[hanyc..]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42732184/article/details/124279932
边栏推荐
- QT compressed folder
- Flutter 新一代图形渲染器 Impeller
- poi生成excel,插入图片
- Common protocols of OSI layer
- 热键,界面可视化配置(界面交互)
- After adding qmenu to qtoolbutton and QPushButton, remove the triangle icon in the lower right corner
- Usage and difference of shellexecute, shellexecuteex and winexec in QT
- 多线程与高并发(3)——synchronized原理
- Shell instruction learning 1
- Utf8 to STD: string and STD: string to utf8
猜你喜欢
多线程与高并发(2)——synchronized用法详解
Jiugong magic square - the 8th Lanqiao provincial competition - group C (DFS and comparison of all magic square types)
Parameter analysis of open3d material setting
OSI层常用协议
Ora: 28547 connection to server failed probable Oracle net admin error
Flutter 新一代图形渲染器 Impeller
Isosceles triangle - the 9th Lanqiao provincial competition - group C
‘EddiesObservations‘ object has no attribute ‘filled‘
转置卷积(Transposed Convolution)
QT displays the specified position and size of the picture
随机推荐
Deep learning object detection
js数字大写方法
多线程与高并发(2)——synchronized用法详解
qt. qpa. plugin: Could not find the Qt platform plugin “xcb“ in ““
Pytorch deep learning practice_ 11 convolutional neural network
Cmake basic tutorial (39) pkgconfig
尚硅谷 p290 多态性练习
引航成长·匠心赋能——YonMaster开发者培训领航计划全面开启
Linear sieve method (prime sieve)
50 SQL exercises, answers and detailed analysis
Map对象 map.get(key)
MySQL lock mechanism
Hotkeys, interface visualization configuration (interface interaction)
‘EddiesObservations‘ object has no attribute ‘filled‘
solidity合约DOS攻击
Relative reference and absolute reference of Excel
软件架构设计——软件架构风格
MySQL triggers, stored procedures, stored functions
Find the number of "blocks" in the matrix (BFS)
catkin_package到底干了什么