JDBCUtils工具類

 

JdbcUtils.java

 1 import java.sql.Connection;
 2 import java.sql.SQLException;
 3 
 4 import javax.sql.DataSource;
 5 
 6 import com.mchange.v2.c3p0.ComboPooledDataSource;
 7 /**
 8  * 依賴:
 9  *        + c3p0-config.xml
10  *        + c3p0-0.9.2-pre1.jar
11  *        + mchange-commons-0.2.jar
12  * 版本1.3 
13  * 更新日期:2015/2/23
14  * @author Administrator
15  *
16  */
17 public class JdbcUtils {
18     //ComboPooledDataSource(String configName)的參數configName指的是配置文件c3p0-config.xml中的 <named-config name="mysql">...</named-config>
19     //如果沒有輸入configName參數,那麼就採用默認的<default-config>...</defalut-config>
20     private static ComboPooledDataSource dataSource = new ComboPooledDataSource("mysql");
21     private static Connection con = null;
22     private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
23     /**
24      * 獲取連接對象
25      * @return
26      * @throws SQLException
27      */
28     public static Connection getConnection() throws SQLException{
29         con = tl.get();
30         if(con != null){return con;}
31         return dataSource.getConnection();
32     }
33     /**
34      * 獲取連接池對象
35      * @return
36      */
37     public static DataSource getDataSource(){
38         return dataSource;
39     }
40     /**
41      * 開啓事務
42      * @throws SQLException
43      */
44     public static void beginTransaction() throws SQLException{
45         con = tl.get();
46         if(con != null){throw new RuntimeException("事務已經開啓!不能重複開啓!");}
47         con = getConnection();
48         con.setAutoCommit(false);
49         tl.set(con);
50     }
51     /**
52      * 提交事務
53      * @throws SQLException
54      */
55     public static void commitTransaction() throws SQLException{
56         con = tl.get();
57         if(con == null){throw new RuntimeException("事務還沒開啓!不能提交!");}
58         con.commit();
59         con.close();//歸還連接對象到連接池
60         tl.remove();//移除連接對象con。那麼tl.get() == null
61     }
62     /**
63      * 回滾事務
64      * @throws SQLException
65      */
66     public static void rollbackTransaction() throws SQLException{
67         con = tl.get();
68         if(con == null){throw new RuntimeException("事務還沒開啓!不能回滾!");}
69         con.rollback();
70         con.close();
71         tl.remove();
72     }
73     /**
74      * 關閉 非事務專用的連接
75      * @param connection
76      * @throws SQLException
77      */
78     public static void releaseConnection(Connection connection) throws SQLException{
79         con = tl.get();//獲取事務專用連接
80         if(con == null){connection.close();}
81         if(con != connection){connection.close();}
82     } 
83 }

c30p-config.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <c3p0-config>
 3     <!-- c3p0有兩種配置方式,一種是默認的default-config,一種是按名字區分:named-config需要添加一個屬性值name -->
 4     <default-config> 
 5         <property name="jdbcUrl">jdbc:oracle:thin:username/password@amrood:1521:EMP</property>
 6         <property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
 7         <property name="user">root</property>
 8         <property name="password"></property>
 9         
10         <property name="acquireIncrement">3</property>
11         <property name="initialPoolSize">10</property>
12         <property name="minPoolSize">2</property>
13         <property name="maxPoolSize">10</property>
14     </default-config>
15     <named-config name="mysql"> 
16         <property name="jdbcUrl">jdbc:mysql://localhost:3306/db_user</property>
17         <property name="driverClass">com.mysql.jdbc.Driver</property>
18         <property name="user">root</property>
19         <property name="password"></property>
20         
21         <property name="acquireIncrement">3</property>
22         <property name="initialPoolSize">10</property>
23         <property name="minPoolSize">2</property>
24         <property name="maxPoolSize">10</property>
25     </named-config>
26 </c3p0-config>

在這個c3p0-config.xml中配置了兩個數據庫,如果我要使用oracle的數據庫那麼我在JdbcUtils類中的ComboPoolDataSource()不傳入參數即可,也就是採用默認的方式。

而我在c3p0-config.xml中就是把oracle配成了默認的方式。如果我要不使用默認方式,那麼我只要在ComboPoolDataSource(String configName)中指定相應的名稱就可以了,如上面的代碼。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章