I realize this was long ago answered but want to suggest an additional approach that avoids the nested try-with-resources double block.

public List<User> getUser(int userId) {
    try (Connection con = DriverManager.getConnection(myConnectionURL);
         PreparedStatement ps = createPreparedStatement(con, userId); 
         ResultSet rs = ps.executeQuery()) {

         // process the resultset here, all resources will be cleaned up

    } catch (SQLException e) {
        e.printStackTrace();
    }
}

private PreparedStatement createPreparedStatement(Connection con, int userId) throws SQLException {
    String sql = "SELECT id, username FROM users WHERE id = ?";
    PreparedStatement ps = con.prepareStatement(sql);
    ps.setInt(1, userId);
    return ps;
}

相关文章:

  • 2022-12-23
  • 2021-09-17
  • 2022-12-23
  • 2022-01-30
  • 2022-03-09
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-30
  • 2020-04-09
  • 2021-11-27
  • 2022-02-07
  • 2022-01-10
  • 2021-07-06
相关资源
相似解决方案