title: Loop through the ResultSet in an efficient way and add column values to List<String>
body: <p>I am working on a multithreaded project in which each thread will <code>randomly find columns for that table</code> and I will be using those columns in my <code>SELECT sql query</code> and then I will be executing that SELECT sql query. AFter exectuing that query, I will be looping through the result set and will add the data for each columns into <code>List&lt;String&gt;</code>.</p>
  
  <p>Here <code>columnsList</code> will contains <code>columns</code> delimited by <code>comma</code>. For example-</p>
  
  <p><code>col1, col2, col3</code></p>
  
  <p>Below is my code. </p>
  
  <pre><code>class ReadTask implements Runnable {
  
  public ReadTask() {
  
  }
  
  
  @Override
  public run() {
    ...
  
    while ( &lt; 60 minutes) {
  
      .....
  
      final int id = generateRandomId(random);
      final String columnsList = getColumns(table.getColumns());
      final String selectSql = "SELECT " + columnsList + "  from " + table.getTableName() + " where id = ?";
      resultSet = preparedStatement.executeQuery();
  
      List&lt;String&gt; colData = new ArrayList&lt;String&gt;(columnsList.split(",").length);
      boolean foundData = false;
  
      if (id &gt;= startValidRange &amp;&amp; id &lt;= endValidRange) {
  
          if (resultSet.next()) {
              foundData = true;
              for (String column : columnsList.split(",")) {
                  colData.add(resultSet.getString(column.trim()));
              }
              resultSet.next();//do I need this here?
          }
      } else if (resultSet.next()) {
          addException("Data Present for Non Valid ID's", Read.flagTerminate);
      }
  
      ....
        }
     }
  
  
      private static void addException(String cause, boolean flagTerminate) {
          AtomicInteger count = exceptionMap.get(cause);
          if (count == null) {
              count = new AtomicInteger();
              AtomicInteger curCount = exceptionMap.putIfAbsent(cause, count);
              if (curCount != null) {
                  count = curCount;
              }
          }
          count.incrementAndGet();
  
          if(flagTerminate) {
              System.exit(1);
          }
      }
  }
  </code></pre>
  
  <p><strong>Problem Statement:-</strong></p>
  
  <p>After executing the <code>SELECT</code> sql query. Below are my two scenarios-</p>
  
  <ol>
  <li>I need to see whether the id is between the valid range. If it is between the Valid Range then check whether <code>resultSet</code> has any data or not. If it has data then loop around the <code>resultSet</code> using the <code>columns</code> from the <code>columnsList</code> and start adding it in <code>coldData</code> list of String.</li>
  <li>else if id is not in the valid range then I need to check I am not getting any data back from the <code>resultSet</code>. But somehow if I am getting the data back and flag is true to stop the program, then exit the program. Else if I am getting the data back but flag is false to stop the program, then count how many of those happening. So for this, I have created <code>addException</code> method.</li>
  </ol>
  
  <p>Can anyone help me out whether the way I am doing here for my above two <code>scenarios</code> is right or not? It looks like, I can improve the <code>if/else loop</code> code more I guess for my above two scenario.</p>