从 JDBC ResultSet 创建数组时出现 java.lang.ArrayIndexOutOfBoundsException
谁能告诉我我在这里做错了什么?我运行了大约 100 次,大约有 2-8 次失败并出现错误:java.lang.ArrayIndexOutOfBoundsException: 10
public String[][] queryResult(String QUERY) throws SQLException{
Connection con = getPoolConnection();
Statement st2 = con.createStatement();
ResultSet rs = st2.executeQuery(QUERY);
System.out.println("Run query: "+QUERY);
// outPrint(logFile,"Run query: "+QUERY+"");
ResultSetMetaData metaData = rs.getMetaData();
int noOfColumns = metaData.getColumnCount();
//System.out.print(metaData.toString()+" : ");
this.columnName = new String[noOfColumns];
this.columnTypes = new String[noOfColumns];
for (int i = 1; i <= noOfColumns; i++) {
this.columnName[i-1] = metaData.getColumnLabel(i);
this.columnTypes[i-1] = metaData.getColumnTypeName(i);
System.out.print(this.columnName[i-1]+" - "+this.columnTypes[i-1]+" , ");
}
System.out.println(" - ");
rs.last();
int noOfRows = rs.getRow();
rs.beforeFirst();
String[][] result = new String[noOfRows][noOfColumns];
int loop = 0;
while(rs.next()) {
loop++;
for (int i = 1; i <= noOfColumns; i++) {
result[loop-1][i-1] = getResultSwitch(metaData.getColumnType(i), rs, i);
//System.out.println("result "+this.columnTypes[i-1]+" : "+result[loop-1][i-1]);
System.out.print(result[loop-1][i-1]+" , ");
}
System.out.println(" - ");
}
rs.close();
st2.close();
con.close();
//System.out.println("Closed connection.");
return result;
}
Can anyone tell me what I am doing wrong here? I run this about 100 times, and about 2-8 times it fails with error: java.lang.ArrayIndexOutOfBoundsException: 10
public String[][] queryResult(String QUERY) throws SQLException{
Connection con = getPoolConnection();
Statement st2 = con.createStatement();
ResultSet rs = st2.executeQuery(QUERY);
System.out.println("Run query: "+QUERY);
// outPrint(logFile,"Run query: "+QUERY+"");
ResultSetMetaData metaData = rs.getMetaData();
int noOfColumns = metaData.getColumnCount();
//System.out.print(metaData.toString()+" : ");
this.columnName = new String[noOfColumns];
this.columnTypes = new String[noOfColumns];
for (int i = 1; i <= noOfColumns; i++) {
this.columnName[i-1] = metaData.getColumnLabel(i);
this.columnTypes[i-1] = metaData.getColumnTypeName(i);
System.out.print(this.columnName[i-1]+" - "+this.columnTypes[i-1]+" , ");
}
System.out.println(" - ");
rs.last();
int noOfRows = rs.getRow();
rs.beforeFirst();
String[][] result = new String[noOfRows][noOfColumns];
int loop = 0;
while(rs.next()) {
loop++;
for (int i = 1; i <= noOfColumns; i++) {
result[loop-1][i-1] = getResultSwitch(metaData.getColumnType(i), rs, i);
//System.out.println("result "+this.columnTypes[i-1]+" : "+result[loop-1][i-1]);
System.out.print(result[loop-1][i-1]+" , ");
}
System.out.println(" - ");
}
rs.close();
st2.close();
con.close();
//System.out.println("Closed connection.");
return result;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您创建一个结果行的 ArrayList,然后将其转换,而不是使用 rs.last() 和 rs.getRow() 来创建静态数组完成后放入数组中。
Instead of using
rs.last()
andrs.getRow()
to make a static array, I would suggest instead that you make an ArrayList of result rows, and convert it into an array when you're done.虽然我不知道哪一行代码抛出此异常,但我在代码中至少看到一个问题:
loop-1 在第一次迭代时为 -1,因为loop = 0
Although I do not know which line of code throws this exception I see at least one problem in your code:
loop-1 is -1 on first iteration because loop = 0