当前位置:网站首页>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
边栏推荐
- College entrance examination volunteer filling reference
- catkin_package到底干了什么
- AcWing 1096. Detailed notes of Dungeon Master (3D BFS) code
- MySQL事务
- Pytorch deep learning practice_ 11 convolutional neural network
- Deconstruction function of ES6
- Typescript interface & type rough understanding
- The 8th Blue Bridge Cup 2017 - frog jumping cup
- Range of numbers (dichotomous classic template topic)
- Getting started with JDBC \ getting a database connection \ using Preparedstatement
猜你喜欢
SQL statement simple optimization
Find the number of "blocks" in the matrix (BFS)
Getting started with JDBC \ getting a database connection \ using Preparedstatement
mysql中duplicate key update
橙单微服务之批量导入
Hongji | how does HR carry out self change and organizational change in the digital era?
QT displays the specified position and size of the picture
Issue 36 summary of atcoder beginer contest 248
手动删除eureka上已经注册的服务
Ora: 28547 connection to server failed probable Oracle net admin error
随机推荐
【华为机试】考试得分总数(如何处理答错的情况?回溯一次,代表答错一题)
Range of numbers (dichotomous classic template topic)
QSslSocket::connectToHostEncrypted: TLS initialization failed
基于ssm 包包商城系统
Navicate连接oracle(11g)时ORA:28547 Connection to server failed probable Oeacle Net admin error
数据安全入门产品——数据库审计系统详解
Cross platform packaging of QT packaging program
Ora: 28547 connection to server failed probable Oracle net admin error
Mysql 查询使用\G,列转行
The role of brackets' [] 'in the parameter writing method in MDN documents
2.devops-sonar安装
MySQL query uses \ g, column to row
JVM系列(4)——内存溢出(OOM)
windows连接mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)
Introduction to data security -- detailed explanation of database audit system
POI generates excel and inserts pictures
提升独立站转化率攻略 | 挽回弃购用户
No.1.#_ 6 Navicat shortcuts
引航成长·匠心赋能——YonMaster开发者培训领航计划全面开启
Sea Level Anomaly 和 Sea Surface Height Anomaly 的区别