简体   繁体   English

我想在jsp中每页显示数据库10的结果

[英]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.

相关问题 我想从数据库中检索图像并将其显示在jsp页面中 - I want to retrieve image from the database and display it in a jsp page 我想将数据库中的多个图像显示到jsp中(我正在映射servlet),所以在jsp中,它将显示在img标签的src中 - I want to display multiple images from database into jsp (i am mapping servlet) so in jsp m gonna display in src of img tag 如何显示从数据库到JSP页面的图像 - How to display a a image from database to JSP page 我想从jsp页面调用servlet - I want to call servlet from jsp page 试图从 mySQL 数据库中获取结果并将其显示在 jsp 文件中 - Trying to fetch results from mySQL database and display it in a jsp file 我想从数据库中检索图像并在JSP中使用显示它 <img> 标签。 我正在使用我的SQL - I want to retrieve image from database and display it in JSP using <img> tag. I am using my sql 我想从 spring controller 在 JSP 中一一显示值 - I want to display one by one value in JSP from spring controller 我想在同一HTML页面中显示servlet的结果,这可能吗? - I want to display the results from servlet in the same HTML page.Is that possible? 从数据库检索图像并显示在另一个jsp页面中 - Retrieve Image from Database and display in another jsp page 如何在jsp页面上显示mysql数据库中的图像和文本 - how to display image and text from mysql database on jsp page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM