C3P0连接池的配置与使用

1、下载c3p0-0.9.1.2.jar

下载地址:http://download.csdn.net/detail/chunxiaqiudong5/9661922


2、添加配置文件c3p0-config.xml



3、配置文件内容如下:

[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <c3p0-config>    
  3.         
  4.     <default-config>    
  5.         <property name="initialPoolSize">10property>    
  6.         <property name="maxIdleTime">30property>    
  7.         <property name="maxPoolSize">100property>    
  8.         <property name="minPoolSize">10property>    
  9.         <property name="maxStatements">200property>    
  10.     default-config>    
  11.     
  12.         
  13.     <named-config name="mysql">    
  14.         <property name="driverClass">com.mysql.jdbc.Driverproperty>    
  15.         <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=UTF8property>    
  16.         <property name="user">rootproperty>    
  17.         <property name="password">property>    
  18.            
  19.         <property name="initialPoolSize">10property>  
  20.             
  21.         <property name="maxIdleTime">30property>    
  22.           
  23.         <property name="maxPoolSize">100property>   
  24.            
  25.         <property name="minPoolSize">10property>   
  26.            
  27.         <property name="maxStatements">200property>    
  28.              
  29.         <property name="checkoutTimeout" value="3000"/>   
  30.              
  31.         <property name="acquireIncrement" value="2"/>   
  32.              
  33.         <property name="acquireRetryAttempts" value="0"/>    
  34.              
  35.         <property name="acquireRetryDelay" value="1000" />   
  36.              
  37.         <property name="autoCommitOnClose">falseproperty>    
  38.              
  39.         <property name="automaticTestTable">Testproperty>   
  40.               
  41.         <property name="breakAfterAcquireFailure">falseproperty>  
  42.              
  43.         <property name="idleConnectionTestPeriod">60property>      
  44.              
  45.         <property name="maxStatementsPerConnection">property>   
  46.     named-config>    
  47.         
  48.         
  49.         
  50.     <named-config name="oracle">    
  51.         <property name="driverClass">oracle.jdbc.driver.OracleDriverproperty>    
  52.         <property name="jdbcUrl">jdbc:oracle:thin:@localhost:1521:orclproperty>    
  53.         <property name="user">scottproperty>    
  54.         <property name="password">liangproperty>    
  55.         <property name="initialPoolSize">10property>    
  56.         <property name="maxIdleTime">30property>    
  57.         <property name="maxPoolSize">100property>    
  58.         <property name="minPoolSize">10property>    
  59.         <property name="maxStatements">200property>    
  60.     named-config>    
  61. c3p0-config>  


4、连接池连接类

[java]  view plain  copy
  1. package com.xxx.utils;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.SQLException;  
  5.   
  6. import javax.sql.DataSource;  
  7.   
  8. import com.mchange.v2.c3p0.ComboPooledDataSource;  
  9.   
  10. public class JDBCUtil {  
  11.   
  12.     private static DataSource dataSource=null;  
  13.     static{  
  14.         dataSource=new ComboPooledDataSource("mysql");  
  15.     }  
  16.       
  17.     /** 
  18.      * 获取数据库连接 
  19.      * @return 
  20.      */  
  21.     public static Connection getConnection(){  
  22.         Connection conn=null;  
  23.         try {  
  24.              conn=dataSource.getConnection();  
  25.         } catch (SQLException e) {  
  26.             e.printStackTrace();  
  27.         }  
  28.         return conn;  
  29.     }  
  30.   
  31.       
  32.     /** 
  33.      * 关闭数据库连接 
  34.      * @param conn 
  35.      */  
  36.     public static void closeConn(Connection conn){  
  37.         try {  
  38.             if(conn!=null && conn.isClosed()){  
  39.                 conn.close();  
  40.             }  
  41.         } catch (SQLException e) {  
  42.             e.printStackTrace();  
  43.         }  
  44.     }  
  45. }  


5、测试

[java]  view plain  copy
  1. package com.xxx.test;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.PreparedStatement;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7.   
  8. import org.junit.Test;  
  9.   
  10. import com.xxx.utils.JDBCUtil;  
  11.   
  12. public class TestJdbc {  
  13.   
  14.     @Test  
  15.     public void test() {  
  16.         Connection conn=JDBCUtil.getConnection();  
  17.         System.out.println(conn);  
  18.         try {  
  19.             PreparedStatement stmt=conn.prepareStatement("select * from tb_user");  
  20.             ResultSet re=stmt.executeQuery();  
  21.             while(re.next()){  
  22.                 String name=re.getString(2);  
  23.                 System.out.println(name);  
  24.                   
  25.             }  
  26.               
  27.               
  28.         } catch (SQLException e) {  
  29.             // TODO Auto-generated catch block  
  30.             e.printStackTrace();  
  31.         }  
  32.     }  
  33.       
  34.       
  35.   
  36. }  

你可能感兴趣的:(Java,Java基础)