title: Efficient way to Handle ResultSet in Java
body: <p>I'm using a ResultSet in Java, and am not sure how to properly close it. I'm considering using the ResultSet to construct a HashMap and then closing the ResultSet after that. Is this HashMap technique efficient, or are there more efficient ways of handling this situation? I need both keys and values, so using a HashMap seemed like a logical choice.</p>
  
  <p>If using a HashMap is the most efficient method, how do I construct and use the HashMap in my code?</p>
  
  <p>Here's what I've tried:</p>
  
  <pre><code>public HashMap resultSetToHashMap(ResultSet rs) throws SQLException {
  
    ResultSetMetaData md = rs.getMetaData();
    int columns = md.getColumnCount();
    HashMap row = new HashMap();
    while (rs.next()) {
       for (int i = 1; i &lt;= columns; i++) {
         row.put(md.getColumnName(i), rs.getObject(i));
       }
    }
    return row;
  }
  </code></pre>