I am getting the values from database of a particular table and adding the values to Array list, the values are added but while adding the last value , IndexOutOfBound Exception is thrown. The sample output has been attached for the reference and the code as well.
**Code:** ----------
try {
statement = conn.createStatement();
String exten = ".dbo.";
String countquery = "select COUNT(" + "" + field + "" + ") from " + "[" + database + "]" + "" + exten + ""+ "[" + table + "]";
String query = "select " + "" + field + "" + " from " + "[" + database + "]" + "" + exten + "" + "[" + table+ "]";
ResultSet countrs = statement.executeQuery(countquery);
while (countrs.next()) {
System.out.println(countrs.getString(1));
count = Integer.parseInt(countrs.getString(1));
}
ArrayList<String> records = new ArrayList<String>(size);
System.out.println("HIHIHI" + query);
ResultSet rs = statement.executeQuery(query);
rs.next();
for (int j = 0; j < count; j++) {
System.out.println(j + rs.getString(field) + records.size());
records.add(rs.getString(field));
System.out.println(records.get(j));
rs.next();
}
return records;
} catch (ArrayIndexOutOfBoundsException exception) {
exception.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
**Exception:**
333Luke 333
Luke
334JOHN 334
JOHN
335lotto 335
lotto
336Jasonhhhhhhhhhhhhhhhhhhhhhhhhh336
Jasonhhhhhhhhhhhhhhhhhhhhhhhhh
337Pamela 337
Pamela
338John 338
[main] INFO net.serenitybdd.core.Serenity - STEP ERROR: java.lang.IndexOutOfBoundsException: Index: 338, Size: 338
[main] INFO net.serenitybdd.core.Serenity - STEP ERROR: java.lang.IndexOutOfBoundsException: Index: 338, Size: 338
[main] INFO net.serenitybdd.core.Serenity - FINISHING STEP
[main] ERROR net.serenitybdd.core.Serenity -
__ _____ _____ ____ _____ _____ _ ___ _ _____ ____
_ / / |_ _| ____/ ___|_ _| | ___/ \ |_ _| | | ____| _ \
(_)_____| | | | | _| \___ \ | | | |_ / _ \ | || | | _| | | | |
_|_____| | | | | |___ ___) || | | _/ ___ \ | || |___| |___| |_| |
(_) | | |_| |_____|____/ |_| |_|/_/ \_\___|_____|_____|____/
\_\
TEST FAILED WITH ERROR: DB check
---------------------------------------------------------------------
[main] ERROR net.serenitybdd.core.Serenity - TEST FAILED AT STEP DB retrievelist: FIRSTNAME, Guru, EMPLOYEE
[main] ERROR net.serenitybdd.core.Serenity - Index: 338, Size: 338
[31mFailed scenarios:[0m
[31mdebug.feature:46 [0m# Scenario Outline: DBCheck
4 Scenarios ([31m1 failed[0m, [32m3 passed[0m)
26 Steps ([31m1 failed[0m, [32m25 passed[0m)
1m49.097s
java.lang.IndexOutOfBoundsException: Index: 338, Size: 338
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at net.thucydides.showcase.cucumber.webdriverclasses.SQLSupportClass.retrievelist(SQLSupportClass.java:41)
at net.thucydides.showcase.cucumber.steps.serenity.CommonSteps.DBRetrievelist(CommonSteps.java:84)
at net.thucydides.showcase.cucumber.steps.serenity.CommonSteps$$EnhancerByCGLIB$$402c7b7f.CGLIB$DBRetrievelist$23(<generated>)
at net.thucydides.showcase.cucumber.steps.serenity.CommonSteps$$EnhancerByCGLIB$$402c7b7f$$FastClassByCGLIB$$c7f4d865.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at net.thucydides.core.steps.StepInterceptor.invokeMethod(StepInterceptor.java:372)
at net.thucydides.core.steps.StepInterceptor.executeTestStepMethod(StepInterceptor.java:357)
at net.thucydides.core.steps.StepInterceptor.runTestStep(StepInterceptor.java:332)
at net.thucydides.core.steps.StepInterceptor.testStepResult(StepInterceptor.java:134)
at net.thucydides.core.steps.StepInterceptor.intercept(StepInterceptor.java:61)
at
You have one too many rs.next() statements.
Replace these two rs.next()
calls:
rs.next();
for (int j = 0; j < count; j++) {
System.out.println(j + rs.getString(field) + records.size());
records.add(rs.getString(field));
System.out.println(records.get(j));
rs.next();
}
with this for minimal changes:
for (int j = 0; j < count; j++) {
rs.next();
System.out.println(j + rs.getString(field) + records.size());
records.add(rs.getString(field));
System.out.println(records.get(j));
}
But a better solution would be to check the result of the next()
call , and make use of it in a while loop. No real need for the count either then.
while (rs.next()) {
records.add(rs.getString(field));
}
And skip the whole countQuery thing, and ignore the initial size to the ArrayList (or if you know it will always be at least a couple of hundred, set it to for example 500). It will grow if needed anyway. Changing from two queries to one will probably be a lot better than any ArrayList resize operations anyway so might as well ignore it.
you can minimize the code using one query. TRY IT:
public static void main(String[] args) {
try {
statement = conn.createStatement();
String exten = ".dbo.";
String query = "select " + "" + field + "" + " from " + "[" + database + "]" + "" + exten + "" + "[" + table + "]";
ResultSet rs = statement.executeQuery(query);
//COUNT THE ROWS USING RESULTSET
count =getCountRows(rs);
ArrayList<String> records = new ArrayList<>();
while (rs.next()) {
records.add(rs.getString(field));
System.out.println(records.get(records.size()-1));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static int getCountRows(ResultSet resultSet) {
int size = 0;
try {
resultSet.last();
size = resultSet.getRow();
resultSet.beforeFirst();
} catch (Exception ex) {
return -1;
}
return size;
}
Keep only one query statement and one rs.next() as below.
class DbResults {
private String name;
private Integer count;
public DbResults(String name,Integer count){
this.name = name;
this.count = count;
}
//write the getter and setter
}
String query = "select " + COUNT(" + "" + field + "" + ") as count"" + field + "" + " from " + "[" + database + "]" + "" + exten + "" + "[" + table+ "] group by " + "field ";
ArrayList<DbResults> records = new ArrayList<>();
ResultSet rs = statement.executeQuery(query);
while(rs.next()){
DbResults eachRecord = new DbResults (rs.getString("count"),rs.getString(field);
records.add(record);
}
//use a for loop or stream to iterate through the records.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.