Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can i take result set in an two dimensional array.

Status
Not open for further replies.

brahman

Programmer
Apr 17, 2003
21
0
0
IN
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.

please help me out in this.

bye for now
amit
 
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.

Regards,

Alex
 
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

hope this helps..

ksolos
 
Ya i am doing the same but it is giving array index out of range error for the code i am giving.

----------------------------------------------------------

ArrayList ar=new ArrayList();
//ArrayList excelarray = new ArrayList();
leadhistory.setObjConnection(conbean.getConnection());


Vector lead_history = new Vector();
Vector lead_history_final = new Vector();
ArrayList arrlist=new ArrayList();

lead_history=leadhistory.populateVars();
int vectorsize=lead_history.size();
int[] track=new int[100];
String name=&quot;&quot;;
String templeadid=&quot;&quot;;
String description=&quot;&quot;;
String laststage=&quot;&quot;;
String currentTemp=&quot;&quot;;
String currentstage=&quot;&quot;;
String firstname=&quot;&quot;;
String ondate=&quot; ON &quot;;
String by=&quot; BY &quot;;
String putfinalvalue=&quot;&quot;;
int c,count=0;
for(int i=count;i<=vectorsize;i++){
c=0;
//out.println(&quot;i value&quot;+i);
StringTokenizer st=new StringTokenizer(lead_history.get(i).toString(),&quot;*&quot;);
String leadname=st.nextToken();
String leadid=st.nextToken();
String stageid=st.nextToken();
String stagename=st.nextToken();
String activitydate=st.nextToken();
String employee=st.nextToken();
String notes=st.nextToken();
out.println(&quot;check 2&quot;);
if(templeadid.equals(&quot;&quot;)){
templeadid=leadid;
}
for(int j=count;j<vectorsize;j++){
//out.println(&quot;j vaLUE&quot;+j);
StringTokenizer stk=new StringTokenizer(lead_history.get(j).toString(),&quot;*&quot;);
String sleadname=stk.nextToken();
String sleadid=stk.nextToken();
String sstageid=stk.nextToken();
String sstagename=stk.nextToken();
String sactivitydate=stk.nextToken();
String semployee=stk.nextToken();
String snotes=stk.nextToken();
if(leadname.equals(sleadname)){
c++;

}
else{
if(c==1){
name=leadname;
laststage=stageid;
currentstage=laststage;
description=notes+ondate+activitydate+by+employee;
}
else{
StringTokenizer stk1=new StringTokenizer(lead_history.get(i-1).toString(),&quot;*&quot;);
String sleadname1=stk1.nextToken();
String sleadid1=stk1.nextToken();
String sstageid1=stk1.nextToken();
String sstagename1=stk1.nextToken();
String sactivitydate1=stk1.nextToken();
String semployee1=stk1.nextToken();
String snotes1=stk1.nextToken();
description=snotes1+ondate+sactivitydate1+by+semployee1;
name=sleadname1;
laststage=sstageid1;
currentstage=stageid;
}
}
}
count=count+c;
track=count;
StringBuffer sb=new StringBuffer(&quot;&quot;);
sb.append(name + &quot;|&quot; + laststage + &quot;|&quot; + currentstage + &quot;|&quot; + description);
//System.out.println(&quot;sb..&quot;+sb.toString());
arrlist.add(sb.toString());
out.println(&quot;LEAD NAME &quot;+ i +&quot; &quot;+name+&quot; LAST &quot;+laststage+&quot; CURRENT &quot;+currentstage+&quot; desc &quot;+description);
%><br><%
//out.println(&quot;count&quot;+count+&quot;c&quot;+c);

//TO REFINE THE ARRAY.
}
for(int k=0;k<arrlist.size()+1;k++){
//out.println(track[k]);
if(track[k]!=track[k+1]){
//out.println(&quot;size&quot;+arrlist.get(k));%><br><%
}
}
----------------------------------------------------------
please guide me why is this error for.
 
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(&quot;a value is <&quot; + 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


bye for now
amit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top