码迷,mamicode.com
首页 > 数据库 > 详细

C3P0数据库连接池使用

时间:2015-08-06 12:53:16      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

C3P0数据库连接池使用

1、拷贝jar包:c3p0-0.9.1.2.jar     c3p0-0.9.1.2-jdk1.3.jar    c3p0-oracle-thin-extras-0.9.1.2.jar(oracle需要)

2、书写配制文件放在src目录下:c3p0-config.xml(名字只能是这个,而且配置文件必须放在src根目录下)

3、类C3P0Util

c3p0-config.xml 内容:

 

 1     <?xml version="1.0" encoding="UTF-8"?>  
 2     <c3p0-config>  
 3         <!-- This is default config! -->  
 4         <default-config>  
 5             <property name="initialPoolSize">10</property>  
 6             <property name="maxIdleTime">30</property>  
 7             <property name="maxPoolSize">100</property>  
 8             <property name="minPoolSize">10</property>  
 9             <property name="maxStatements">200</property>  
10         </default-config>  
11       
12         <!-- This is my config for mysql-->  
13         <named-config name="mysql">  
14             <property name="driverClass">com.mysql.jdbc.Driver</property>  
15             <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>  
16             <property name="user">root</property>  
17             <property name="password"></property>  
18             <property name="initialPoolSize">10</property>  
19             <property name="maxIdleTime">30</property>  
20             <property name="maxPoolSize">100</property>  
21             <property name="minPoolSize">10</property>  
22             <property name="maxStatements">200</property>  
23         </named-config>  
24           
25           
26         <!-- This is my config for oracle -->  
27         <named-config name="oracle">  
28             <property name="driverClass">oracle.jdbc.driver.OracleDriver</property>  
29             <property name="jdbcUrl">jdbc:oracle:thin:@localhost:1521:orcl</property>  
30             <property name="user">scott</property>  
31             <property name="password">liang</property>  
32             <property name="initialPoolSize">10</property>  
33             <property name="maxIdleTime">30</property>  
34             <property name="maxPoolSize">100</property>  
35             <property name="minPoolSize">10</property>  
36             <property name="maxStatements">200</property>  
37         </named-config>  
38     </c3p0-config>  

 

C3P0Util:

 1     package com.liang.util;  
 2       
 3     import java.io.InputStream;  
 4     import java.sql.Connection;  
 5     import java.sql.PreparedStatement;  
 6     import java.sql.ResultSet;  
 7     import java.sql.SQLException;  
 8     import java.util.Properties;  
 9       
10     import com.mchange.v2.c3p0.ComboPooledDataSource;  
11     /** 
12      * 数据库工具类 
13      * @author liang 
14      * 
15      */  
16     public class C3P0Util {  
17         static ComboPooledDataSource cpds=null;  
18         static{  
19             //这里有个优点,写好配置文件,想换数据库,简单  
20             //cpds = new ComboPooledDataSource("oracle");//这是oracle数据库  
21             cpds = new ComboPooledDataSource("mysql");//这是mysql数据库  
22         }  
23         /** 
24          * 获得数据库连接 
25          * @return   Connection 
26          */  
27         public static Connection getConnection(){  
28             try {  
29                 return cpds.getConnection();  
30             } catch (SQLException e) {  
31                 e.printStackTrace();  
32                 return null;  
33             }  
34         }  
35           
36         /** 
37          * 数据库关闭操作 
38          * @param conn   
39          * @param st     
40          * @param pst 
41          * @param rs 
42          */  
43         public static void close(Connection conn,PreparedStatement pst,ResultSet rs){  
44             if(rs!=null){  
45                 try {  
46                     rs.close();  
47                 } catch (SQLException e) {  
48                     e.printStackTrace();  
49                 }  
50             }  
51             if(pst!=null){  
52                 try {  
53                     pst.close();  
54                 } catch (SQLException e) {  
55                     e.printStackTrace();  
56                 }  
57             }  
58       
59             if(conn!=null){  
60                 try {  
61                     conn.close();  
62                 } catch (SQLException e) {  
63                     e.printStackTrace();  
64                 }  
65             }  
66         }  
67         /** 
68          * 测试DBUtil类 
69          * @param args 
70          */  
71         public static void main(String[] args) {  
72             Connection conn=getConnection();  
73             System.out.println(conn.getClass().getName());  
74             close(conn,null,null);  
75         }  
76     }  

转自:http://blog.csdn.net/liang5630/article/details/39055805

 

C3P0数据库连接池使用

标签:

原文地址:http://www.cnblogs.com/tooker/p/4707646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!