当前位置:网站首页>C3P0数据库连接池使用
C3P0数据库连接池使用
2022-04-23 05:41:00 【hanyc..】
配置文件:(注意:c3p0的xml配置文件名必须为 c3p0-config ,否则会报空指针)
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!--
c3p0的缺省(默认)配置
如果在代码中ComboPooledDataSource ds=new ComboPooledDataSource();这样写就表示使用的是c3p0的缺省(默认)
-->
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="acquiredIncrement">5</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">5</property>
<property name="maxPoolSize">20</property>
</default-config>
<!--
c3p0的命名配置
如果在代码中ComboPooledDataSource ds=new ComboPooledDataSource("MySQL");这样写就表示使用的是name是MySQL
-->
<name-config name="MySQL">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="acquiredIncrement">5</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">5</property>
<property name="maxPoolSize">20</property>
</name-config>
</c3p0-config>
工具类提取:
package com.hyc.study06.utils;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class C3P0Utils {
public static DataSource dataSource = null;
static {
try {
//代码的方式配置
// dataSource = new ComboPooledDataSource();
// dataSource.setDriverClass();
// dataSource.setJdbcUrl();
// dataSource.setUser();
// dataSource.setPassword();
// dataSource.setMaxPoolSize();
// dataSource.setMinPoolSize();
//配置文件写法(XML文件不用读取,加载的时候会自动匹配)
dataSource = new ComboPooledDataSource("MySQL");
} catch (Exception e) {
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection() throws SQLException {
//从数据源中获取连接
return dataSource.getConnection();//与DBCP都是实现了DataSource接口
}
//释放连接
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();
}
}
}
}
测试:
package com.hyc.study06;
import com.hyc.study06.utils.C3P0Utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
public class TestC3P0 {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//3、获取数据库连接
connection = C3P0Utils.getConnection();//原来是自己实现的,现在是用别人实现的
//使用?占位符替代参数
String sql = "INSERT INTO `users`(`id`,`NAME`,`PASSWORD`,`email`,`birthday`) " +
"VALUES (?,?,?,?,?);";
//4、获取执行sql的对象
//预编译sql,先写sql,然后不执行
preparedStatement = connection.prepareStatement(sql);
//手动给参数赋值
preparedStatement.setInt(1, 5);
preparedStatement.setString(2, "hhh");
preparedStatement.setString(3, "1135656");
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、释放连接
C3P0Utils.release(connection, preparedStatement, resultSet);
}
}
}
版权声明
本文为[hanyc..]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42732184/article/details/124280972
边栏推荐
- Flutter 新一代圖形渲染器 Impeller
- [machine learning] scikit learn introduction
- Navicate连接oracle(11g)时ORA:28547 Connection to server failed probable Oeacle Net admin error
- Frequently asked interview questions - 2 (computer network)
- Dwsurvey is an open source questionnaire system. Solve the problem that cannot be run and modify the bug.
- Breadth first search topics (BFS)
- Shell instruction learning 1
- SQL statement simple optimization
- refused connection
- Xiuxian real world and game world
猜你喜欢
多线程与高并发(1)——线程的基本知识(实现,常用方法,状态)
Pilotage growth · ingenuity empowerment -- yonmaster developer training and pilotage plan is fully launched
Breadth first search topics (BFS)
C language - Spoof shutdown applet
Transposed convolution
‘EddiesObservations‘ object has no attribute ‘filled‘
软件架构设计——软件架构风格
Differences between sea level anatomy and sea surface height anatomy
Hongji | how does HR carry out self change and organizational change in the digital era?
Fletter next generation graphics renderer impaller
随机推荐
Establish excel bookkeeping book through setting context menu
Summary of redis classic interview questions 2022
io.lettuce.core.RedisCommandExecutionException: ERR wrong number of arguments for ‘auth‘ command
xxl-job采坑指南xxl-rpc remoting error(connect timed out)
Xiuxian real world and game world
Reading notes of modern methods of C language programming
Deep learning object detection
opensips(1)——安装opensips详细流程
Parameter analysis of open3d material setting
Pytorch deep learning practice_ 11 convolutional neural network
Date增加天数
The role of brackets' [] 'in the parameter writing method in MDN documents
引航成长·匠心赋能——YonMaster开发者培训领航计划全面开启
MySQL的锁机制
World and personal development
MySQL transaction
Insert picture in freemark
Common protocols of OSI layer
STL function library
Character recognition easyocr