我从特定表的数据库中获取值并将值添加到Array列表中,添加了值,但在添加最后一个值时,抛出了IndexOutOfBound异常。已附加样本输出以供参考和代码使用。
**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
答案 0 :(得分:0)
你有一个太多的rs.next()语句。
替换这两个rs.next()
来电:
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();
}
用这个进行最小的改动:
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));
}
但更好的解决方案是检查next()
调用的结果,并在while循环中使用它。当时也不需要计算。
while (rs.next()) {
records.add(rs.getString(field));
}
跳过整个countQuery的事情,忽略ArrayList的初始大小(或者如果你知道它总是至少几百,将它设置为例如500)。无论如何,它会增长。从两个查询更改为一个查询可能比任何ArrayList调整大小操作要好得多,所以不妨忽略它。
答案 1 :(得分:0)
您可以使用一个查询最小化代码。试试吧:
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;
}
答案 2 :(得分:0)
只保留一个查询语句和一个rs.next(),如下所示。
io.connect('https://test16793.herokuapp.com/');