User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JSP section within the Web Development category of DaniWeb, a massive community of 392,088 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,927 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JSP advertiser: Lunarpages JSP Web Hosting
Views: 1806 | Replies: 3 | Solved
Reply
Join Date: May 2007
Posts: 2
Reputation: assoora is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
assoora assoora is offline Offline
Newbie Poster

Help how to read data from database page by page, please it is urgent...

  #1  
May 16th, 2007
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2007
Posts: 83
Reputation: rgtaylor is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
rgtaylor rgtaylor is offline Offline
Junior Poster in Training

Re: how to read data from database page by page, please it is urgent...

  #2  
May 16th, 2007
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,
Reply With Quote  
Join Date: May 2007
Posts: 2
Reputation: assoora is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
assoora assoora is offline Offline
Newbie Poster

Help Re: how to read data from database page by page, please it is urgent...

  #3  
May 17th, 2007
Originally Posted by rgtaylor View Post
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
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,646
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 191
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: how to read data from database page by page, please it is urgent...

  #4  
May 17th, 2007
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.
42 Private messages asking for help will be ignored
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JSP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the JSP Forum

All times are GMT -4. The time now is 12:36 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC