JDBCUtils工具类

来源:互联网 发布:手机温度监控软件 编辑:程序博客网 时间:2024/06/06 05:04
 

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.xml10  *        + c3p0-0.9.2-pre1.jar11  *        + mchange-commons-0.2.jar12  * 版本1.3 13  * 更新日期:2015/2/2314  * @author Administrator15  *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      * @return26      * @throws SQLException27      */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      * @return36      */37     public static DataSource getDataSource(){38         return dataSource;39     }40     /**41      * 开启事务42      * @throws SQLException43      */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 SQLException54      */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() == null61     }62     /**63      * 回滚事务64      * @throws SQLException65      */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 connection76      * @throws SQLException77      */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)中指定相应的名称就可以了,如上面的代码。

0 0
原创粉丝点击