zoukankan      html  css  js  c++  java
  • jdbc 以及 事务的java类编写

    package com.gaosheng.utils;


    import java.sql.Connection;
    import java.sql.SQLException;

    import javax.sql.DataSource;

    import com.mchange.v2.c3p0.ComboPooledDataSource;

    public class JdbcUtil {
    private static DataSource datasource = new ComboPooledDataSource(); //连接池对象
    static ThreadLocal<Connection> t = new ThreadLocal();
    private static Connection conn = null; //定义一个事务的专用连接

    //获得连接池对象
    public static DataSource getDatasource(){
    return datasource;
    }

    //获得连接对象
    public static Connection getConnection() throws SQLException{
    conn = t.get();
    if(conn == null) {
    return datasource.getConnection();
    }
    return conn;
    }

    //开启事务方法
    public static void beginTranscation() throws SQLException{
    conn = t.get();
    if(conn != null) {
    throw new SQLException("事务已经开启,在没有结束当前事务时,不能再开启事务!");
    }

    conn = datasource.getConnection();
    conn.setAutoCommit(false);
    t.set(conn);
    }

    //提交事务的方法
    public static void commitTransaction() throws SQLException{
    conn = t.get();
    if(conn == null) {
    throw new SQLException("当前没有事务,所以不能提交事务!");
    }
    conn.commit();
    conn.close();
    //conn = null;
    t.remove();
    }

    //回滚事物的方法
    public static void rollbackTransaction() throws SQLException{
    if(conn == null) {
    throw new SQLException("当前没有事务,所以不能回滚事务!");
    }
    conn.rollback();
    conn.close();
    //conn = null;
    t.remove();
    }
    }

    package com.gaosheng.utils;


    import java.sql.Connection;
    import java.sql.SQLException;

    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.ResultSetHandler;

    public class TxQueryRunner extends QueryRunner{

    @Override
    public int[] batch(String sql, Object[][] params) throws SQLException {
    Connection conn = JdbcUtil.getConnection();
    return super.batch(conn, sql, params);
    }


    @Override
    public <T> T query(String sql, Object param, ResultSetHandler<T> rsh) throws SQLException {
    Connection conn = JdbcUtil.getConnection();
    return super.query(conn,sql, param, rsh);
    }

    /* (non-Javadoc)
    * @see org.apache.commons.dbutils.QueryRunner#query(java.lang.String, java.lang.Object[], org.apache.commons.dbutils.ResultSetHandler)
    */
    @Override
    public <T> T query(String sql, Object[] params, ResultSetHandler<T> rsh) throws SQLException {
    Connection conn = JdbcUtil.getConnection();
    return super.query(conn,sql, params, rsh);
    }

    /* (non-Javadoc)
    * @see org.apache.commons.dbutils.QueryRunner#query(java.lang.String, org.apache.commons.dbutils.ResultSetHandler, java.lang.Object[])
    */
    @Override
    public <T> T query(String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException {
    // TODO Auto-generated method stub
    return super.query(sql, rsh, params);
    }

    /* (non-Javadoc)
    * @see org.apache.commons.dbutils.QueryRunner#query(java.lang.String, org.apache.commons.dbutils.ResultSetHandler)
    */
    @Override
    public <T> T query(String sql, ResultSetHandler<T> rsh) throws SQLException {
    // TODO Auto-generated method stub
    return super.query(sql, rsh);
    }

    /* (non-Javadoc)
    * @see org.apache.commons.dbutils.QueryRunner#update(java.lang.String)
    */
    @Override
    public int update(String sql) throws SQLException {
    // TODO Auto-generated method stub
    return super.update(sql);
    }

    /* (non-Javadoc)
    * @see org.apache.commons.dbutils.QueryRunner#update(java.lang.String, java.lang.Object)
    */
    @Override
    public int update(String sql, Object param) throws SQLException {
    // TODO Auto-generated method stub
    return super.update(sql, param);
    }

    /* (non-Javadoc)
    * @see org.apache.commons.dbutils.QueryRunner#update(java.lang.String, java.lang.Object[])
    */
    @Override
    public int update(String sql, Object... params) throws SQLException {
    Connection conn = JdbcUtil.getConnection();
    return super.update(conn,sql, params);
    }


    }

  • 相关阅读:
    洛谷 P2634 BZOJ 2152 【模板】点分治(聪聪可可)
    洛谷 P3819 松江1843路
    洛谷 P1005 矩阵取数游戏
    洛谷 P2712 摄像头
    洛谷 P2774 方格取数问题
    洛谷 P3369 BZOJ 3224 【模板】普通平衡树(Treap/SBT)
    洛谷 P2805 BZOJ 1565 植物大战僵尸
    洛谷 P2312 解方程
    洛谷 P3355 骑士共存问题
    洛谷 P2762 太空飞行计划问题
  • 原文地址:https://www.cnblogs.com/0307lmy/p/11685347.html
Copyright © 2011-2022 走看看