当前位置:网站首页>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
边栏推荐
- Pytorch学习记录(十二):学习率衰减+正则化
- Custom exception class
- Pyemd installation and simple use
- 域内用户访问域外samba服务器用户名密码错误
- 给yarn配置国内镜像加速器
- Understand the current commonly used encryption technology system (symmetric, asymmetric, information abstract, digital signature, digital certificate, public key system)
- Graphic numpy array matrix
- Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots
- 开发环境 EAS登录 license 许可修改
- Pyqy5 learning (4): qabstractbutton + qradiobutton + qcheckbox
猜你喜欢
Filebrowser realizes private network disk
Pytorch learning record (7): skills in processing data and training models
Multithreading and high concurrency (1) -- basic knowledge of threads (implementation, common methods, state)
图像恢复论文——[RED-Net, NIPS16]Image Restoration Using Very Deep Convolutional Encoder-Decoder Networks wi
Multithreading and high concurrency (2) -- detailed explanation of synchronized usage
Understand the current commonly used encryption technology system (symmetric, asymmetric, information abstract, digital signature, digital certificate, public key system)
Pytorch learning record (XII): learning rate attenuation + regularization
Linear algebra Chapter 2 - matrices and their operations
Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi
数字图像处理基础(冈萨雷斯)一
随机推荐
In depth source code analysis servlet first program
Fact final variable and final variable
What is the difference between the basic feasible solution and the basic feasible solution in linear programming?
Custom exception class
ValueError: loaded state dict contains a parameter group that doesn‘t match the size of optimizer‘s
Multithreading and high concurrency (3) -- synchronized principle
CONDA virtual environment management (create, delete, clone, rename, export and import)
线性规划问题中可行解,基本解和基本可行解有什么区别?
Pytorch introduction notes - use a simple example to observe the output size of each layer of forward propagation
Linear algebra Chapter 2 - matrices and their operations
Pytorch learning record (XII): learning rate attenuation + regularization
Pytorch learning record (XI): data enhancement, torchvision Explanation of various functions of transforms
Pyqy5 learning (III): qlineedit + qtextedit
图像恢复论文简记——Uformer: A General U-Shaped Transformer for Image Restoration
Viewer: introduce MySQL date function
The attendance client date of K / 3 wise system can only be selected to 2019
Solution record of slow access speed of SMB service in redhat6
JDBC连接数据库
Shansi Valley P290 polymorphism exercise
Multithreading and high concurrency (1) -- basic knowledge of threads (implementation, common methods, state)