当前位置:网站首页>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
边栏推荐
- Mysql 查询使用\G,列转行
- Idea plug-in --- playing songs in the background
- What financial products will benefit during May Day?
- 合约锁仓漏洞
- ‘EddiesObservations‘ object has no attribute ‘filled‘
- Common interview questions - 4 (MySQL)
- MySQL query uses \ g, column to row
- MySQL lock mechanism
- poi导出excel,行相同数据自动合并单元格
- Ora: 28547 connection to server failed probable Oracle net admin error
猜你喜欢

框架解析1.系统架构简介

Breadth first search topics (BFS)

QSS, qdateedit, qcalendarwidget custom settings

‘EddiesObservations‘ object has no attribute ‘filled‘

Hongji | how does HR carry out self change and organizational change in the digital era?

Parameter analysis of open3d material setting

Interview Basics

Common protocols of OSI layer

C, class library

Issue 36 summary of atcoder beginer contest 248
随机推荐
Mysql 查询使用\G,列转行
No.1.#_6 Navicat快捷键
Shell instruction learning 1
Hongji micro classroom | cyclone RPA's "flexible digital employee" actuator
Frequently asked interview questions - 1 (non technical)
Generation of straightening body in 3D slicer
Object to map
Summary of redis classic interview questions 2022
mysql中duplicate key update
JS number capitalization method
Formal parameters, local variables and local static variables
Pilotage growth · ingenuity empowerment -- yonmaster developer training and pilotage plan is fully launched
Dwsurvey is an open source questionnaire system. Solve the problem that cannot be run and modify the bug.
Sword finger offer II 022 The entry node of the link in the linked list
实体中list属性为空或者null,设置为空数组
TypeScript interface & type 粗略理解
Issue 36 summary of atcoder beginer contest 248
windows连接mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)
MySQL triggers, stored procedures, stored functions
Common interview questions - 4 (MySQL)