I am working on a multithreaded project in which each thread will randomly find columns for that table and I will be using those columns in my SELECT sql query 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 List<String>.

Here columnsList will contains columns delimited by comma. For example-

col1, col2, col3

Below is my code.

class ReadTask implements Runnable {

public ReadTask() {


public run() {

  while ( < 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<String> colData = new ArrayList<String>(columnsList.split(",").length);
    boolean foundData = false;

    if (id >= startValidRange && id <= endValidRange) {

        if (resultSet.next()) {
            foundData = true;
            for (String column : columnsList.split(",")) {
            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;

        if(flagTerminate) {

Problem Statement:-

After executing the SELECT sql query. Below are my two scenarios-

  1. I need to see whether the id is between the valid range. If it is between the Valid Range then check whether resultSet has any data or not. If it has data then loop around the resultSet using the columns from the columnsList and start adding it in coldData list of String.
  2. else if id is not in the valid range then I need to check I am not getting any data back from the resultSet. 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 addException method.

Can anyone help me out whether the way I am doing here for my above two scenarios is right or not? It looks like, I can improve the if/else loop code more I guess for my above two scenario.


1 Answer 1


I am not that familiar with JDBC, but I think the method you search for is: isBeforeFirst: "true if the cursor is before the first row; false if the cursor is at any other position or the result set contains no rows"

This does not modify the results cursor then.

if (id >= startValidRange && id <= endValidRange)

I would change it to:

if (startValidRange <= id && id <= endValidRange)

Only a small change, but increase readability a lot (at least for me)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.