2017-09-20 88 views
0

我正在创建一个使用JDBC和MySQL的DAO类。我还没有收到关于如何关闭标题中列出的项目的任何迹象,但我认为这是一个很好的做法。现在我认为这应该在每个CRUD方法中完成,但处理异常似乎有点人为,我不确定如何实现它。在使用JDBC的DAO类中,这两种方法中的哪一种处理尝试捕获以关闭ResultSet,PreparedStatement和Connection的最佳方法?

第一个例子:

public boolean update2(Dto dto) { 
    assert dto != null; 
    if (readById(dto.getId()).getId() == 0) { 
     throw new RuntimeException("Row with this id doesn't exist"); 
    } 
    boolean flag = false; 
    try { 
     Connection connection = DAOFactory.createConnection(); 
     String sql = "SQL statement"; 
     try { 
      PreparedStatement ps = connection.prepareStatement(sql); 
      try { 
       // Some stuff with preparedstatement 
       ps.executeUpdate(); 
       flag = true; 
      } finally { 
       if (ps != null) ps.close(); 
      } 
     } finally { 
      if (connection != null) connection.close(); 
     } 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 

    return flag; 
} 

第二个例子:

public boolean update(Dto dto) { 
    assert dto != null; 
    if (readById(dto.getId()).getId() == 0) { 
     throw new RuntimeException("Row with this id doesn't exist"); 
    } 
    boolean flag = false; 
    PreparedStatement ps = null; 
    Connection connection = null; 
    try { 
     connection = DAOFactory.createConnection(); 
     String sql = "SQL statement"; 
     ps = connection.prepareStatement(sql); 
     // Some stuff with preparedstatement 
     ps.executeUpdate(); 
     flag = true; 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } finally { 
     if (ps != null) { 
      try { 
       ps.close(); 
      } catch (SQLException e) { 
       e.printStackTrace(); 
      } 
     } 
     if (connection != null) { 
      try { 
       connection.close(); 
      } catch (SQLException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    return flag; 
} 

在第二个例子中,我需要重复的异常处理。第一个解决方案对我来说似乎更聪明,但我不确定它比第二个解决方案更具可读性。

设计中是否会采用不仅仅是主观的约定?

回答

1

假设您使用Java 1.7及更高版本,则可以使用try with resources语句来简化资源关闭。提供的资源实现AutoClosable接口,这是ConnectionPreparedStatement的情况下,你可以重写你的代码如下:

public boolean update2(String dto) { 
    assert dto != null; 

    if (readById(dto.getId()).getId() == 0) { 
     throw new RuntimeException("Row with this id doesn't exist"); 
    } 

    boolean flag = false; 
    String sql = "SQL statement"; 
    try (Connection connection = DAOFactory.createConnection(); 
     PreparedStatement ps = connection.prepareStatement(sql)) { 
     ps.executeUpdate(); 
     flag = true; 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 

    return flag; 
} 
+0

此结构是更可读,我不知道,我非常感谢你。 但是,我不明白,对于1.7以前的版本,选择落在开发者身上还是需要采用惯例。 – TheItalianJobless

+0

然后使用第二个选项;这是我更喜欢的,因为代码比第一个更清洁。 – ujulu

+0

这是一个很好的答案,但我会更积极地处理'ClassNotFoundException',也许还会''SQLException'。将它们封装在运行时异常中,因为这些几乎总是由不能恢复的部署/编程错误(或数据/数据库问题)引起的。 –

相关问题