JDBC基础工具类的抽取

JDBC基础工具类的抽取

v1版本

public class Demo1 {

    //将常用值设置成静态常量
    private static final String driverClass;
    private static final String url;
    private static final String username;
    private static final String password;

    static {
        driverClass = "com.mysql.cj.jdbc.Driver";
        url = "jdbc:mysql:///jdbctest";
        username = "root";
        password = "root";
    }


    //祖册驱动的方法
    public static void loadDriver() throws ClassNotFoundException {
        Class.forName(driverClass);
    }

    //获得链接
    public static Connection getConnection() throws SQLException, ClassNotFoundException {
        loadDriver();;
        Connection conn = DriverManager.getConnection(url,username,password);
        return conn;
    }

    //资源的释放
    public static void release(Statement stmt, Connection conn){

        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;
        }
    }

    public static void release(ResultSet rs, Statement stmt, Connection conn){
        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;
        }
    }
}

v2版本

首先将配置信息提取到 properties文件中

driverClass=com.mysql.cj.jdbc.Driver
url=jdbc:mysql:///jdbctest
username=root
password=root

然后在java代码中引入

public class Demo1 {

    //将常用值设置成静态常量
    private static final String driverClass;
    private static final String url;
    private static final String username;
    private static final String password;

    static {

        //加载属性文件并解析
        Properties props = new Properties();
        //获得输入流
        //类的加载器
        InputStream is = Demo1.class.getClassLoader().getResourceAsStream("jdbc.properties");
        try {
            props.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }

        driverClass = props.getProperty("driverClass");
        url = props.getProperty("url");
        username = props.getProperty("username");
        password = props.getProperty("password");

    }


    //祖册驱动的方法
    public static void loadDriver() throws ClassNotFoundException {
        Class.forName(driverClass);
    }

    //获得链接
    public static Connection getConnection() throws SQLException, ClassNotFoundException {
        loadDriver();;
        Connection conn = DriverManager.getConnection(url,username,password);
        return conn;
    }

    //资源的释放
    public static void release(Statement stmt, Connection conn){

        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;
        }
    }

    public static void release(ResultSet rs, Statement stmt, Connection conn){
        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;
        }
    }
}

该方法方便修改配置

你可能感兴趣的:(学习笔记,java,jdbc,mysql,java)