当前位置:网站首页>C3p0 database connection pool usage
C3p0 database connection pool usage
2022-04-23 06:01:00 【hanyc..】
The configuration file :( Be careful :c3p0 Of xml The profile name must be c3p0-config , Otherwise, null pointer will be reported )
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!--
c3p0 Default ( Default ) To configure
If in code ComboPooledDataSource ds=new ComboPooledDataSource(); This means that you are using c3p0 Default ( Default )
-->
<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 Named configuration for
If in code ComboPooledDataSource ds=new ComboPooledDataSource("MySQL"); This means that you are using name yes 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>
Tool class extraction :
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 {
// Code configuration
// dataSource = new ComboPooledDataSource();
// dataSource.setDriverClass();
// dataSource.setJdbcUrl();
// dataSource.setUser();
// dataSource.setPassword();
// dataSource.setMaxPoolSize();
// dataSource.setMinPoolSize();
// Configuration file writing (XML The file does not need to be read , It will match automatically when loading )
dataSource = new ComboPooledDataSource("MySQL");
} catch (Exception e) {
e.printStackTrace();
}
}
// Get the connection
public static Connection getConnection() throws SQLException {
// Get the connection from the data source
return dataSource.getConnection();// And DBCP It's all implemented DataSource Interface
}
// Release the connection
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();
}
}
}
}
test :
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、 Get database connection
connection = C3P0Utils.getConnection();// It was realized by myself , Now it is realized by others
// Use ? Placeholders replace parameters
String sql = "INSERT INTO `users`(`id`,`NAME`,`PASSWORD`,`email`,`birthday`) " +
"VALUES (?,?,?,?,?);";
//4、 Access to perform sql The object of
// precompile sql, First write sql, Then don't execute
preparedStatement = connection.prepareStatement(sql);
// Assign parameters manually
preparedStatement.setInt(1, 5);
preparedStatement.setString(2, "hhh");
preparedStatement.setString(3, "1135656");
preparedStatement.setString(4, "[email protected]");
//java.sql.Date It's database Date , Only the date information is included , It is java.util.Date( Contains the information of month, day, hour, minute and second ) Subclasses of
preparedStatement.setDate(5, new java.sql.Date(new Date().getTime()));
//5、 perform sql sentence
//6、 Return execution result set
int num = preparedStatement.executeUpdate();
if (num > 0) {
System.out.println(" Insert data succeeded !");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//7、 Release the connection
C3P0Utils.release(connection, preparedStatement, resultSet);
}
}
}
版权声明
本文为[hanyc..]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230541159432.html
边栏推荐
- Manually delete registered services on Eureka
- Configure domestic image accelerator for yarn
- Viewer: introduce MySQL date function
- Postfix变成垃圾邮件中转站后的补救
- Pytorch学习记录(九):Pytorch中卷积神经网络
- delete和truncate
- A general U-shaped transformer for image restoration
- Pytorch学习记录(三):神经网络的结构+使用Sequential、Module定义模型
- Programming record - picture rotation function SciPy ndimage. Simple use and effect observation of rotate()
- 编程记录——图片旋转函数scipy.ndimage.rotate()的简单使用和效果观察
猜你喜欢

PyQt5学习(一):布局管理+信号和槽关联+菜单栏与工具栏+打包资源包

Paper on LDCT image reconstruction: edge enhancement based transformer for medical image denoising

数字图像处理基础(冈萨雷斯)二:灰度变换与空间滤波

Latex快速入门

Pytorch学习记录(十一):数据增强、torchvision.transforms各函数讲解
![Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots](/img/fd/84df62c88fe90a73294886642036a0.png)
Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots

Chapter 4 of line generation - linear correlation of vector systems

Pytorch Learning record (XIII): Recurrent Neural Network

Pytorch学习记录(七):处理数据和训练模型的技巧

域内用户访问域外samba服务器用户名密码错误
随机推荐
Viewer: introduce MySQL date function
JSP syntax and JSTL tag
Numpy common function table sorting of data processing
interviewter:介绍一下MySQL日期函数
Pytorch learning record (7): skills in processing data and training models
PyQy5学习(三):QLineEdit+QTextEdit
Traitement des séquelles du flux de Tensor - exemple simple d'enregistrement de torche. Utils. Données. Dataset. Problème de dimension de l'image lors de la réécriture de l'ensemble de données
金蝶EAS“总账”系统召唤“反过账”按钮
事实最终变量与最终变量
Pytorch学习记录(五):反向传播+基于梯度的优化器(SGD,Adagrad,RMSporp,Adam)
Pytorch learning record (IV): parameter initialization
建表到页面完整实例演示—联表查询
Complete example demonstration of creating table to page - joint table query
对比学习论文——[MoCo,CVPR2020]Momentum Contrast for Unsupervised Visual Representation Learning
Latex quick start
Conda 虚拟环境管理(创建、删除、克隆、重命名、导出和导入)
实操—Nacos安装与配置
Paper on LDCT image reconstruction: edge enhancement based transformer for medical image denoising
Pytorch notes - observe dataloader & build lenet with torch to process cifar-10 complete code
Pytorch学习记录(三):神经网络的结构+使用Sequential、Module定义模型