title: How to correctly get the number of columns from a JDBC query
body: <p>I have this code that converts a JDBC result set into a list of hash tables:</p>
  
  <pre><code>private static ArrayList&lt;HashMap&lt;String, Object&gt;&gt; convertResultSet(ResultSet resultSet) throws SQLException {
      ArrayList&lt;HashMap&lt;String, Object&gt;&gt; map = new ArrayList&lt;HashMap&lt;String, Object&gt;&gt;();
      ResultSetMetaData rsmd = resultSet.getMetaData();
      int nColumns = rsmd.getColumnCount();
      while(resultSet.next()) {
          HashMap&lt;String, Object&gt; hash = new HashMap&lt;String, Object&gt;();
          for (int i = 0; i &lt; nColumns; i++)
          {
              hash.put(rsmd.getColumnName(i), resultSet.getObject(i));
          }
          map.add(hash);
      }
      return map;
  }
  </code></pre>
  
  <p>but when run, it throws a "Column index out of range." exception.</p>
  
  <p>Any ideas?</p>