昨天,Stack上的许多人推荐使用try-with-resources。我现在正在为我的所有数据库操作执行此操作。今天,我想将语句更改为PreparedStatement,以使查询更安全。但是,当我尝试在try-with-resources中使用准备好的语句时,我总是得到像'identifier expected‘、';’或')‘这样的错误。
我做错了什么?或者这是不可能的?这是我的代码:
try (Connection conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
PreparedStatement stmt = conn.prepareStatement("SELECT id FROM users WHERE id = ? LIMIT 1");
stmt.setInt(1, user);
ResultSet rs = stmt.executeQuery()) {
// if no record found
if(!rs.isBeforeFirst()) {
return false;
}
// if record found
else {
return true;
}
} catch (SQLException e) {
// log error but dont do anything, maybe later
String error = "SQLException: " + e.getMessage() + "\nSQLState: " + e.getSQLState() + "\nVendorError: " + e.getErrorCode();
return false;
}发布于 2014-07-24 19:31:55
try-with-resource语句用于声明(Autoclosable)资源。Connection、PreparedStatement和ResultSet都是Autoclosable,所以这没问题。
但是stmt.setInt(1, user)不是一个资源,而是一个简单的语句。在try-with-resource语句中不能有简单的语句(没有资源声明)!
解决方案:创建多个try-with-resource语句!
try (Connection conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS)) {
executeStatement(conn);
} catch (SQLException e) {
// log error but dont do anything, maybe later
String error = "SQLException: " + e.getMessage() + "\nSQLState: " + e.getSQLState() + "\nVendorError: " + e.getErrorCode();
return false;
}
private void executeStatement(Connection con) throws SQLException {
try (PreparedStatement stmt = conn.prepareStatement("SELECT id FROM users WHERE id=? LIMIT 1")) {
stmt.setInt(1, user);
try (ResultSet rs = stmt.executeQuery()) {
// process result
}
}
}(请注意,从技术上讲,不需要像我那样将SQL语句的执行放在单独的方法中。如果打开连接和创建资源都在同一个try-with- PreparedStatement语句中,那么它也是有效的。我只是认为将连接管理内容与其余代码分开是一种很好的做法)。
发布于 2014-07-24 19:23:37
尝试以下代码:
try (Connection conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS)) {
PreparedStatement stmt = conn.prepareStatement("SELECT id FROM users WHERE id = ? LIMIT 1");
stmt.setInt(1, user);
ResultSet rs = pstmt.executeQuery())
// if no record found
if(!rs.isBeforeFirst()) {
return false;
}
// if record found
else {
return true;
}
} catch (SQLException e) {
// log error but dont do anything, maybe later
String error = "SQLException: " + e.getMessage() + "\nSQLState: " + e.getSQLState() + "\nVendorError: " + e.getErrorCode();
return false;
}注意,在这里,资源是您的连接,您必须在try块()中使用它
发布于 2014-07-24 19:24:31
移动
stmt.setInt(1, user);
ResultSet rs = stmt.executeQuery()...within the try{ /*HERE*/ }
这是因为stmt是正在创建的要使用的资源try (/*HERE*/) {} try{ /*HERE*/ }
Try-with-resources
try (/*Create resources in here such as conn and stmt*/)
{
//Use the resources created above such as stmt
}重点是在资源创建块implements AutoClosable中创建的所有内容,当退出try块时,将对它们调用close()。在您的代码中,stmt.setInt(1, user);不是AutoCloseable资源,因此出现了问题。
https://stackoverflow.com/questions/24932262
复制相似问题