Loading a JDBC ResultSet into a Vector of Vectors
How do I load the contents of a JDBC ResultSet into a Vector of Vectors?
This approach returns an error message:
Code:
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:
Code:
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.
Vector of Vectors: "Column Index out of range ( 0 > 11)"
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:
Code:
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:
Code:
String bringBackSeqsQry = " SELECT * FROM Sequences WHERE job_id = '222 - A - 006' ";
...
This is supposed to load the ResultSet into the Vector:
Code:
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.
Loading a JDBC ResultSet into a Vector of Vectors
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)