c3p0的使用
来源:互联网 发布:高大上的礼物知乎 编辑:程序博客网 时间:2024/06/06 13:48
自己按照网上的文章配置了c3p0,不知道是不是正确或者哪里不好,请各位指点批评
项目结构如下图
src下有c3p0的配置文件 c3p0-config.xml 配置文件内容如下
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <!-- This is default config! --> <default-config> <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 is my config for mysql--> <named-config name="mysql"> <!-- 指定连接数据库的驱动 --> <property name="driverClass">com.mysql.jdbc.Driver</property> <!-- 指定连接数据库的URL --> <property name="jdbcUrl">jdbc:mysql://xxx.xx.xxx.xxx:3307/golvon?characterEncoding=utf-8</property> <!-- 指定连接数据库的用户名 --> <property name="user">xxxx</property> <!-- 指定连接数据库的密码 --> <property name="password">xxxx</property> <!-- 指定连接池中保留的最大连接数. Default:15 --><property name="maxPoolSize">100</property><!-- 指定连接池中保留的最小连接数 --><property name="minPoolSize">10</property><!-- 指定连接池的初始化连接数 取值应在minPoolSize 与 maxPoolSize 之间.Default:3 --><property name="initialPoolSize">20</property><!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0 --><property name="maxIdleTime">600</property><!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3 --><property name="acquireIncrement">5</property><!-- JDBC的标准,用以控制数据源内加载的PreparedStatements数量。 但由于预缓存的statements属于单个connection而不是整个连接池所以设置这个参数需要考虑到多方面的因数. 如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0 --><property name="maxStatements">5</property><!-- 每60秒检查所有连接池中的空闲连接.Default:0 --><property name="idleConnectionTestPeriod">60</property> </named-config> </c3p0-config>还有一个c3p0链接工具类C3P0Util 内容如下
package com.cn.piaoju.db.util;import java.sql.Connection;import java.sql.SQLException;import com.mchange.v2.c3p0.ComboPooledDataSource;import com.mysql.jdbc.PreparedStatement;import com.mysql.jdbc.ResultSet;public class C3P0Util {static ComboPooledDataSource cpds=null; static{ //这里有个优点,写好配置文件,想换数据库,简单 //cpds = new ComboPooledDataSource("oracle");//这是oracle数据库 cpds = new ComboPooledDataSource("mysql");//这是mysql数据库 } /** * 获得数据库连接 * @return Connection */ public static Connection getConnection(){ try { cpds.getConnection(); return cpds.getConnection(); } catch (SQLException e) { e.printStackTrace(); return null; } } /** * 数据库关闭操作 * @param conn * @param st * @param pst * @param rs */ public static void close(Connection conn,PreparedStatement pst,ResultSet rs){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(pst!=null){ try { pst.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 测试DBUtil类 * @param args */ public static void main(String[] args) { Connection conn=getConnection(); System.out.println(conn.getClass().getName()); close(conn,null,null); } }main方法运行如下:
com.mchange.v2.c3p0.impl.NewProxyConnection
这样是不是就已经配置成功了,其他的数据库操作是不是可以想jdbc一样操作
求知道
0 0
- C3P0使用的笔记
- c3p0的使用
- C3P0使用的笔记
- c3p0的使用
- c3p0的使用实例!
- c3p0的使用
- C3P0的使用
- c3p0的使用实例!
- c3p0的使用
- c3p0的使用
- C3P0的使用
- 数据源c3p0的使用
- C3P0的使用
- hibernate中c3p0的使用
- c3p0连接池的使用
- c3p0数据库连接池的使用
- c3p0数据库连接池的使用
- c3p0数据库连接池的使用
- 自己修改的MBProgressHUD 代码
- cookie基础知识
- ZooKeeper应用案例
- Spark生态之Alluxio学习5--tachyon的几个问题(待解决)
- Android Studio注释模板
- c3p0的使用
- yaffs2文件系统的制作
- 对Set的遍历
- 视频通信01
- 第27条 泛型——优先考虑泛型方法
- 使用SQLAlchemy
- 开启PowerDesigner15工具栏上的被禁用掉的图标
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- 开始记录开发中的一些心得