zoukankan      html  css  js  c++  java
  • 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)中指定相应的名称就可以了,如上面的代码。

  • 相关阅读:
    MAC 上找不到.bash_profile或者ect/profile该怎么办?
    iOS极光推送集成步骤
    排序算法
    iOS之内存管理浅谈
    iOS之多线程浅谈
    iOS之RunTime浅谈
    Hash算法初见
    webApi实现增删改查操作
    Python: sqlite3模块
    python3:iterable, iterator, generator,抽象基类, itertools的使用。
  • 原文地址:https://www.cnblogs.com/JamKong/p/4899319.html
Copyright © 2011-2022 走看看