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() {
}
@Override
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(",")) {
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);
}
}
}
Problem Statement:-
After executing the SELECT
sql query. Below are my two scenarios-
- 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 theresultSet
using thecolumns
from thecolumnsList
and start adding it incoldData
list of String. - 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 createdaddException
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.