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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Interate over ResultSet got from a Bean 1

Status
Not open for further replies.

kunalkaul

Programmer
Jul 24, 2001
3
JP
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
 
in a simple example, this what i would do.

have a jsp talkto a servlet that uses a db connected bean.

the db bean will return the result set to the servlet. here you want to remove any database specific knowledge. i would read from the result set into a vector or something else that can manage your data. from your servlet, add this new vector as an attribute of the request i.e.

// Create your vector
Vector list = new Vector( 0, 1 );

// Add the results to the vector

// Add the project list to the request
request.setAttribute( "myList", list );

in your jsp, read the following
<% Vector pl = ( Vector ) request.getAttribute( &quot;myList&quot; ); %>

and read from the vector as required.

note, this is a quick and dirty example and you should look into using String[] where possible as this will save you from importing different packages into your jsp

hope this helps :)
 
Hi LittleWing,
Thanks for the tip. But tell me if i want to retun the vector directly to my jsp then how would i do it. For eg. If i use the method like given below then how do i retrive it in my jsp. Would i still need to use a servlet??

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(&quot;your Sql code&quot;);
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;
}

Thanks
Kunal
 
yeah, u can do it your way by passing back your vector as a return value. that you have to do in this case in to import your bean, create an instance of it and then call your method, assuming it isn't static. FYI, a jsp is compiled into a servlet anyway!!

i suggested the other way as its a more structured approach for developing web systems
 
Hi LittleWing,

Thanks for ur advice. I wanted to know more about the difference between the 2 way viz., returing the vector directly to jsp and returing the method to jsp via a servlet. As u said it is a more structured approach. Could you please eloborate on it. Or is it a standerd way to do it.

Thanks
Kunal
 
its the standard development approach.

jsp's are used for presentation only so they shouldn't be calling a bean to connect to the database. your jsp should call have links to servlets that are designed specificly for interacting with your tiered app. a jsp should only read business level data objects and add the data to their presentation. thats it

Servlets, as mentioned above, know how to read data input from the jsp and passed it to other beans in your business layer. it knows where in the business layer to send the requests but that is.

the business layer, be it jaba beans, session ejb, know how to contact the data access layer but know nothing specific about the data sources other than their names and what request requries what source.

the data layer encapsulates all the system connections, be they database or external systems.

use your own data objects to store the data that is passed bewteen layers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top