title: Iterate over result set keys - is it possible?
body: <p>I have a method that takes a database connection, query, and parameters and parses that query into a result set object. This is great but the problem is to get each value out of a result set I have to write one line of code for every row of data I am pulling to then save it in a JSON container. Is there a way to do this systematically so I can automatically parse the data type and create the JSON object based upon the keys fetched from the result set w/o manually specifying the keys?</p>
  
  <pre><code>public static JSONArray q2rs2j(Connection connection, String query, List&lt;String&gt; params) throws Exception {
      JSONArray tContainer = new JSONArray();
      PreparedStatement pStatement = connection.prepareStatement(query);
      int pit = 1;
      if(params != null) {
          for (String param : params) {
              try {
                  double paramAsDouble = Double.parseDouble(param);
                  try {
                      int paramAsInt = Integer.parseInt(param);
                      pStatement.setInt(pit, paramAsInt);
                  } catch (NumberFormatException e) {
                      pStatement.setDouble(pit, paramAsDouble);
                  }
              } catch (NumberFormatException e) {
                  pStatement.setString(pit, param);
              }
              pit++;
          }
      }
      ResultSet resultSet = pStatement.executeQuery();
      try {
          while (resultSet.next()) {
              // Iterate through KEYS in the resultSet.next() row
              while (hasKey) {
                  // Store key Name and key Value in variables - todo: determine data type via try parsing as Int, double, etc
                  String thisKeyName = (nextKeyName);
                  String thisKeyValue = (nextKeyValue);
                  JSONObject tObject = new JSONObject();
                  tObject
                      .put(nextKeyName, nextKeyValue);
              }
              tContainer.put(tObject);
          }
          resultSet.close();
      } catch (Exception e) { e.printStackTrace(); }
      return tContainer;
  }
  </code></pre>