在使用 JDBC 的 DAO class 中关闭 ResultSet、PreparedStatement 和 Connection 时,这两种方法中哪一种是处理 try-catches 的最佳方法?
Which of these two is the best way to handle try-catches for closing ResultSet, PreparedStatement, and Connection in a DAO class that uses JDBC?
我正在创建一个使用 JDBC 和 MySQL 的 DAO class。我没有收到任何关于如何关闭标题中列出的项目的指示,但我读到这样做是很好的做法。现在我认为这应该在每个 CRUD 方法中完成,但是处理异常似乎有点人为,我不确定如何实现它。
第一个例子:
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;
}
在第二个例子中我需要重复的异常处理。第一个解决方案对我来说似乎更聪明,但我不确定它是否比第二个更可读。
设计中是否采用了不仅仅是主观的约定?
假设你使用的是Java 1.7及以上版本,你可以使用try with resources语句来简化关闭资源。如果资源实现了 AutoClosable
接口,即 Connection
和 PreparedStatement
的情况,您可以按如下方式重写代码:
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;
}
我正在创建一个使用 JDBC 和 MySQL 的 DAO class。我没有收到任何关于如何关闭标题中列出的项目的指示,但我读到这样做是很好的做法。现在我认为这应该在每个 CRUD 方法中完成,但是处理异常似乎有点人为,我不确定如何实现它。
第一个例子:
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;
}
在第二个例子中我需要重复的异常处理。第一个解决方案对我来说似乎更聪明,但我不确定它是否比第二个更可读。
设计中是否采用了不仅仅是主观的约定?
假设你使用的是Java 1.7及以上版本,你可以使用try with resources语句来简化关闭资源。如果资源实现了 AutoClosable
接口,即 Connection
和 PreparedStatement
的情况,您可以按如下方式重写代码:
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;
}