C3P0模板
2024-09-27 11:02:21
1.创建c3p0-config.xml配置文件放在src下
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!-- default-config 默认的配置, -->
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost/mydb</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
</default-config>
<!-- This app is massive! -->
<named-config name="oracle">
<property name="acquireIncrement">50</property>
<property name="initialPoolSize">100</property>
<property name="minPoolSize">50</property>
<property name="maxPoolSize">1000</property>
<!-- intergalactoApp adopts a different approach to configuring statement caching -->
<property name="maxStatements">0</property>
<property name="maxStatementsPerConnection">5</property>
<!-- he's important, but there's only one of him -->
<user-overrides user="master-of-the-universe">
<property name="acquireIncrement">1</property>
<property name="initialPoolSize">1</property>
<property name="minPoolSize">1</property>
<property name="maxPoolSize">5</property>
<property name="maxStatementsPerConnection">50</property>
</user-overrides>
</named-config>
</c3p0-config>
2.创建JDBCUtil
package com.rick.util;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JDBCUtil {
static ComboPooledDataSource dataSource = null;
static{
dataSource = new ComboPooledDataSource();
}
/**
* 数据库连接池C3P0
* @return
* @throws SQLException
*/
public static DataSource getDataSouce() {
return dataSource;
}
/**
* 获取连接对象
* @return
* @throws SQLException
*/
public static Connection getConn() throws SQLException{
return dataSource.getConnection();
}
/**
* 释放资源
* @param conn
* @param st
* @param rs
*/
public static void release(Connection conn , Statement st , ResultSet rs){
closeRs(rs);
closeSt(st);
closeConn(conn);
}
public static void release(Connection conn , Statement st){
closeSt(st);
closeConn(conn);
}
private static void closeRs(ResultSet rs){
try {
if(rs != null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
rs = null;
}
}
private static void closeSt(Statement st){
try {
if(st != null){
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
st = null;
}
}
private static void closeConn(Connection conn){
try {
if(conn != null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
conn = null;
}
}
}
怎么用呢?
public class StudentDaoImpl implements StudentDao{
/*
* 抛异常的时候父类没有抛子类不能直接抛,所以去父类先抛一下
*/
//查找所有
@Override
public List<Student> findAll() throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil.getDataSouce());
String sql = "select * from stu";
List<Student> list = runner.query(sql, new BeanListHandler<Student>(Student.class));
return list;
}
//添加
@Override
public void insert(Student student) throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil.getDataSouce());
runner.update("insert into stu values(null,?,?,?,?,?,?)",
student.getSname(),student.getGender(),student.getPhone(),
student.getBirthday(),student.getHobby(),student.getInfo());
}
//删除
@Override
public void delete(Integer sid) throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil.getDataSouce());
runner.update("delete from stu where sid = ?",sid);
}
//查找单个
@Override
public Student findStudentById(Integer sid) throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil.getDataSouce());
Student student = runner.query("select * from stu where sid = ?", new BeanHandler<Student>(Student.class),sid);
return student;
}
//修改
@Override
public void update(Student student) throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil.getDataSouce());
runner.update("update stu set sname=?,gender=?,phone=?,birthday=?,hobby=?,info=? where sid=?",
student.getSname(),student.getGender(),student.getPhone(),
student.getBirthday(),student.getHobby(),student.getInfo(),
student.getSid());
}
}
最新文章
- HTML5+CSS3实现图片可倾斜摆放的动画相册效果
- Entity Framework在Asp.net MVC中的实现One Context Per Request(附源码)
- struts2&;&;Hibernate Demo1
- 学习使用:before和:after伪元素
- android-exploitme(七):高级加密
- Android java.net.SocketException四大异常解决方案
- iOS之layout方法-layoutSubviews、layoutIfNeeded、setNeedsLayout
- pycharm console 控制台乱码的解决
- Entity Framework Code First在Oracle下的伪实现
- puppet配置问题统计
- php 连接mssql
- Measuring Text Difficulty Using Parse-Tree Frequency
- $.ajax ,ajax请求添加请求头,添加Authorization字段
- cocos creator热更新教程
- (译)内存沉思:多个名称相关的神秘的SQL Server内存消耗者。
- python第三十五天-----作业完成--学校选课系统
- [翻译] UCZProgressView
- tcping测试端口是否畅通
- (转)ci
- Windows Oracle连接ORA-12541:TNS:无监听程序
热门文章
- spring事务传播属性和隔离级别
- IEEE Spectrum 2014年十大编程语言盘点
- 学习进度-10 python爬虫
- CentOS LVM 卷在线扩容
- app1----攻防世界
- C++面试常见问题——17类模板的使用
- Centos 7 安装与卸载MYSQL5.7
- 004.CI4框架CodeIgniter, 配置mysql数据库,并进行数据库查询
- https://blog.csdn.net/yyoinge/article/details/81557604
- pyhton中pandas数据分析模块快速入门(非常容易懂)