zoukankan      html  css  js  c++  java
  • c3p0封装

    配置文件

    1 <?xml version="1.0" encoding="UTF-8"?>
    2 <c3p0-config>
    3     <default-config>
    4         <property name="user">root</property>
    5         <property name="password">root</property>
    6         <property name="driverClass">com.mysql.jdbc.Driver</property>
    7         <property name="jdbcUrl">jdbc:mysql:///day35</property>
    8     </default-config>
    9 </c3p0-config> 

    Java

     1 package utils;
     2 
     3 import java.sql.Connection;
     4 import java.sql.ResultSet;
     5 import java.sql.SQLException;
     6 import java.sql.Statement;
     7 
     8 import javax.sql.DataSource;
     9 
    10 import com.mchange.v2.c3p0.ComboPooledDataSource;
    11 
    12 public class DataSourceUtils {
    13 
    14     private static DataSource dataSource = new ComboPooledDataSource();
    15 
    16     private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
    17 
    18     // 直接可以获取一个连接池
    19     public static DataSource getDataSource() {
    20         return dataSource;
    21     }
    22 
    23     public static Connection getConnection() throws SQLException {
    24         return dataSource.getConnection();
    25     }
    26 
    27     // 获取连接对象
    28     public static Connection getCurrentConnection() throws SQLException {
    29 
    30         Connection con = tl.get();
    31         if (con == null) {
    32             con = dataSource.getConnection();
    33             tl.set(con);
    34         }
    35         return con;
    36     }
    37 
    38     // 开启事务
    39     public static void startTransaction() throws SQLException {
    40         Connection con = getCurrentConnection();
    41         if (con != null) {
    42             con.setAutoCommit(false);
    43         }
    44     }
    45 
    46     // 事务回滚
    47     public static void rollback() throws SQLException {
    48         Connection con = getCurrentConnection();
    49         if (con != null) {
    50             con.rollback();
    51         }
    52     }
    53 
    54     // 提交并且 关闭资源及从ThreadLocall中释放
    55     public static void commitAndRelease() throws SQLException {
    56         Connection con = getCurrentConnection();
    57         if (con != null) {
    58             con.commit(); // 事务提交
    59             con.close();// 关闭资源
    60             tl.remove();// 从线程绑定中移除
    61         }
    62     }
    63 
    64     // 关闭资源方法
    65     public static void closeConnection() throws SQLException {
    66         Connection con = getCurrentConnection();
    67         if (con != null) {
    68             con.close();
    69         }
    70     }
    71 
    72     public static void closeStatement(Statement st) throws SQLException {
    73         if (st != null) {
    74             st.close();
    75         }
    76     }
    77 
    78     public static void closeResultSet(ResultSet rs) throws SQLException {
    79         if (rs != null) {
    80             rs.close();
    81         }
    82     }
    83 
    84 }
  • 相关阅读:
    51nod 1227 平均最小公倍数
    51nod 1238 最小公倍数之和 V3
    「G2016 SCOI2018 Round #2」B
    51nod 1258 序列求和 V4
    2301: [HAOI2011]Problem b
    POJ
    NOIP2017解题报告
    笔记-[ZJOI2014]力
    题解-Little C Loves 3 III
    李超线段树
  • 原文地址:https://www.cnblogs.com/voidchar/p/10514397.html
Copyright © 2011-2022 走看看