>> What is the diference between putting the resultset into a list and
using the list as a session attribute, vs using a bean to hold the
resultset values? They seem about the same to me (an ultra-newbie),
except that a bean can have narrower scope. COuld somebody put qa
piece of code showing the use of a bean here? <<
Here are two scenarios... let's say we are outputting a company directory
list.
== Using ResultSet ===========
<% ResultSet rs = (ResultSet)session.getAttribute("allemployees"); %>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Phone</th>
</tr>
<% while (rs.next()) { %>
<tr>
<td><%= rs.getString("id") %></td>
<td><%= rs.getString("name") %></td>
<td><%= rs.getString("phone") %></td>
</tr>
<% } %>
==============================
== Using Class ===============
<% Employee[] e = (Employee[])session.getAttribute("allemployees"); %>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Phone</th>
</tr>
<% for (int i = 0; i < e.length; i++) { %>
<tr>
<td><%= e[i].getId() %></td>
<td><%= e[i].getName() %></td>
<td><%= e[i].getPhone() %></td>
</tr>
<% } %>
==============================
First of all, you wouldn't want to store a ResultSet in the session because
it requires you to keep a connection open. Your database connection pool
would quickly be filled up if you have many concurrent users.
But equally important is that the second method allows you to have greater
flexibility moving forward. If you had code like Example 1 all over your
site, if you later decide to change the name of the Phone column, you have
to hunt down everywhere you called getString() using that columnname. With
the second example, you just need to change the code in Employee.getPhone().
Or what if you decided not to use a database at all, but instead use Web
Services to retrieve your employee data from a different server? It would
be much easier if you had used an Employee class.
Thirdly, the second method provides more compile-time checking. If you
misspell the name of a method, the compiler will instantly let you know. If
you misspell the name of a column name, you won't know until that code
actually runs. For example:
<% if (rs.getString("department").equals("Sales")) { %>
<%= rs.getString("mobile_phone_nubmer") %>
<% } %>
The column "mobile_phone_number" is misspelled, but unless you test using a
Sales person you wouldn't be notified.
There are probably other advantages of using classes instead of directly
referring to ResultSets but I think that's enough...?
-jmc
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://archives.java.sun.com/jsp-interest.html
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.jsp
http://www.jguru.com/faq/index.jsp
http://www.jspinsider.com