当前位置:网站首页>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
边栏推荐
猜你喜欢

编程记录——图片旋转函数scipy.ndimage.rotate()的简单使用和效果观察

Anaconda安装PyQt5 和 pyqt5-tools后没有出现designer.exe的问题解决
![Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi](/img/1b/4eea05e2634780f45b44273d2764e3.png)
Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi

Pytorch learning record (XI): data enhancement, torchvision Explanation of various functions of transforms
![Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising](/img/cd/10793445e6867eeee613b6ba4b85cf.png)
Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising

Manually delete registered services on Eureka

Pytorch learning record (XII): learning rate attenuation + regularization

On traversal of binary tree
![无监督去噪——[TMI2022]ISCL: Interdependent Self-Cooperative Learning for Unpaired Image Denoising](/img/cd/10793445e6867eeee613b6ba4b85cf.png)
无监督去噪——[TMI2022]ISCL: Interdependent Self-Cooperative Learning for Unpaired Image Denoising
![去噪论文阅读——[CVPR2022]Blind2Unblind: Self-Supervised Image Denoising with Visible Blind Spots](/img/fd/84df62c88fe90a73294886642036a0.png)
去噪论文阅读——[CVPR2022]Blind2Unblind: Self-Supervised Image Denoising with Visible Blind Spots
随机推荐
Pytorch learning record (III): structure of neural network + using sequential and module to define the model
给yarn配置国内镜像加速器
线性代数第二章-矩阵及其运算
治疗TensorFlow后遗症——简单例子记录torch.utils.data.dataset.Dataset重写时的图片维度问题
Pytorch学习记录(九):Pytorch中卷积神经网络
Pyqy5 learning (4): qabstractbutton + qradiobutton + qcheckbox
Pytorch学习记录(四):参数初始化
基于thymeleaf实现数据库图片展示到浏览器表格
Anaconda installed pyqt5 and pyqt5 tools without designer Exe problem solving
Latex快速入门
Software architecture design - software architecture style
创建企业邮箱账户命令
Solve the error: importerror: iprogress not found Please update jupyter and ipywidgets
JSP语法及JSTL标签
RedHat realizes keyword search in specific text types under the directory and keyword search under VIM mode
Pytorch——数据加载和处理
图解HashCode存在的意义
去噪论文阅读——[CVPR2022]Blind2Unblind: Self-Supervised Image Denoising with Visible Blind Spots
Latex quick start
In depth understanding of the relationship between dncblevel and noise denoising in the paper