Hi all,
<br>
I've a JSP page that has 2 buttons one to bring data from database via a servlet & a java Bean, and the other is a next button.
<br>

i want to show the data from the database 4 rows per a page, i.e. when i click on the next button i bring the next 4 rows from the database (the next 4 rows are shown and so on).

<br>
note:
my code shows the whole data per one page.

<br>
please can anyone help, it is urgent.

OK, first of all, what DB are you using?
Second, where are you doing the formatting, in JSP, in the Servlet or in the Bean?

Depending on the DB you use, you should be able to set a limit and and offset int he SQL statememt. This will have pseudo random results unless you also have an order by criteria. In other words, unless you tell it what order to put them in, the order could change each time you run an SQL statement, so using offset and limit yield would yield unexpected results.

Lets assume you have an SQL statment such as
Select * from orders where order_date = "2006/05/21";

Don't worry about the syntax at this point, it may be DB specific...

you may need to modify it such as this

Select * from orders where order_date = "2006/05/21" order by order_id limit 4 offset 0;

NOW, the offset would be entered as a parameter that your code could change for each page so the next button would tell it to use page 2, so (2-1) * 4 or 4... 0-3 for the first page, 4 -7 for page 2, etc. if you want to have more than 4 on a page, then you simple change the value of the limit and match it with the multiplier for the offset, so you have limit 5 offset 5 for page 2 if you wanted 5 per page...

Some DBs allow the limit and offset to be set in the limit command thus:
Select * from orders where order_date = "2006/05/21" order by order_id limit 4, 0;

i.e. limit [limit],[offset]

As long as your servlet passes the info to the bean, it won't really matter where the formatting is done, the result set will only contain the 4 items so the rest of the code should work without edits...

NOW, if your DB doesn't support the limit command, and many don't, then where you format the list for your page will matter...

If, for example the list is formatted in the Bean, then you will need a way to tell the bean which 4 to show. And have it format ONLY those 4 and return only those 4 to the JSP page... this can be done by having the "next" button, again, indicate the page # and either having the JSP page set the parameter for the bean prior to reading the list or by having the bean access the URL and read the parameter directly, which is much more tricky...

Of course there are many ways to weight monkey crap.... why would you want to weight monkey crap, I don't know but it sounds more humane than "ways to skin a cat" which I wonder who would want to do that too... Anyway, you can always switch to AJAX, which is basically just using Javascript to make queries to the server int he background, getting the results and then updating just that part ofthe page that needs to be... So you could have Javascript query a servlet which rather than storing results in a bean, could return the 4 rows to the JS and have the JS write the new 4 rows to your screen replacing the old 4 rows... the rest of the page doesn't get refreshed so it looks like a smoother transaction, and actually functions much faster due to lower bandwidth requirements... Now, for this, typically JS would format the data for you, though you can pretty much do with it what you want...

Again, too many ways to describe them all... I hope some of the pointers here help, feel free to provide more specific details and I may be able to provide more specific information ;-)

Peace,

OK, first of all, what DB are you using?
Second, where are you doing the formatting, in JSP, in the Servlet or in the Bean?

Depending on the DB you use, you should be able to set a limit and and offset int he SQL statememt. This will have pseudo random results unless you also have an order by criteria. In other words, unless you tell it what order to put them in, the order could change each time you run an SQL statement, so using offset and limit yield would yield unexpected results.

Lets assume you have an SQL statment such as
Select * from orders where order_date = "2006/05/21";

Don't worry about the syntax at this point, it may be DB specific...

you may need to modify it such as this

Select * from orders where order_date = "2006/05/21" order by order_id limit 4 offset 0;

NOW, the offset would be entered as a parameter that your code could change for each page so the next button would tell it to use page 2, so (2-1) * 4 or 4... 0-3 for the first page, 4 -7 for page 2, etc. if you want to have more than 4 on a page, then you simple change the value of the limit and match it with the multiplier for the offset, so you have limit 5 offset 5 for page 2 if you wanted 5 per page...

Some DBs allow the limit and offset to be set in the limit command thus:
Select * from orders where order_date = "2006/05/21" order by order_id limit 4, 0;

i.e. limit [limit],[offset]

As long as your servlet passes the info to the bean, it won't really matter where the formatting is done, the result set will only contain the 4 items so the rest of the code should work without edits...

NOW, if your DB doesn't support the limit command, and many don't, then where you format the list for your page will matter...

If, for example the list is formatted in the Bean, then you will need a way to tell the bean which 4 to show. And have it format ONLY those 4 and return only those 4 to the JSP page... this can be done by having the "next" button, again, indicate the page # and either having the JSP page set the parameter for the bean prior to reading the list or by having the bean access the URL and read the parameter directly, which is much more tricky...

Of course there are many ways to weight monkey crap.... why would you want to weight monkey crap, I don't know but it sounds more humane than "ways to skin a cat" which I wonder who would want to do that too... Anyway, you can always switch to AJAX, which is basically just using Javascript to make queries to the server int he background, getting the results and then updating just that part ofthe page that needs to be... So you could have Javascript query a servlet which rather than storing results in a bean, could return the 4 rows to the JS and have the JS write the new 4 rows to your screen replacing the old 4 rows... the rest of the page doesn't get refreshed so it looks like a smoother transaction, and actually functions much faster due to lower bandwidth requirements... Now, for this, typically JS would format the data for you, though you can pretty much do with it what you want...

Again, too many ways to describe them all... I hope some of the pointers here help, feel free to provide more specific details and I may be able to provide more specific information ;-)

Peace,

<br>
<br>
---------------------------------------------
<br>

hi,
thanks a lot for your message, it is great.
<br>
my code is attached, please tell me where to put your suggestion.
<,br>
thanks a lot in advance.

<br>
assoora

not in a JSP, that's where you put it.

database code does NOT go in JSPs, in fact no code goes there at all.
JSPs contain JSTL tags and maybe tags from 3rd party tag libraries, and that's it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.