JDBCUtils工具类

package com.oracleoaec.utils;

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

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JDBCUtils {
    // 饿汉式
    private static ComboPooledDataSource ds = new ComboPooledDataSource();

    /**
     * 它为null表示没有事务
     * 它不为null表示有事务
     * 当开启事务时,需要给它赋值
     * 当结束事务时,需要给它赋值为null
     * 并且在开启事务时,让dao的多个方法共享这个Connection
     */
    private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();

    public static DataSource getDataSource() {
        return ds;
    }

    /**
     * dao使用本方法来获取连接
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        /*
         * 如果有事务,返回当前事务的con
         * 如果没有事务,通过连接池返回新的con
         */
        Connection con = tl.get();//获取当前线程的事务连接
        if(con != null) return con;
        return ds.getConnection();
    }

    /**
     * 开启事务
     * @throws SQLException 
     */
    public static void beginTransaction() throws SQLException {
        Connection con = tl.get();//获取当前线程的事务连接
        if(con != null) throw new SQLException("已经开启了事务,不能重复开启!");
        con = ds.getConnection();//给con赋值,表示开启了事务
        con.setAutoCommit(false);//设置为手动提交
        tl.set(con);//把当前事务连接放到tl中
    }

    /**
     * 提交事务
     * @throws SQLException 
     */
    public static void commitTransaction() throws SQLException {
        Connection con = tl.get();//获取当前线程的事务连接
        if(con == null) throw new SQLException("没有事务不能提交!");
        con.commit();//提交事务
        con.close();//关闭连接
        con = null;//表示事务结束!
        tl.remove();
    }

    /**
     * 回滚事务
     * @throws SQLException 
     */
    public static void rollbackTransaction() throws SQLException {
        Connection con = tl.get();//获取当前线程的事务连接
        if(con == null) throw new SQLException("没有事务不能回滚!");
        con.rollback();
        con.close();
        con = null;
        tl.remove();
    }

    /**
     * 释放Connection
     * @param con
     * @throws SQLException 
     */
    public static void releaseConnection(Connection connection) throws SQLException {
        Connection con = tl.get();//获取当前线程的事务连接
        if(connection != con) {//如果参数连接,与当前事务连接不同,说明这个连接不是当前事务,可以关闭!
            if(connection != null &&!connection.isClosed()) {//如果参数连接没有关闭,关闭之!
                connection.close();
            }
        }
    }
    public static QueryRunner getQueryRunner(){
        try {
            /*System.out.println("正在使用连接数"+ds.getNumBusyConnections());// 正在使用连接数
            System.out.println("空闲连接数"+ds.getNumIdleConnections());// 空闲连接数
            System.out.println("总连接数"+ds.getNumConnections());// 总连接数
*/            
            
            return new QueryRunner(ds);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

实例:

    @Override
    public int insert(User user) {
        //通过jdbc模板获取数据库操作对象
        QueryRunner run = JDBCUtils.getQueryRunner();
        //2.操作
        String sql = "INSERT INTO t_user VALUES(SEQ_USERS.NEXTVAL,?,?,?)";
        int row = -1;
        try {
            row = run.update(sql, user.getUsername(),user.getPassword(),user.getEmail());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

    @Override
    public User queryByName(String name) {
        //1.获取数据库操作对象
        QueryRunner run = JDBCUtils.getQueryRunner();
        //2.执行
        String sql = "SELECT * FROM t_user WHERE USERNAME = ?";
        User user = null;
        try {
            user = run.query(sql, new BeanHandler<>(User.class), name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值