jdbc之分页查询
分页查询作为一项十分重要的数据库查询技术,在很多web项目中都会要用到,当然移动开发中也是会涉及的。
一、分页查询的sql语句:
ps:为了方便阐述,下面统一使用student表作为查询的表;colName表示student表中的某个字段名字。
1、mysql
select * from student (order by colName) limit m, n;
参数解释 m:表示要查询记录的上一行的行号,比如要从第1条记录开始查则m取0;
n:表示希望从m+1开始查询多少条记录;
示例:
studet表中的数据如下




2、oracle
select * from (select t.*,rownum rn from (select * from student) t where rownum<=m) where rn>=n;
参数解释 rownum:是oracle系统为查询返回的行顺序分配的编号,详细介绍请参考该博文:http://www.cnblogs.com/zjrstar/archive/2006/08/31/491090.html
m:查询的最大行号;
n:查询的最小行号;
这样查询的结果行数为:m-n+1
示例:
student表中的数据如下:


3、sql server
实现的sql语法有多种:
①利用ID大于多少
select top 4 * from student where stuid>( select max(stuid) from ( select top 2 stuid from student order by stuid ) as t ) order by stuid;
②利用not in
select top 4 * from student where stuid not in ( select top 2 stuid from student order by stuid) as t order by stuid;
③利用颠倒型top
select * from ( select top 4 * from ( select top 6 * from student order by stuid ) as t order by t.stuid desc ) as t1 order by t1.stuid;
④使用ROW_NUMBER()函数
select * from ( select *,row_number() over (order by stuid) as rank from student ) as t where t.rank between 3 and 6;
由于没怎么用sql server,所以电脑上没装,这里就不给出查询示例了。
二、jdbc中如何实现动态分页
1、四个重要参数
pageNow 当前的页码
pageCount 总页数
pageSize 每页中显示多少条记录
rowCount 表中记录的总行数
2、根据rowCount和pageSize计算pageCount的小算法
① if(rowCount % pageSize == 0) {
pageCount = rowCount / pageSize;
} else {
pageCount = rowCount / pageSize + 1;
}
② pageCount = rowCount % pageSize == 0 ? rowCount / pageSize : rowCount / pageSize + 1;
③ pageCount = (rowCount - 1) / pageSize + 1;
原理是一样的,只是给出了三种形式,个人比较喜欢③。
3、将pageSize和pageNow用在jdbc的sql语句中查询当前页的记录
⑴mysql
select * from student (order by colName) limit (pageNow-1)*pageSize, pageSize;
⑵oracel
select * from (select t.*,rownum rn from (select * from student) t where rownum<=pageNow*pageSize) where rn>=(pageNow-1)*pageSize+1;
⑶sql server
select top pageSize * from student where stuid>( select max(stuid) from ( select top (pageNow-1)*pageSize stuid from student order by stuid ) as t ) order by stuid;
sql server分页查询语句中,这条语句的效率较高,其他的几种查询形式这里就不讨论了。
jdbc之分页查询的更多相关文章
- JDBC在Java Web中的应用——分页查询
分页查询 通过JDBC实现分页查询的方法有很多种,而且不同的数据库机制也提供了不同的分页方式,在这里介绍两种非常典型的分页方法. 通过ResultSet的光标实现分页 通过ResultSet的光标实现 ...
- 分页查询信息(使用jdbc连接mysql数据库实现分页查询任务)
分页查询信息 使用jdbc连接mysql数据库实现分页查询任务 通过mysql数据库提供的分页机制,实现商品信息的分页查询功能,将查询到的信息显示到jsp页面上. 本项目 ...
- JDBC使用游标实现分页查询的方法
本文实例讲述了JDBC使用游标实现分页查询的方法.分享给大家供大家参考,具体如下: /** * 一次只从数据库中查询最大maxCount条记录 * @param sql 传入的sql语句 * @par ...
- JDBC分页查询及实现
当数据过多时,一页之内是无法显示的,因此需要进行分页显示. (一)分页技术实现: 物理分页: - 在数据库执行查询时(实现分页查询),查询需要的数据--依赖数据库的SQL语句 - 在sql查询时,从数 ...
- JdbcTemplate+PageImpl实现多表分页查询
一.基础实体 @MappedSuperclass public abstract class AbsIdEntity implements Serializable { private static ...
- MyBatis简单的增删改查以及简单的分页查询实现
MyBatis简单的增删改查以及简单的分页查询实现 <? xml version="1.0" encoding="UTF-8"? > <!DO ...
- Java GUI+mysql+分页查询
1.要求 : 创建一个学生信息管理数据库 2.实现分页查询 代码如下: a)学生实体类: /** * @author: Annie * @date:2016年6月23日 * @description: ...
- 基于Mysql数据库的SSM分页查询
前言: Hello,本Y又来了,"分页"在我们使用软件的过程中是一个很常见的场景,比如博客园对于每个博主的博客都进行了分页展示.可以简单清晰的展示数据,防止一下子将过多的数据展现给 ...
- java使用插件pagehelper在mybatis中实现分页查询
摘要: com.github.pagehelper.PageHelper是一款好用的开源免费的Mybatis第三方物理分页插件 PageHelper是国内牛人的一个开源项目,有兴趣的可以去看源码,都有 ...
随机推荐
- LXNetwork – 基于AF3.0封装的iOS网络请求库
本框架实现思路与YTKNetwork和RTNetworking类似,相当于一个简单版,把每一个网络请求封装成对象.使用LXNetwork,你的每一个请求都需要继承LXBaseRequest类,通过覆盖 ...
- envi5.1下载地址
ENVI 5.1 installer 32 bit :链接: http://pan.baidu.com/s/1c0EGZIw 密码: gcogENVI 5.1 Installer 64 bit :链接 ...
- ORACLE时间函数(SYSDATE)深入理解
ORACLE时间函数(SYSDATE)深入理解 加法 select sysdate,add_months(sysdate,12) from dual; --加1年 select sysdate,add ...
- [转]DataTable用中使用Compute 实现简单的DataTable数据的统计
本文转自:http://blog.csdn.net/zwxrain/article/details/252285 調用格式: object DataTable.Compute(string expre ...
- [转]Using Replacement Strings with Regex.Replace
本文转自:http://www.knowdotnet.com/articles/regereplacementstrings.html The String.Replace function has ...
- LeetCode 152
Maximum Product Subarray Find the contiguous subarray within an array (containing at least one numbe ...
- Java中windows路径转换成linux路径等工具类
项目中发现别人写好的操作系统相关的工具类: 我总结的类似相关博客:http://www.cnblogs.com/DreamDrive/p/4289860.html import java.net.In ...
- MapReduce中使用SequenceFile的方式上传文件到集群中
如果有很多的小文件,上传到HDFS集群,每个文件都会对应一个block块,一个block块的大小默认是128M,对于很多的小文件来说占用了非常多的block数量,就会影响到内存的消耗, MapRedu ...
- 关于Java的this关键字
java中的this随处可见,用法也多,现在整理有几点: 1. this是指当前对象自己. 当在一个类中要明确指出使用对象自己的的变量或函数时就应该加上this引用.如下面这个例子中: public ...
- Error This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. T
错误提示: Severity Code Description Project File Line Suppression StateError This project references NuG ...