This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JDBC and the fly likes how to get data from a db into an array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to get data from a db into an array?" Watch "how to get data from a db into an array?" New topic
Author

how to get data from a db into an array?

Kameswari Jyosyula
Ranch Hand

Joined: Feb 20, 2001
Posts: 39
Hi, I am trying to get data from a db into my jsp page as an array.How do i do that?
I am calling the returned value from my method.But if i give the return type as String only a single value is returned.
I tried to use array.
if i say

i get an error (obviously for String [] and String are incompatible)I get the sam incompatible error if i use Array[] and getArray().So how to return the string type column(x) as an array?
Plz help
TIA

[This message has been edited by Kameswari Jyosyula (edited August 14, 2001).]
Vishakha Ahuja
Ranch Hand

Joined: Sep 13, 2000
Posts: 191
Since you have defined a while loop, why don't you use an index for array :
int a = 0;
while(rs.next()) {
stringArray[a] = rs.getString("x");
a++; }
Kameswari Jyosyula
Ranch Hand

Joined: Feb 20, 2001
Posts: 39
the point is the left hand side (array) is incompatible with the right hand side (String).Also the return statement for the method has to be outside the while loop so it returns only single value not all.
Originally posted by Vishakha Ahuja:
Since you have defined a while loop, why don't you use an index for array :
int a = 0;
while(rs.next()) {
stringArray[a] = rs.getString("x");
a++; }

Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Originally posted by Kameswari Jyosyula:
the point is the left hand side (array) is incompatible with the right hand side (String).Also the return statement for the method has to be outside the while loop so it returns only single value not all.

?? I don't understand this explanation.
stringArray[a] is a String. rs.getString("x") returns a String. So why can't you assign a String value to an element in a String array?
Jamie
J You
Greenhorn

Joined: Jul 31, 2001
Posts: 29

How about using Array a=getArray(); instead of
Arrayp[] a=getArray();
Originally posted by Kameswari Jyosyula:
[B]Hi, I am trying to get data from a db into my jsp page as an array.How do i do that?
I am calling the returned value from my method.But if i give the return type as String only a single value is returned.
I tried to use array.
if i say

i get an error (obviously for String [] and String are incompatible)I get the sam incompatible error if i use Array[] and getArray().So how to return the string type column(x) as an array?
Plz help
TIA

[This message has been edited by Kameswari Jyosyula (edited August 14, 2001).][/B]

J You
Greenhorn

Joined: Jul 31, 2001
Posts: 29
how about using vector instead of array,
public Vector x(String column ) throws SQLException {
Vector v=new Vector();
.....
while(rs.next()) {
v.addElement(rs.getString("x"));
}
.....
return v;
}
when calling this method,
Vector vecResult = x("column_name");
SoonAnn Lim
Ranch Hand

Joined: Jun 21, 2001
Posts: 155
Hi, as i can see, the solution you look for is not very obvious. There are several problems in your code. First, all sql codes must be in a try and catch block that throw SQLException. Second, you don't know the size of the array you are creating. If i were to solve this problem,
1. i will use a vector to store all the result from resultset. If your result set contains more than one column, then write a simple class to hold all the values from the results, then put the instances of the class into vector.
2. Then i can find the size of the vector using Vector.size, this let me create an array of String (your case) or array of other objects with correct size. After that you want to iterate through all the elements in your vector.
3. Use Enumeration to iterate through your vector. In Vector class, Vector.element() method return the enumeration of vector.
"Enumeration e = v.elements();".
4. Finally, a while loop can help you to iterate through all the element in the Enumeration. "while(e.hasMoreElements())", In this loop, you extract all objects from Enumeration and assign to your array(String or objects) with an explicit cast. The method is "Enumeration.nextElement()". For example,
a=0;
while (e.hasMoreElements()){
str[a++]=(String)e.nextElement();
}
Hope this will help.
Kameswari Jyosyula
Ranch Hand

Joined: Feb 20, 2001
Posts: 39
Thanks a lot.
That helped.It worked.
Originally posted by SoonAnn Lim:
Hi, as i can see, the solution you look for is not very obvious. There are several problems in your code. First, all sql codes must be in a try and catch block that throw SQLException. Second, you don't know the size of the array you are creating. If i were to solve this problem,
1. i will use a vector to store all the result from resultset. If your result set contains more than one column, then write a simple class to hold all the values from the results, then put the instances of the class into vector.
2. Then i can find the size of the vector using Vector.size, this let me create an array of String (your case) or array of other objects with correct size. After that you want to iterate through all the elements in your vector.
3. Use Enumeration to iterate through your vector. In Vector class, Vector.element() method return the enumeration of vector.
"Enumeration e = v.elements();".
4. Finally, a while loop can help you to iterate through all the element in the Enumeration. "while(e.hasMoreElements())", In this loop, you extract all objects from Enumeration and assign to your array(String or objects) with an explicit cast. The method is "Enumeration.nextElement()". For example,
a=0;
while (e.hasMoreElements()){
str[a++]=(String)e.nextElement();
}
Hope this will help.

 
GeeCON Prague 2014
 
subject: how to get data from a db into an array?