JDBC工具类的提取

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";
     try{
loadDriver();
     }catch(ClassNotFoundException e){
       e.printStackTrace();
     }
}
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; } } }

 

try {
loadDriver();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
posted @ 2020-02-16 22:21  shouyaya  阅读(268)  评论(0编辑  收藏  举报