首页 > 数据库技术 > 详细

JDBC工具类的提取

时间:2020-02-16 23:18:25      阅读:86      评论:0      收藏:0      [点我收藏+]
package JDBCUtils;

import java.sql.*;

public class JDBCUtils{
    private static final String driverClass;
    private static final String url;
    private static final String username;
    private static final String password;

    static{
        driverClass="com.mysql.jdbc.Driver";
        url="jdbc:mysql:///jdbc?serverTimezone=UTC";
        username="root";
        password="root";
    }

    public void loadDriver() throws ClassNotFoundException {
        Class.forName(driverClass);
    }
    public static Connection getConnection() throws SQLException {
        Connection conn = DriverManager.getConnection(url, username, password);
        return conn;
    }
    public static void release(Connection conn, Statement stat) {
        if (stat != null) {
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            stat = null;
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;

        }
    }
    public static void release(ResultSet rs,Connection conn, Statement stat) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs = null;

        }
        if (stat != null) {
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            stat = null;
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;

        }
    }
}

 

JDBC工具类的提取

原文:https://www.cnblogs.com/shouyaya/p/12319133.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!