JDBC工具类优化代码

未优化前:

public class JDBCDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = null;
        Statement statement = null;
        
        try {
            //sql的操作
            String sql = "insert into account values('李青',5000)";
            //获取连接对象
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db5", "root", "clearlove7");
            //获取sql对象
            statement = connection.createStatement();
            //执行sql操作
            int count = statement.executeUpdate(sql);
            System.out.println(count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if(statement != null){
                statement.close();
            }
            if(connection != null){
                connection.close();
            }
        }
    }
}

优化后:

public class JDBCDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //注册驱动
        //Class.forName("com.mysql.jdbc.Driver");
        Connection connection = null;
        Statement statement = null;
        
        try {
            //sql的操作
            String sql = "insert into account values('李青',5000)";
            //获取连接对象
            //connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db5", "root", "clearlove7");
            connection = JDBCUtils.getConnection();
            //获取sql对象
            statement = connection.createStatement();
            //执行sql操作
            int count = statement.executeUpdate(sql);
            System.out.println(count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
/*            if(statement != null){
                statement.close();
            }
            if(connection != null){
                connection.close();
            }*/
            JDBCUtils.getClose(connection,statement,null);
        }
    }
}

JDBC工具类(用于简化代码)

public class JDBCUtils {
    private static String url;
    private static String user;
    private static String password;
    private static String driver;
    //配置文件只需加载一次,故用静态代码块
    static {
        Properties pro = new Properties();
        try {
            pro.load(new FileReader("Z:\\javaPro\\Jdbc\\out\\JDBCUtils.properties"));
            url = pro.getProperty("url");
            user = pro.getProperty("user");
            password = pro.getProperty("password");
            driver = pro.getProperty("driver");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //直接获得连接对象
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url,user,password);
    }
    //关闭资源
    public static void getClose(Connection conn , Statement state , ResultSet result) throws SQLException {
        if(conn != null){
            conn.close();
        }
        if(state != null){
            state.close();
        }
        if(result != null){
            result.close();
        }
    }
    
}

配置文件:

url=jdbc:mysql://localhost:3306/db5
user=root
password=clearlove7
driver=com.mysql.jdbc.Driver

你可能感兴趣的:(JDBC工具类优化代码)