首页 > 代码库 > 数据源C3P0配置
数据源C3P0配置
1.导入jar包(dbutil->QueryRunner)
2.C3P0Util工具类
package com.learning.utils; import java.beans.PropertyVetoException; 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 C3P0Util { private static DataSource dataSource = new ComboPooledDataSource(); /*没有用配置文件 private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); static{ try { dataSource.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver dataSource.setJdbcUrl( "jdbc:mysql://localhost:3306/book" ); dataSource.setUser("root"); dataSource.setPassword("root"); } catch (PropertyVetoException e) { // TODO Auto-generated catch block e.printStackTrace(); } }*/ //从数据源中获得连接 public static Connection getConnection() { try { return dataSource.getConnection(); } catch (SQLException e) { throw new RuntimeException("数据库连接失败"); } } //释放连接 public static void release(Connection connection,Statement statement,ResultSet resultSet) { if (connection!=null) { try { //把不用的connection对象放回连接池(实际用了装饰模式) connection.close(); } catch (SQLException e) { e.printStackTrace(); } connection=null; } if (statement!=null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } statement=null; } if (resultSet!=null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } resultSet=null; } } }
3.配置文件:c3p0-config.xml(名字不能更改)
<c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/book</property> <property name="user">root</property> <property name="password">root</property> <property name="automaticTestTable">con_test</property> <property name="checkoutTimeout">30000</property> <property name="idleConnectionTestPeriod">30</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> <user-overrides user="test-user"> <property name="maxPoolSize">10</property> <property name="minPoolSize">1</property> <property name="maxStatements">0</property> </user-overrides> </default-config> </c3p0-config>
数据源C3P0配置
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。