JDBCUtil模板

来源:互联网 发布:aws没有centos 编辑:IT博客网 时间:2019/10/18 01:33
package com.utils;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 JdbcUtil {    private static String user;    private static String url;    private static String password;    private static String driverClassName;    static {        ResourceBundle rb = ResourceBundle.getBundle("database");//database是database的基名,去掉后缀,所在src目录下的路径        driverClassName = rb.getString("driverClassName");        user = rb.getString("user");        url = rb.getString("url");        password = rb.getString("password");        try {            Class.forName(driverClassName);        } catch (ClassNotFoundException e) {            e.printStackTrace();        }    }    /**     * 取得Connection     */    public static Connection getConnection() throws SQLException {        Connection conn = DriverManager.getConnection(url, user, password);        return conn;    }    /**     * 关闭流     *      * @param conn     * @param rs     * @param stmt     */    public static void release(Connection conn, ResultSet rs, Statement stmt) {        if (rs != null) {            try {                rs.close();            } catch (SQLException e) {                e.printStackTrace();            }            rs = null;        }        if (stmt != null) {            try {                stmt.close();            } catch (SQLException e) {                e.printStackTrace();            }            stmt = null;        }        if (conn != null) {            try {                conn.close();            } catch (SQLException e) {                e.printStackTrace();            }            conn = null;        }    }}

这是database.properties文件内容
database.properties文件目录

0 0