[英]I want to display results from database 10 per page in jsp
I have the following code for my website, and I want to expand this result to display 10 results per page. 我的网站有以下代码,我想将此结果扩展为每页显示10个结果。 If some one can help me I will be grateful.
如果有人可以帮助我,我将不胜感激。
java.sql.PreparedStatement p = servlet1.DB.query("select * from user where userdate like ");
p.setString(1,userdate);
java.sql.ResultSet r = p.executeQuery();
Have a read at http://www.javaranch.com/journal/2008/08/pagination-using-JDBC-and-JSP.html . 阅读http://www.javaranch.com/journal/2008/08/pagination-using-JDBC-and-JSP.html 。 This talks about various ways of pagination.
这里讨论分页的各种方式。 Choose which fits best in your case.
选择最适合您的情况。
There are two basic approaches. 有两种基本方法。
The most efficient is to do the pagination in the database. 最有效的是在数据库中进行分页。 You will need to implement something like this:
您将需要实现以下内容:
public long countMyData(..query params..);
public Object[] loadMyDataPage(..query params.., long startIndex, long count);
Having those two methods, you then render the page controls on the page based on the number of results from countMyData(). 使用这两种方法,然后可以根据countMyData()的结果数在页面上呈现页面控件。 When user selects a new page you get the data for that page only using loadMyDataPage() with correct startIndex and count.
当用户选择一个新页面时,仅使用具有正确的startIndex和count的loadMyDataPage()才能获取该页面的数据。
You also want to make sure that the query is relatively "steady" - the result will be mostly same when it's called time after time. 您还想确保查询相对“稳定”-一次又一次地调用时,结果将基本相同。 The simplest way to do that is to make sure you sort the result on something sensible - like topic date for forum software or something like that.
最简单的方法是确保您对结果进行合理排序 -例如论坛软件的主题日期或类似内容。 Otherwise the items will "jump" around.
否则,这些物品将“跳动”。
Second approach is to just load everything in one go and store it in some cache, and then display from there. 第二种方法是一次性加载所有内容并将其存储在某个缓存中,然后从那里显示。 The problem is that it is highly wasteful especially if it's unique per visitor, so you need to be careful not to waste all memory if you try to do it this way.
问题是它非常浪费,特别是如果每个访问者唯一的话,因此,如果您尝试这样做,就必须注意不要浪费所有内存。
Have a look at DisplayTag . 看一下DisplayTag 。 It is a JSP tag library that can take a result set and display it in a tabular fashion, including pagination:
它是一个JSP标记库,它可以获取结果集并以表格形式显示它,包括分页:
<sql:query var="results">
select * from table
</sql:query>
<display:table name="${results.rows}" pagesize="10"/>
Take a look at this links. 看一下这个链接。
The pattern (you need to know): 模式(您需要知道):
Value List Handler Pattern. 值列表处理程序模式。
The implementation (you can use this): 实现(您可以使用此):
ValueList: An implementation of Value List Handler Pattern. ValueList:“值列表处理程序模式”的实现。
Hope this will help you. 希望这会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.