JDBC分页 [英] JDBC Pagination

查看:110
本文介绍了JDBC分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JDBC实现分页。我想知道的实际情况是如何从第1页和第2页的数据库中获得前50个然后接下来的50个记录

I want to implement pagination using JDBC. The actual thing I want to know is "How can i get first 50 and then next 50 records from database for page 1 and 2 respectively"

我的查询是从数据中选择* [数据表包含20,000行]

My Query is Select * from data [data table contains 20,000 rows]

对于第1页,我得到50条记录和第2页我想获得下一个50条记录。如何在JDBC中有效地实现它?

For page #1 I get 50 records and for page #2 I want to get next 50 records. How can I implement it efficiently in JDBC?

我搜索过并发现 rs.absolute(row)是跳过第一页记录的方法,但是在大型结果集上花费了一些时间,我不想承担这么多时间。另外,我不想使用 rownum 限制 + 抵消在查询中,因为这些不适合在查询中使用,我不知道为什么,我仍然不想在查询中使用它。

I have searched and found that rs.absolute(row) is the way to skip first page records but it takes some amount of time on large result sets and I don't want to bear this amount of time. Also, I don't want to use rownum and limit + offset in query because these are not good to use in query, I dont know why, still I don't want to use it in query.

任何人都可以帮我如何获得限制 ResultSet 的分页,或者JDBC有什么方式给我们?

Can anyone help me how to get limited ResultSet for pagination or is there any way JDBC is giving us?

推荐答案

通过简单地使用JDBC,没有有效的方法。您必须将限制为n行并将从第i个项子句直接表示为SQL,以使其高效。根据数据库,这实际上可能非常简单(参见MySQL的LIMIT-keyword),在其他数据库(如Oracle)上,它可能有点棘手(涉及子查询和使用rownum伪列)。

There is no efficient way of doing this by simply using JDBC. You have to formulate the limit to n rows and start from i-th item clauses directly to the SQL for it to be efficient. Depending on the database this might actually be quite easy (see MySQL's LIMIT -keyword), on other databases such as Oracle it can be a little trickier (involves subquery and using rownum pseudo column).

请参阅此JDBC分页教程:
http://java.avdiel.com/Tutorials/JDBCPaging.html

See this JDBC Pagination Tutorial: http://java.avdiel.com/Tutorials/JDBCPaging.html

这篇关于JDBC分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆