【问题标题】:try with resources - parameterized PreparedStatements [duplicate]尝试使用资源 - 参数化 PreparedStatements [重复]
【发布时间】:2018-09-06 18:06:02
【问题描述】:

编辑 建议的答案是不可接受的,不能为我所拥有的每种方法声明其他方法,想象一下?。必须有一个嵌套的 try with resources 解决方案。

如何将 try 语句与带有参数的 prepare 语句一起使用?

我有以下两行的错误。

preparedStatement.setString(1, uuid);
preparedStatement.setString(2, emailAddress);

错误声明最终或有效最终变量被执行

public UserBean getUserDevices(final String emailAddress, final String uuid) throws SQLException {
    UserBean userBean = null;

    String query = "SELECT user.id, user.emailAddress, device.uuid, device.active, device.user_id FROM user " +
            "LEFT JOIN device ON user.id = device.user_id AND device.uuid = ? " +
            "WHERE user.emailAddress = ?";

    try (Connection connection = dataSource.getConnection();
         PreparedStatement preparedStatement = connection.prepareStatement(query);
         preparedStatement.setString(1, uuid);
         preparedStatement.setString(2, emailAddress);
         ResultSet resultSet = preparedStatement.executeQuery();

    ) {
        while(resultSet.next()) {
            if(userBean == null) { // initialize user once while iterating for devices
                userBean = new UserBean(resultSet.getInt("user.id"), resultSet.getString("user.emailAddress"), null);
            }
            if(resultSet.getString("device.uuid") != null) { // if device exists (it's a LEFT JOIN) then add
                userBean.getDevices().add(new DeviceBean(0, resultSet.getString("device.uuid"), resultSet.getBoolean("device.active"), resultSet.getInt("device.user_id")));
            }
        }
    }
    return userBean;
}

【问题讨论】:

  • 这两行代码应该在try块里面。不在 try 块前面的括号中。
  • 在实例化实现 Closeable 的类时使用 try-with-resources 的想法。将代码移到 try 块中
  • resultSet 和preparedStatements 都是可关闭的,我想利用try 资源自动关闭,有没有办法与内外部try 资源一起使用?
  • 我们都告诉了你如何继续。问题是什么?你试过吗?

标签: java prepared-statement


【解决方案1】:

使用inner try with resource,在external try with resource之后,初始化语句,然后使用inner try with resource作为resultSet。

public UserBean getUserDevices(final String emailAddress, final String uuid) throws SQLException {
    UserBean userBean = null;

    String query = "SELECT user.id, user.emailAddress, device.uuid, device.active, device.user_id FROM user " +
            "LEFT JOIN device ON user.id = device.user_id AND device.uuid = ? " +
            "WHERE user.emailAddress = ?";

    try (Connection connection = dataSource.getConnection();
         PreparedStatement preparedStatement = connection.prepareStatement(query);
    ) {
        preparedStatement.setString(1, uuid);
        preparedStatement.setString(2, emailAddress);

        try(ResultSet resultSet = preparedStatement.executeQuery();) {
            while(resultSet.next()) {
                if(userBean == null) { // initialize user once while iterating for devices
                    userBean = new UserBean(resultSet.getInt("user.id"), resultSet.getString("user.emailAddress"), null);
                }
                if(resultSet.getString("device.uuid") != null) { // if device exists (it's a LEFT JOIN) then add
                    userBean.getDevices().add(new DeviceBean(0, resultSet.getString("device.uuid"), resultSet.getBoolean("device.active"), resultSet.getInt("device.user_id")));
                }
            }
        }
    }
    return userBean;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多