当前位置:网站首页>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
边栏推荐
- STD:: String implements split
- 第36期《AtCoder Beginner Contest 248 打比赛总结》
- Add days to date
- Pytorch deep learning practice_ 11 convolutional neural network
- poi导出excel,行相同数据自动合并单元格
- windows连接mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)
- Map对象 map.get(key)
- MySQL triggers, stored procedures, stored functions
- acwing854. Floyd finds the shortest path
- freemark中插入图片
猜你喜欢

Ora: 28547 connection to server failed probable Oracle net admin error

第36期《AtCoder Beginner Contest 248 打比赛总结》

手动删除eureka上已经注册的服务

Data mining -- understanding data

jdbc入门\获取数据库连接\使用PreparedStatement

JVM系列(4)——内存溢出(OOM)

2-軟件設計原則

C语言——恶搞关机小程序

SQL statement simple optimization

Breadth first search topics (BFS)
随机推荐
Golang通过exec模块实现Ping连通性检测案例
Frequently asked interview questions - 3 (operating system)
Shell instruction learning 1
手动删除eureka上已经注册的服务
poi生成excel,插入图片
Use of qwbengneview and qwebchannel.
MySQL transaction
Dwsurvey is an open source questionnaire system. Solve the problem that cannot be run and modify the bug.
windows连接mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)
After adding qmenu to qtoolbutton and QPushButton, remove the triangle icon in the lower right corner
Navicate连接oracle(11g)时ORA:28547 Connection to server failed probable Oeacle Net admin error
The list attribute in the entity is empty or null, and is set to an empty array
SQL statement simple optimization
Radar equipment (greedy)
Relative reference and absolute reference of Excel
mysql如何将存储的秒转换为日期
Issue 36 summary of atcoder beginer contest 248
The QT debug version runs normally and the release version runs crash
Sea Level Anomaly 和 Sea Surface Height Anomaly 的区别
MySQL创建oracle练习表