jdbc数据库连接工具类

1.使用ThreadLocal整合jdbc工具类:
注意事项:tomcat线程复用可能导致,线程获取到已经关闭的连接,所以需要在获取连接时判断是否关闭,如果已关闭重新创建连接;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

//使用ThreadLocal完成操作
public class JdbcUtils {

    public static final String DRIVERCLASS;
    public static final String URL;
    public static final String USERNAME;
    public static final String PASSWORD;

    static {
        // 从配置文件中加载数据
        ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
        DRIVERCLASS = bundle.getString("driver");
        URL = bundle.getString("url");
        USERNAME = bundle.getString("username");
        PASSWORD = bundle.getString("password");
    }

    static {
        // 1.注册驱动
        try {
            Class.forName(DRIVERCLASS);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    private static final ThreadLocal<Connection> tl = new ThreadLocal<Connection>();

    public static Connection getConnection() throws ClassNotFoundException,
            SQLException {

        // 2.获取连接对象Connection
        Connection con = tl.get(); // 从ThreadLocal中获取,那么第一次获取时,得到的是null 代码
                                   // map.get(Thread.currentThread());
        //tomcat线程复用可能导致,线程获取到已经关闭的连接,所以需要判断是否关闭
        if (con == null || con.isClosed()) {
            con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            tl.set(con);// 这时就相当于 map.put(Thread.currentThead(),con);
        }
        return con;
    }

    // 做一个开启事务的方法
    public static void startTransaction() throws ClassNotFoundException,
            SQLException {
        Connection con = JdbcUtils.getConnection();
        if (con != null)
            con.setAutoCommit(false);
    }

    // 事务回滚
    public static void rollback() throws SQLException, ClassNotFoundException {
        Connection con = JdbcUtils.getConnection();
        if (con != null) {
            con.rollback();
        }
    }

    // 事务提交及关闭资源
    public static void commitAndClose() throws SQLException,
            ClassNotFoundException {
        Connection con = JdbcUtils.getConnection();
        if (con != null) {
            con.commit();
            con.close();
        }
    }
    public static void Close() throws SQLException,
    ClassNotFoundException {
        Connection con = JdbcUtils.getConnection();
        if (con != null) {
            con.close();
        }
    }

    // 提供关闭操作
    public static void closeConnection(Connection con) throws SQLException {
        if (con != null) {
            con.close();
        }
    }

    public static void closeStatement(Statement st) throws SQLException {
        if (st != null)
            st.close();
    }

    public static void closeResultSet(ResultSet rs) throws SQLException {
        if (rs != null)
            rs.close();
    }
}

2.简单jdbc工具类:


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

public class JdbcUtils {

    public static final String DRIVERCLASS;
    public static final String URL;
    public static final String USERNAME;
    public static final String PASSWORD;

    static {
        // 从配置文件中加载数据
        ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
        DRIVERCLASS = bundle.getString("driverClass");
        URL = bundle.getString("url");
        USERNAME = bundle.getString("username");
        PASSWORD = bundle.getString("password");
    }

    static {
        // 1.注册驱动
        try {
            Class.forName(DRIVERCLASS);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    public static Connection getConnection() throws ClassNotFoundException,
            SQLException {

        // 2.获取连接对象Connection
        Connection con = DriverManager.getConnection(URL, USERNAME, PASSWORD);

        return con;
    }

    // 提供关闭操作
    public static void closeConnection(Connection con) throws SQLException {
        if (con != null) {
            con.close();
        }
    }

    public static void closeStatement(Statement st) throws SQLException {
        if (st != null)
            st.close();
    }

    public static void closeResultSet(ResultSet rs) throws SQLException {
        if (rs != null)
            rs.close();
    }
}

3.配置文件,classPath根路径下:

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql:///day17
username=root
password=root
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值