How to Interate over ResultSet got from a Bean
Hi All,
Recently I got to know that if u run ur Queries directly in ur JSP, well it is a design flaw. JSP's are ment to provide only the presentation logic. That means i had to shift all the Funtional logic to beans.
Till here it is cool. The problem that i am facing is if u execute a query in a bean and get a resultset, suppose u get 10 records. U can iterate over then on the bean side...but how do u iterate over then on the jsp side for displaying them.
Any code example would be really appriciated.
Thankx in advance
Kunal
[575 byte] By [
kunalkaul] at [2007-9-26 1:55:12]

I not sure sure whether this will work for you.
In my jsp page, i use resultset to execute my query then in order to go through and display. I wrote this:
<%
java.sql.ResultSet members = stmt.executeQuery(SQL);
while (members.next())
{ %>
<%= members.getString("UserName") %>
<% } %>
Hope that this is useful to you.
Hi caiyun01,
I think my question is not totally clear. Sorry for that.
I don't want to get the resultset to the jsp. I want only the records in the jsp. So while the bean is iterating over the resultset i should be able to display the records on the jsp side.
Hope that i have been able to clarify on the question.
what you can do is in your bean class define
a method for eample getRecods() this way
public Vector getRecords() throws SQLException
{
int cnt=0;
Vector vec = new Vector(10,0);
Statement stmt;
ResultSet rs;
if (conn != null)
{
stmt = conn.createStatement();
String query = new String("your Sql code");
rs = stmt.executeQuery(query);
while(rs.next())
{
// put the data in a object & add
// that object in vector object exam vec
vec.addElement(your objct)
}
}
vec.trimToSize();
return vec;
}
in jsp page call this method & get vector object and iterate over it to display your data .
Insted of vector u can use any other colection suitable for your pupose
Hi parvez_k ,
Thanks for you reply. I have not implemented it as yet as there is one thing that i would like to clarify from you. Lets say that i follow the code example that u have given then, the thing is that is not clear is, while i am getting the vector on the jsp page i will have a predefined sequence of records comming.
Let me clarify further.
Lets say that from the bean the record set that i have got has the elements Name, Age and City. Now as the resultset iterates on the bean side the sequence in which the items are added to the vector are name, age, city, name1, age1, city1....and so on till the end of resultset.
Now on the jsp side the sequence in which i can get the elements will be the same starting from vector position 0 where i will have name and at vector position 1 i will have age and at vector position 2 i will have city and at vector position 3 i will have name1 ....so on
Please correct me if i am wrong anywhere.
so on the jsp side i will have to do something like
for (int i = 0; i = vec.size(); i ++){
out.println(vec(i)); // for getting the name
out.println(vec(i+1));// for getting the age
out.println(vec(i+2)); // for getting the city
}
please let me know if this the way i would have to do it.
Thankx
Hi parvez_k ,Well i have been trying to implement the code that u have provided. Now the place that i am stuck at is....how do i get the Vector vec on the jsp after the vec is returned By the getRecords() method. I mean how do i get in on jsp.Thankx
What is the problem in getting the Vector . You have invoked the method and that method is returing the method.
The alternative to the earlier program is that you should concatenate the fields of the same record and form a string. This concatenation should insert one special character between each field eg. if you get a result set in which the record has 3 fields so name, deptt and salary then you should concatenate the 3 like
name + "$" + deptt + "$" + salary
this way you know where one field ends. Now when you receive the vector where you invoked the method , you can use the java.util.StringTokeniser for each object in the vector to get individual fields.
Hope this will help
Hi all,I would like to thank you people for helping me out here. Really appriciate the code sent by "parvez_k" . And the advice of "javahunt".Take careKunal
[nobr]Another path to a similar conclusion is to have your fields wrapped with display strings so you get something like this (please excuse dismal pseudocode):String beforeRec1="<b>"
String beforeRec2="</b><br><br>"
String beforeRec3="<br><i>"
String afterRec3="</i><br>"
Stringbuffer returner;
for (i =0; i<record1.length; i++)
returner.append(beforeRec1+record1[i]+beforeRec2+record2[i]+beforeRec3+record3[i]+afterRec3);
return returner;
That creates a display-ready set of records. You can then have setBeforeRec1(String newBefore) type methods to let the user have total control of the system. I use this and it is very popular with designers because they only really have to deal with fragments of HTML with almost no Java code on the jsp page.>[/nobr]
Kunal,
Hi! I hope i got your name right. anyway, I have a bean and working working much like what you want to do but it would be long to post the code here. I will just send you my bean and my JSP file with some comments on it so that you can further appreciate the code. if you could please email me at forever855@hotmail.com so that i can email you back the code. in your email please give me a brief intro of yourself so that i can remember it.
thanks!
Cris