See below...

Geert Van Damme wrote:

> No, putting the whole solution in the session isn't
a good idea.
> Keeping the resultset in the session isn't either.
Even for scrollable
> resultsets.
> In the future, we'll be using WebRowSets for this
functionality,
> but, since this is still very new, most of us are
stuck with the old
> solutions.
> IMHO, the only good solution is to just plainly do
the same query again and
> discard the first x rows.
> It looks stupid from a performance point of view,
but look at it this way.
> - the DBMS uses caching too, so the second time you
do an exact same query,
> it's probably not very heavy.

I agree. And going on the same lines here's a code
snippet from J2EE Pet Store docs
that helps achieve the same.

public Collection getProducts(String categoryId, int
startIndex,
int count) throws SQLException {
String qstr =
"select itemid, listprice, unitcost, " +
"attr1, a.productid, name, descn " +
"from item a, product b where " +
"a.productid = b.productid and category = "
+ "�" + categoryId + "� " + " order by name";
ArrayList al = new ArrayList();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(qstr);
HashMap table = new HashMap();
// skip initial rows as specified by the startIndex
parameter
while (startIndex-- > 0 && rs.next());
// Now get data as requested
while (count-- > 0 && rs.next()) {
int i = 1;
String itemid = rs.getString(i++).trim();
double listprice = rs.getDouble(i++);
double unitcost = rs.getDouble(i++);
Product product = null;
if (table.get(productid) == null) {
product = new Product(productid, name, descn);
table.put(productid, product);
al.add(product);
}
}
rs.close();
stmt.close();
return al;
}
I customized this for my application by adding a
boolean value to the array list
that determines if the next button has to be
displayed.
For example after the result set while loop, include
if(rs.next()) {
showNextButton = true;
}

-Kiran.

>
> - Think about preparedstatements and the possibility
of already having the
> best access plan...
> - In most cases a search engine-like result will
only need the first and
> maybe the second page. The probability that the x'th
page is needed anyhow
> decreases very quickly. So I wouldn't bother too
much in preparing the
> second page a priori.
> In extreme programming they call this YAGNI.
> - If your query isn't simple and takes some time,
you have aproblem anyway.
> In that case the first page will also take a long
time, and your web page
> sucks anyhow ;-) So first solve the first page.
> When that's done, the second page is trivial See
supra.
>
> Geert Van Damme
>
> > -----Original Message-----
> > From: A mailing list about Java Server Pages
specification and reference
> > [mailto:[EMAIL PROTECTED]]On Behalf Of
Marco
> > Sent: woensdag 15 november 2000 23:16
> > To: [EMAIL PROTECTED]
> > Subject: Re: JSP page that display 10 result...
> >
> >
> > Thanx,
> > But i think is a resource killer to put 1000
records in a session
> > bean.......
> > i' ve already done a solution like that but i'm
not happy with this......
> > So i'm searching another solution.....
> > Thanx....
> > mark
> >
> >
> > ----- Original Message -----
> > From: "Duffey, Kevin" <[EMAIL PROTECTED]>
> > To: <[EMAIL PROTECTED]>
> > Sent: Wednesday, November 15, 2000 10:10 PM
> > Subject: Re: JSP page that display 10 result...
> >
> >
> > > Well, I don't have code, but I can at least
explain how I might approach
> > it.
> > >
> > > First, if your using JDBC 2.0, I think there is
the ability to move
> > forward
> > > and backwards in a ResultSet from the database.
However..I am
> > not sure, so
> > > my first intuition would be to use some sort of
counter in your
> > controller
> > > class. I assume your using MVC setup? At any
rate, say you get
> > a Vector of
> > > objects back from the database..1000 of them.
You want to display 10.
> > Using
> > > an HttpSession object of some sort (probably a
bean that the
> > JSP page has
> > > access to via session scope is best), you keep a
count of where in the
> > > vector you are at. First of all..you most likely
will want to display a
> > list
> > > of links that allow the user to jump x amount of
items at a time. If you
> > got
> > > 1000 items back, you would use some sort of math
to take the
> > total output,
> > > and divide it into say 10 sections. In this case
you might see something
> > > like:
> > >
> > > <- prev x    next x ->
> > > 0 100   200  300  400  500  600  700 800 900
> > >
> > > The above being "links" that jump you to item
100, 200, 300,
> > etc. Then, in
> > > your JSP page you would do a loop:
> > >
> > > <%
> > >   int cnt = bean.getCurrentPosition() + 10;
> > >   for( int cntr = bean.getCurrentPosition();
cntr < cnt; cntr++ )
> > >   {
> > >     // bean.getResults() is a bean Vector
variable that points to the
> > entire
> > > Vector of results received from the database
> > >     SomeClassName scn = (SomeClassName)
> > > bean.getResults().getElementAt(cntr);
> > > %>
> > >     <a href="some_href_link_<%= cntr %>">Item
<%= scn.getName()
> > %></a><br>
> > > <%
> > >   }
> > > %>
> > >
> > >
> > > That isn't exactly how it should be done..but
its close. Basically, the
> > > SomeClassName would be the class that represents
each Object in the
> > Vector.
> > > You get each class so you can display its name,
contents, whatever the
> > link
> > > is to say. You may also want to display more
parts of the object beyond
> > the
> > > link..such as static text, item number, etc.
> > >
> > > I hope this gets you started.
> > >
> > > > -----Original Message-----
> > > > From: Marco [mailto:[EMAIL PROTECTED]]
> > > > Sent: Wednesday, November 15, 2000 11:58 AM
> > > > To: [EMAIL PROTECTED]
> > > > Subject: JSP page that display 10 result...
> > > >
> > > >
> > > > How to make a JSP page that display 10 result
(from a DB) per
> > > > time and after
> > > > with a next and prev link going forward the
result??
> > > > Can anyone explain me to this??
> > > > Ca i have a code that explain this???
> > > > Thanx in advance...
> > > > marks
> > > >
> > > >
=======================================================


__________________________________________________
Do You Yahoo!?
Yahoo! Calendar - Get organized for the holidays!
http://calendar.yahoo.com/

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to