This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

how to get data from a db into an array?  RSS feed

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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++; }


 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!