数据库连接池技术--c3p0
生活随笔
收集整理的這篇文章主要介紹了
数据库连接池技术--c3p0
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一丶c3p0所需jar包(maven項目中pom.xml文件中添加如下)
<!-- https://mvnrepository.com/artifact/c3p0/c3p0 --> <dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version> </dependency>二丶創建一個文件名為c3p0-config.xml的文件(注意的是:文件名必須為這個)
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config><!-- 這是默認配置信息 --><default-config><!-- 連接四大參數配置 --><property name="jdbcUrl">jdbc:mysql://localhost:3306/tb_test</property><property name="driverClass">com.mysql.jdbc.Driver</property><property name="user">root</property><property name="password">root</property><!-- 池參數配置 --><property name="acquireIncrement">3</property><property name="initialPoolSize">10000</property><property name="minPoolSize">10</property><property name="maxPoolSize">10</property><property name="defaultAutoCommit" value="false"/><property name="maxIdle" value="5"/><property name="minIdle" value="1"/><property name="maxActive" value="40"/><property name="removeAbandoned" value="true"/><property name="removeAbandonedTimeout" value="180"/><property name="maxWait" value="15000"/><property name="timeBetweenEvictionRunsMillis" value="120000"/><property name="minEvictableIdleTimeMillis" value="300000"/></default-config> </c3p0-config>三丶寫一個數據庫連接池c3p0的工具類
private static Connection connection;//定義一個連接private static ComboPooledDataSource dataSource = new ComboPooledDataSource();//創建一個數據庫連接池public static Connection getConnection(){//從數據庫連接池中獲取連接try {connection=dataSource.getConnection();//獲取到一個連接} catch (SQLException e) {e.printStackTrace();}return connection;}四丶測試
public static void main(String[] args) {Connection connection = c3p0Util.getConnection();//調用c3p0工具類中獲取連接方法try {Statement statement = connection.createStatement();ResultSet set = statement.executeQuery("select * from tb_dept");while (set.next()){System.out.println(set.getString("deptno")+" "+set.getString("dname")+" "+set.getString("loc"));}} catch (SQLException e) {e.printStackTrace();}}總結
以上是生活随笔為你收集整理的数据库连接池技术--c3p0的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 15个SaaS问答(图文并茂版)
- 下一篇: 数据库操作技术--Spring jdbc