>From: sufi malak <[EMAIL PROTECTED]>
>Reply-To: A mailing list about Java Server Pages specification and
> reference <[EMAIL PROTECTED]>
>To: [EMAIL PROTECTED]
>Subject: Iterator of range or rows ?
>Date: Wed, 14 Feb 2001 18:48:10 -0000
>
>Last time I posted a similar question, but I don't know why some people are
>rude, because this is not an easy thing like hello world, I am trying to
>learn , anyway
>
>I am looking to get in my jsp page the employee names range by range, I
>mean
>get the first 10 rows, and then when pressing NEXT , will get the next 10,
>now I have 10 employees and two links NEXT and PREVIOUS.
Of course, Resultsets don't normally work this way... as has been explained
recently, better mechanisms are to store keys for records, and maintain
pointers through the keyset. That has the advantage of maintaining a
constant list, and you avoid JDBC driver issues far more that way. (Hint:
use EJBs!)
>So what I did was :
>I wrote a class that implement Iterator :
>package com;
>import java.util.*;
>import java.sql.*;
>
>public class ResultSetIterator implements Iterator
>{
> private ResultSet rs;
> private int size;
> private int position = 0;
>
> public ResultSetIterator(ResultSet rs, int x, int range) throws
>SQLException
> {
> this.rs = rs;
> this.size = range;
> rs.relative(x-1);
>
> }
>
> public boolean hasNext()
> {
> return position < size;
> }
>
> public Object next()
> {
> try
> {
> if (rs.isLast() || position >= size)
> throw new NoSuchElementException();
> position++;
> rs.next();
> return rs;
>
> }
> catch (SQLException e)
> {
> throw new IllegalStateException(e.getMessage());
> }
>
> }
>
> public void remove()
> {
> throw new UnsupportedOperationException();
> }
>}
>
>and in another bean called RewardBean I have this function :
>
>public Iterator threads(int start, int range) throws SQLException
> {
> ResultSet results;
> synchronized(getAllRewardsStmt)
> {
> results = getAllRewardsStmt.executeQuery();
> }
>
> return new ResultSetIterator(results, start, range);
> }
>
>Please tell me what is wrong in this design, am I missing something ?
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com
===========================================================================
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://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