当前位置:网站首页>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
边栏推荐
- Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising
- Contrôle automatique (version Han min)
- umi官网yarn create @umijs/umi-app 报错:文件名、目录名或卷标语法不正确
- Pyqy5 learning (III): qlineedit + qtextedit
- You cannot access this shared folder because your organization's security policy prevents unauthenticated guests from accessing it
- EditorConfig
- Shansi Valley P290 polymorphism exercise
- Pyqy5 learning (2): qmainwindow + QWidget + qlabel
- Pytorch learning record (XI): data enhancement, torchvision Explanation of various functions of transforms
- LDCT图像重建论文——Eformer: Edge Enhancement based Transformer for Medical Image Denoising
猜你喜欢
Graphic numpy array matrix
对比学习论文——[MoCo,CVPR2020]Momentum Contrast for Unsupervised Visual Representation Learning
线性代数第一章-行列式
PyTorch笔记——观察DataLoader&用torch构建LeNet处理CIFAR-10完整代码
解决报错:ImportError: IProgress not found. Please update jupyter and ipywidgets
去噪论文阅读——[CVPR2022]Blind2Unblind: Self-Supervised Image Denoising with Visible Blind Spots
PreparedStatement防止SQL注入
Pytoch learning record (x): data preprocessing + batch normalization (BN)
Linear algebra Chapter 1 - determinant
Pytorch学习记录(五):反向传播+基于梯度的优化器(SGD,Adagrad,RMSporp,Adam)
随机推荐
Programming record - picture rotation function SciPy ndimage. Simple use and effect observation of rotate()
Contrôle automatique (version Han min)
PyQy5学习(三):QLineEdit+QTextEdit
编写一个自己的 RedisTemplate
Pytorch——数据加载和处理
ValueError: loaded state dict contains a parameter group that doesn‘t match the size of optimizer‘s
Pytorch学习记录(九):Pytorch中卷积神经网络
无监督去噪——[TMI2022]ISCL: Interdependent Self-Cooperative Learning for Unpaired Image Denoising
Reading of denoising paper - [ridnet, iccv19] real image denoising with feature attention
治療TensorFlow後遺症——簡單例子記錄torch.utils.data.dataset.Dataset重寫時的圖片維度問題
Anaconda installed pyqt5 and pyqt5 tools without designer Exe problem solving
rsync实现文件服务器备份
Manually delete registered services on Eureka
创建二叉树
字符串(String)笔记
Pytorch学习记录(十二):学习率衰减+正则化
Font shape `OMX/cmex/m/n‘ in size <10.53937> not available (Font) size <10.95> substituted.
Postfix变成垃圾邮件中转站后的补救
Use Matplotlib. In Jupiter notebook Pyplot server hangs up and crashes
Get the value of state in effects in DVA