CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2001
    Posts
    140

    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.

  2. #2
    Join Date
    Jan 2002
    Location
    Halifax, NS, Canada
    Posts
    985
    Your using setElementAt(...) try using addElement

    and this here
    Code:
    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
    Code:
    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
          }
       }
    }
    Last edited by Goodz13; January 23rd, 2003 at 04:56 PM.

  3. #3
    Join Date
    Apr 2001
    Posts
    140

    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.

  4. #4
    Join Date
    Jan 2002
    Location
    Halifax, NS, Canada
    Posts
    985
    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
    Code:
    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

    Code:
    ResultSetMetaData rsmd = myResultset.getMetaData();
    int iColCount = rsmd.getColumnCount();
    myResultset would be the tQ object

    Sorry about the mistake.
    Last edited by Goodz13; January 26th, 2003 at 02:46 PM.

  5. #5
    Join Date
    Apr 2001
    Posts
    140

    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)

  6. #6
    Join Date
    Jan 2002
    Location
    Halifax, NS, Canada
    Posts
    985
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured