I want to save a result set into a two dimensional array and than later do some calculation using the array columns, i tried saving the resultset in an vector but than later it would give some complexities.
Brahman,
I was wondering why you would want a two dimensional array. Remember, Java is about classes, so you can construct one class that contains the information you want and it can also be returned in a methos as an object.
I completly understand why you want a two dimensional array for your resultset...doing it with a Vector..you can later move the data from a Vector to an Array. Depending on the JDBC support for your particular database, you may be able to get the number of records...sometimes you can't...using the vector will make your code a little more reusable.
Keep in mind that this is kind of dangereous, if you pull 50000 rows from the db then the vectors get el grande and you can use up loads of memory.
here's the code (from memory...maybe some syntax issues)
Vector data_vector = new Vector();
Vector row_vector = new Vector();
int num_columns = rslt.getMetaData().getColumnCount();
while (rslt.next()){
row_vector = new Vector(); //clear the row vector
//for each column add the value to row vector
for(int i=1; i<=num_columns; i++({
row_vector.addElement(rslt.getObject(i));
}
//add the row_vector to the data vector
data_vector.addElement(row_vector);
}
// each element of the data vector represents a row
// each element of row vector is a column
1. array indexoutofbound error means you are trying to access the array Object using index that is out of range. For instance,
int a [] = new int [2];
System.out.println("a value is <" + a[2]); //index of out b
since the index starts from 0, hence 0 to 1.
2.
an advise on using collection like vector:-
Vector is synchronized version of list. hence it's is slower than ArrayList ; although it also implements RandomAccess interface. if you don't need a lot of insertion/deletion, use ArrayList instead, since it implements the marker interface - randomAccess(SDK 1.4). It's fast!
Thanx a lot for the Support on this query of mine now i am able to solve the problem.
And am getting the desired output.
Thanx specially to alexs97,ksolos and maxpower1
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.