- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个使用 JDBC 和 MySQL 的 DAO 类。我没有收到任何关于如何关闭标题中列出的项目的指示,但我读到这样做是一个很好的做法。现在我认为这应该在每个 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
接口(interface)(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;
}
关于java - 这两种方法中哪一种是处理使用 JDBC 的 DAO 类中关闭 ResultSet、PreparedStatement 和 Connection 的 try-catch 的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46326422/
我有一个简单的问题。我的代码中有一些内存泄漏,因为我认为这是因为 PreparedStatement 和 ResultSet 我想知道释放资源及其内存的最佳、安全和正确的方法是什么。 Prepared
package jdbcDemo; import java.sql.*; public class PreparedStatement { public static void mai
服务器端和客户端上的语句缓存是通过 PreparedStatement 的字符串呈现严格缓存还是其他什么?换句话说,如果两个不同的 PreparedStatement 是通过不同的方式创建的,但最终具
我对 C++ Mysql 连接器有“一点”问题。我想使用 PreparedStatement。我尝试了文档中的示例 http://dev.mysql.com/doc/refman/5.1/en/con
我已经将 JAVA 应用程序与 MySql 连接起来。当我编写 PreparedStatement ps = null ;然后显示导入包的两个选项。两个建议的包是:com.mysql.jdbc.Pre
PreparedStatement setString 取一个“null”(就像 String a = "null"),在 .addBatch 之后它被转换成一个普通的 null(就像 String
我正在使用 PreparedStatement 的 setString 方法在 sql 查询中设置开始日期和结束日期的值。 String sql = "..... " AND tableA.time
这个问题已经有答案了: Using Prepared Statements to set Table Name (8 个回答) 已关闭 5 年前。 我正在尝试使用准备好的语句创建一个新表,但收到错误
使用 SQL 的最佳解决方法是什么 IN具有 java.sql.PreparedStatement 实例的子句,由于 SQL 注入(inject)攻击安全问题,不支持多个值:1 ?占位符代表一个值,而
我正在开发一个使用PreparedStatement的java应用程序。 SELECT FIELD1, FIELD2 FROM MYTABLE WHERE (FIELD1='firstFieldVal
我正在使用PreparedStatements写入数据库,有时我会在日志中看到以下内容: 2015-02-02 15:44:14,601 WARN SQL Warning Code: 1292,
query = "SELECT * FROM POST_COMMENT WHERE Post_date_time= ? AND Post_User= !;"; query = query.r
有没有一种方法可以在 php 准备好的语句中设置字段名称,而不是仅仅设置那里的值,例如, $stmt = $mysqli->prepare("UPDATE movies SET filmName =
我对“同时使用 Java 和 SQL”这整个事情还很陌生,我似乎找不到解决这个问题的方法。我通过 Workbench 在服务器上创建了一个数据库,现在我正在使用它的 Netbeans 创建一个简单的应
我对这样的准备好的语句有疑问: select ... from ... where xy = ? and foo = ? and bla = ? 我可以为每个索引设置一个字符串吗?否则我不得不做 pr
我有一个多线程代码的工作版本,但是我对我的 PreparedStatement-wrapper 类是非线程安全的感到不满意。所以我决定在 ThreadLocal 中生成 PreparedStateme
当使用 JDBC 的 PreparedStatements 查询 Oracle 时,请考虑: String qry1 = "SELECT col1 FROM table1 WHERE rownum=?
使用 SQL 的最佳解决方法是什么 IN具有 java.sql.PreparedStatement 实例的子句,由于 SQL 注入(inject)攻击安全问题,不支持多个值:1 ?占位符代表一个值,而
使用 SQL 的最佳解决方法是什么 IN具有 java.sql.PreparedStatement 实例的子句,由于 SQL 注入(inject)攻击安全问题,不支持多个值:1 ?占位符代表一个值,而
使用 SQL 的最佳解决方法是什么 IN具有 java.sql.PreparedStatement 实例的子句,由于 SQL 注入(inject)攻击安全问题,不支持多个值:1 ?占位符代表一个值,而
我是一名优秀的程序员,十分优秀!