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是国内牛人的一个开源项目,有兴趣的可以去看源码,都有 ...
随机推荐
- 小白日记35:kali渗透测试之Web渗透-手动漏洞挖掘(一)-默认安装引发的漏洞
手动漏洞挖掘 即扫描后,如何对发现的漏洞告警进行验证. #默认安装 流传linux操作系统比windows系统安全的说法,是因为windows系统默认安装后,会开放很多服务和无用的端口,而且未经过严格 ...
- Linux 学习之路:read,array,declare
一.read 键盘读取变量 用法:read -p "PLS keyin your name:" -t 60 name -p :后面接提示符,-t 后面接可以等待的时间,其中nam ...
- 关于Windows下mysql忘记root密码的解决方法
原文链接: http://www.cnblogs.com/andy_tigger/archive/2012/04/12/2443652.html 1. 首先检查mysql服务是否启动,若已启动则先将其 ...
- 关于location.href几种用法的区别
常见的几种开发形式: self.location.href; window.location.href; this.location.href; location.href; parent.locat ...
- Radware中APPDirector系列的Farm Table中的session mode参数说明
Session mode中共有5种会话保持方式:1.Regular,是普通的会话保持,形成的表项是:Client ip+Server ip的形式2.EntryPerSession(EPS),是端口与i ...
- Bootstrap ACE后台管理界面模板-jquery已整理
做后台通用模板,基于bootstrap,jquery写成的模板,非常齐全.国内不能正常访问google我将不能访问的jquery替换成cdn.bootcss.com网站下的jquery 链接: htt ...
- 初识CSS3之媒体查询(2015年05月31日)
一.什么是媒体查询 媒体查询是面向不同设备提供不同样式的一种实现方式,它可以为每种类型的用户提供最佳的体验,也是响应式设计的实现方式. 现今每天都有更多的手机和平板电脑问市.消费者能够拥有可想象到的各 ...
- Jersey(1.19.1) - Root Resource Classes
Root resource classes are POJOs (Plain Old Java Objects) that are annotated with @Path have at least ...
- django 学习-5 模板使用流程
首先在模板下建一个index.html <!DOCTYPE html><html><head><meta charset="utf-8" ...
- asp中utf8不会出现乱码的写法
<%@ CODEPAGE=65001 %> <% Response.CodePage=65001%> <% Response.Charset="UTF-8&qu ...