关闭方法中的数据库相关资源

时间:2014-12-17 20:22:41

标签: java mysql sql database resources

@Joeblade建议我将数据库连接逻辑移动到一个单独的方法,如果不是一个单独的类,因为用户界面类已经非常混乱了很多用户界面代码,所以添加数据库无法提供可读性代码。如果我需要,这也有助于重用db代码。我做的。但现在,我的问题是关闭资源。

static Connection getConnection() {
    return connection;
} 

public ResultSet Execute_Query(String queryIn) throws SQLException {
    ResultSet resSet = null;        
    try {               
        connection = TableWithBottomLine.getConnection();
        Statement stmt = null;
        stmt = connection.createStatement();
        resSet = stmt.executeQuery(queryIn);                    
    } catch (SQLException e) {
        e.printStackTrace();
    }               
    return resSet;
}

public String textForLabel() throws SQLException{

    List<Float> floatArrayList = new ArrayList<Float>();

        String query ="SELECT f.Flat, f.Mobile, f.Food, f.Alcohol, f.Transport, f.Outdoor, f.Pauls_stuff, f.Stuff FROM finance.fin f WHERE f.tbl_Date >= DATE_FORMAT( NOW( ) ,  '%Y-%m-10' ) + INTERVAL IF( DAY( NOW( ) ) >10, 0 , -1 ) MONTH AND f.tbl_Date <= CURDATE( ) ";

        try {               
            ResultSet rs = null;
            rs = Execute_Query(query);          

            while (rs.next()){
              floatArrayList.add(rs.getFloat("Flat"));
              floatArrayList.add(rs.getFloat("Mobile"));
              floatArrayList.add(rs.getFloat("Food"));
              floatArrayList.add(rs.getFloat("Alcohol"));
              floatArrayList.add(rs.getFloat("Transport"));
              floatArrayList.add(rs.getFloat("Outdoor"));
              floatArrayList.add(rs.getFloat("Pauls_stuff"));
              floatArrayList.add(rs.getFloat("Stuff"));
            }           
       } catch(SQLException ee){
           ee.printStackTrace();
         }
// Irrelevant stuff down below.....
  1. 我是否必须在Execute_Query()方法中关闭Connection,Statement,ResultSet并在textForLabel()方法中关闭ResultSet(仅限)?
  2. 如何使用try-with-resources功能完成关闭?

3 个答案:

答案 0 :(得分:1)

尝试这样的事情

} catch (SQLException e) {
   e.printStackTrace();
} finally {
   connection.close();
}

finally语句将确保'connection.close();'即使代码抛出异常,也会调用它。

答案 1 :(得分:1)

移动所有数据库逻辑,而不仅仅是连接建立代码。

List<Float> listMyStuff() {
    ArrayList<Float> list = new ArrayList<Float>();
    Connection con = null;
    Statement statement = null;
    ResultSet rs = null;

    try {
        con = DriverManager.getConnection(MY_DB_URL);
        statement = con.createStatement();
        rs = statement.executeQuery(MY_SQL);
        while(rs.next()) {
            list.add(rs.getFloat("Flat"));
            list.add(rs.getFloat("Mobile"));
            list.add(rs.getFloat("Food"));
            list.add(rs.getFloat("Alcohol"));
            list.add(rs.getFloat("Transport"));
            list.add(rs.getFloat("Outdoor"));
            list.add(rs.getFloat("Pauls_stuff"));
            list.add(rs.getFloat("Stuff"));            
        }
    }
    catch(SQLException e) {
        e.printStackTrace();
        //Do something else!?!?
    }
    finally {
        if(rs != null) { try{ rs.close(); }catch(Throwable t){} }
        if(statement != null) { try{ statement.close(); }catch(Throwable t){} }
        if(con != null) { try{ con.close(); }catch(Throwable t){} }
    }

    return list;
}

答案 2 :(得分:0)

如果您选择这样做,使用try with resources会更好。

首先获取您的连接详细信息并在try中准备语句。

      String query ="SELECT f.Flat, f.Mobile, f.Food, f.Alcohol, f.Transport, f.Outdoor, f.Pauls_stuff, f.Stuff FROM finance.fin f WHERE f.tbl_Date >= DATE_FORMAT( NOW( ) ,  '%Y-%m-10' ) + INTERVAL IF( DAY( NOW( ) ) >10, 0 , -1 ) MONTH AND f.tbl_Date <= CURDATE( ) ";
        try (Connection connection = DriverManager.getConnection(url);
    Statement stmt=connection.createStatement();) {
        try (ResultSet rs = stmt.executeQuery(query); ) {
                while(rs.next()) {
    floatArrayList.add(rs.getFloat("Flat"));
                  floatArrayList.add(rs.getFloat("Mobile"));
                  floatArrayList.add(rs.getFloat("Food"));
                  floatArrayList.add(rs.getFloat("Alcohol"));
                  floatArrayList.add(rs.getFloat("Transport"));
                  floatArrayList.add(rs.getFloat("Outdoor"));
                  floatArrayList.add(rs.getFloat("Pauls_stuff"));
                  floatArrayList.add(rs.getFloat("Stuff"));
                }           
           } 
}catch(SQLException ee){
               ee.printStackTrace();
    }