C3P0连接池使用
来源:互联网 发布:迅游加速器mac版 编辑:程序博客网 时间:2024/05/16 08:03
1、需要引入c3p0的包
2、配置文件配置连接池
- c3p0的配置方式分为三种,分别是
- 1.setters一个个地设置各个配置项
2.类路径下提供一个c3p0.properties文件
3.类路径下提供一个c3p0-config.xml文件
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> <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property> <property name="user">root</property> <property name="password">199420</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> </named-config> <!-- This is my config for oracle --> <named-config name="oracle"> <property name="driverClass">oracle.jdbc.driver.OracleDriver</property> <property name="jdbcUrl">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="user">scott</property> <property name="password">liang</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> </named-config> </c3p0-config>
3、java中获取连接
package utils;import java.sql.Connection; import java.sql.SQLException; import com.mchange.v2.c3p0.ComboPooledDataSource;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 { return cpds.getConnection(); } catch (SQLException e) { e.printStackTrace(); return null; } } /** * 数据库连接关闭操作 * @param conn */ public static void close(Connection conn){ if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 测试 * @param args */ public static void main(String[] args) { Connection conn=getConnection(); String sql = "select * from book_info order by score desc"; DBUtil.query(sql, conn); try { System.out.println(cpds.getNumConnections()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
阅读全文
0 1
- c3p0连接池使用
- 使用C3P0连接池
- C3P0连接池使用
- C3P0连接池使用
- c3p0连接池的使用
- Tomcat 使用 c3p0连接池
- C3P0连接池使用小结
- 使用c3p0创建连接池
- 使用c3p0连接池checkoutTimeout
- C3P0连接池使用小结
- c3p0连接池的使用
- hibernate使用c3p0连接池
- Hibernate使用c3p0连接池
- C3P0连接池的使用
- C3P0连接池使用教程
- C3P0连接池使用教程
- 使用C3P0连接池释放当前连接
- Hibernate使用C3P0的连接池,并队c3p0配置。
- C#使用Protocol Buffer(ProtoBuf)进行Unity中的Socket通信
- CentOS6.5下安装10G
- i++和++i ,i--和--i的使用方法和作用
- Sublime Text 3 快捷键
- idea写代码注意事项
- C3P0连接池使用
- Spring MVC中Controller的四种配置方法
- hpuoj 【1166】阶乘问题(一) 【数学】&&【思维转化】
- codeforces 583A Asphalting Roads
- iOS懒加载不执行的坑
- LeetCode之路:543. Diameter of Binary Tree
- 求素数及素数表的获取
- parse(),stringify()
- Notification通知