Click to See Complete Forum and Search --> : Vector of Vectors: "Column Index out of range ( 0 > 11)"


javaQQ
January 23rd, 2003, 02:40 PM
How do I load the contents of a JDBC ResultSet into a Vector of Vectors?

This approach returns an error message:


theStmt = con.createStatement();
ResultSet tQ = theStmt.executeQuery( theQuery );

Vector theDataVector = new Vector();
Vector theRowVector;

while ( tQ.next() ) {

theRowVector = new Vector();

theRowVector.setElementAt( ricq.getString( "field1" ), 0 ); // field1
theRowVector.setElementAt( ricq.getString( "field2" ), 1 ); // field2
theRowVector.setElementAt( ricq.getString( "field3" ), 2 ); // field3
...
theRowVector.setElementAt( ricq.getString( "fieldX" ), 10 ); // fieldX

theDataVector.addElement( theRowVector );

}

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.setElementAt(Vector.java:482)
at ReaderJobUPDATE_PS_2.main(ReaderJobUPDATE_PS_2.java:531)


This code returns another error message:


theStmt = con.createStatement();
ResultSet tQ = riConfirmStmt.executeQuery( theQuery );

while ( tQ.next() ) {
for( int i = 0; i < 20; i++ ) {
for( int j = 0; j < 11; j++ ) {
((Vector)theDataVector.elementAt(i)).setElementAt( tQ.getString(i), j ); // pop_id
}
}
}

//SQLException: Column Index out of range ( 0 > 11).
//Transaction is being rolled back
//SQLException: Can't call commit when autocommit=true


What do "0 >= 0" and especially "0 > 11" (!!) refer to, and how do I fix them?

Many thanks in advance.

Goodz13
January 23rd, 2003, 03:53 PM
Your using setElementAt(...) try using addElement

and this here


while ( tQ.next() ) {
for( int i = 0; i < 20; i++ ) {
for( int j = 0; j < 11; j++ ) {
((Vector)theDataVector.elementAt(i)).setElementAt( tQ.getString(i), j ); // pop_id
}
}
}


try

while ( tQ.next() ) {
for( int i = 0; i < theDataVector.size(); i++ ) {
for( int j = 0; j < ((Vector)theDataVector.elementAt(i)).size(); j++ ) {
((Vector)theDataVector.elementAt(i)).setElementAt( tQ.getString(i), j ); // pop_id
}
}
}

javaQQ
January 26th, 2003, 12:29 PM
Last week I posted a two-part question on this problem

Even after trying the suggestions that I received, I still have a the problem which I will describe below.

I want to load the contents of a JDBC ResultSet into an EXISTING Vector of Vectors.

This code builds the Vector of Vectors:

Vector theDataVector = new Vector();
Vector theRowVector;

for( int i = 0; i < 20; i++ ) {
theRowVector = new Vector();
for( int j = 0; j < 11; j++ ) {
theRowVector.addElement( i + "/" + j );
}
theDataVector.addElement( theRowVector );
}

...

The query statement:

String bringBackSeqsQry = " SELECT * FROM Sequences WHERE job_id = '222 - A - 006' ";
...

This is supposed to load the ResultSet into the Vector:

Statement theStmt = con.createStatement();
ResultSet tQ = theStmt.executeQuery( theQry );

while ( tQ.next() ) {
for( int i = 0; i < theDataVector.size(); i++ ) {
for( int j = 1; j <= theDataVector.size(); j++ ) {
((Vector)theDataVector.elementAt(i)).setElementAt( tQ.getString(i), j ); // pop_id
}
}
}

However, it returns this error message:

SQLException: Column Index out of range ( 0 > 11).
Transaction is being rolled back
SQLException: Can't call commit when autocommit=true

There ARE eleven columns in the table and the ResultSet.

What is the point of the nutjob error message?

Many thanks in advance.

Goodz13
January 26th, 2003, 01:44 PM
I made a mistake in my previous post.

ResultSets are 1 based. The error message that you are getting is a little misleading. "Column Index out of range ( 0 > 11)."
It actually should be 0 < 1.

for the first for loop you could use

for (int i=1; i <= 11; i++)

if you know how many Columns are in the ResultSet or you can use ResultSetMetaData to get the Columns count.

the code would be


ResultSetMetaData rsmd = myResultset.getMetaData();
int iColCount = rsmd.getColumnCount();


myResultset would be the tQ object

Sorry about the mistake.

javaQQ
January 26th, 2003, 02:10 PM
Hi, Goodz13;

Thanks for the attention to my problem.
Here Is some code that I got from another forum which solves the problem:

int numCols = tQ.getMetaData().getColumnCount();
int rowIndex = 0;
while ( tQ.next() ) {
Vector row = (Vector)theDataVector.elementAt(rowIndex);
for(int i = 1;i <= numCols;i++){
row.setElementAt( tQ.getString(i), i - 1 );
}
rowIndex++;
}

Again, many thanks. (And sorry about that re-post of my original message--If it was my fault, it was inadvertent)

Goodz13
January 26th, 2003, 04:42 PM
Don't worry about the repost, I just figured that it would be easier for someone to answer if they could see the whole problem, that's why I combined the threads.