hi, I need help or routine for jsp about how i retreive 10 rows per page with next and previous page button, just like technical forum. i would be very thankful if someone help mee . thanx atif ------------------
"atifwasi", The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please choose a new name which meets the requirements. Thanks.
What i used to do is i populate my database information in a vector . Each element in the vector will be a String or a StringBuffer array. Each of this String/StringBuffer array will strore information about one DB row. I have a class/bean which performs these above operations for me. In my jsp i read this vector (thru the bean instance) sequentially and display first 10 rows and create a session variable with initial value 0. On pressing the next button add 10 to this session variable and this will be starting index for the vector in my next page (11-20. But remember vector index is from 10-19) and read 10 rows. Always check if you have something to display in the nextpage by checking the vector's maxlength with current session variable + 10. This is to prohibit from going ahead if you've already in the last page(display mesaage "Already in last page" and last 10 rows).If previous button subtract 10 from the session variable and read 10 rows. Always check if you are already on the first page by checking if current session variable is negative. If you are already on the first page display the first 10 rows and error message "Already at the first row" or something. I dont know if i make any sense here. Unfortunately i have the code at my job computer. If you can wait till monday i can post one for you There are other alternatives one of them is to use scrollable resultset if you have JDBC 2.0 and its compatible driver. I never tried this myself but always an option. Also i heard vector compared to other similar collection objects is expensive. And StringBuffer is preferred over String (in this instance) because no new objects are created. I leaving these to your discretion. Let me know if this helps Ajan
[This message has been edited by Ajan Balakrishnan (edited February 04, 2001).]
Originally posted by atifwasi: I need help or routine for jsp about how i retreive 10 rows per page with next and previous page button, just like technical forum.
Unfortunately, you give very little indication at what kind of level you need help -- where are you getting stuck? I might be stating the obvious: the next and previous link (or button) should invoke your JSP (or servlet) with enough information to determine which records to retrieve. The most naive way would be to invoke the same JSP with a record range, i.e. href="mypage.jsp?records=20,40". A slightly more sophisticated approach would use an MVC (Model 2) architecture and invoke a controller servlet with a next/previous command, e.g. href="/servlet/controller/mypage.jsp?action=next". Once you've decided how to set up the control structure, the next problem is how to actually retrieve the desired records. There are basically three alternatives, the one you choose depends on the type of application you're developing. You can, at the first query, retrieve all results in an ArrayList and bind that directly in the session or store it in a session-scoped JavaBean. This makes retrieving arbitrary parts of the result set very easy but it may eat more memory than you can afford. If you're using JDBC 2.0, you can make use of the new scrollable ResultSet -- it's got previous(), next(), first() and last() methods. Of course that means you'll have a ResultSet object lying around in your session consuming resources. Finally, you can re-do your query, preferably formulating it in such a way that only the desired records are returned. For example, if you would be using an Oracle database, you would be able to simply say "SELECT [...] WHERE ROWNUM BETWEEN 20 AND 40". All the above assumes you're not using Enterprise JavaBeans. - Peter
thanx ajan, actually at this moment i have no routine or logic to develop this kind of stuff, after review of ur opinion now i m trying to make this kind of stuff. might be posible i could not develope this routine if u can email me ur routine then i will be much thankful to u. email@example.com thanx ------------------
subject: need help for 10 rows per page just like forum