分页技术的使用

在数据库中,我们获得n-m条记录的时候,使用的是行内视图的方式。及执行下面这样的代码:

select id,user_id,login_time,logout_time
from (	select id, user_id, login_time,logout_time, rownum rn
	from my_fen
   	where rownum <20)
where rn >=10;

我们可以看到,rownum的别名是rn,在外层的查询中用了别名rn.内层的查询,找个一个结果集。外层查询主要是对内存查询得到的这个结果集做进一步的查询。这样叫行内视图的方式的查询。分页技术就是能用程序的读入m,n,来定位结果集。还有就是要指定每页显示记录的条数。

分页问题的具体需求:

已知条件:每页的pageSize条,

需求:找第n页。

例如每页显示10条记录,则有如下情况:

1,1 -------- 10

2,11 ------  20

...........

n,(n-1)*10+1 ----------- n*10

所以可以得出一个一般意义下的公式:

from: (n-1)*pageSIze+1

to: from+pageSize

查找的区间是[from, to),左闭右开的一个区间。

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestPage {
	public static void main(String[] args){
		getResult(10, 20);
		System.out.println();
		getPage(15, 2);
		System.out.println();
		getPageSimple(10, 2);
	}
	public static int getTotalNumber(){
		int n = -1;
		String sql = "select count(*) num from my_fen";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try{
			conn = ConnectionUtils.openConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			rs.next();
			n = rs.getInt("num");
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			ConnectionUtils.closeConnection(conn);
			ConnectionUtils.closeResultSet(rs);
			ConnectionUtils.closeStatement(stmt);
		}
		return n;
	}
	public static void getPageSimple(int pageSize, int page){
		int totalPage = 0;
		int totalNumber = getTotalNumber();
		if(totalNumber % pageSize == 0){
			totalPage = totalNumber / pageSize;
		}else{
			totalPage = totalNumber / pageSize + 1;
		}
		if(page > totalPage){
			page = totalPage;
		}
		if(page < 1){
			page = 1;
		}
		int from = (page - 1)*pageSize + 1;
		int to = from + pageSize;
		getResult(from, to);
	}
	public static void getPage(int pageSize, int page){
		int from = (page-1)*pageSize + 1;
		int to = from + pageSize;
		Connection conn = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		try{
			String sql = "select id, user_id, login_time, logout_time, rn " +
					"from(select id, user_id, login_time, logout_time, rownum rn " +
					"from my_fen where rownum < ?)" +
					"where rn >=?";
			
			conn = ConnectionUtils.openConnection();
			pStmt = conn.prepareStatement(sql);
			pStmt.setInt(1, to);
			pStmt.setInt(2, from);
			rs = pStmt.executeQuery();
			while(rs.next()){
				String line = rs.getString("id")+"-"+rs.getString("user_id")+"-"+
				rs.getString("rn");
				System.out.println(line);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			ConnectionUtils.closeConnection(conn);
			ConnectionUtils.closeStatement(pStmt);
			ConnectionUtils.closeResultSet(rs);
		}
		
	}
	public static void getResult(int m, int n){
		Connection conn = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		try{
			String sql = "select id, user_id, login_time, logout_time, rn " +
					"from (select id, user_id, login_time, logout_time, rownum rn " +
					"from my_fen " +
					"where rownum < ?) " +
					"where rn >= ?";
			conn = ConnectionUtils.openConnection();
			pStmt = conn.prepareStatement(sql);
			pStmt.setInt(1, n);
			pStmt.setInt(2, m);
			rs = pStmt.executeQuery();
			while(rs.next()){
				String line = rs.getString("id")+"-"+rs.getString("user_id")+"-"+
				rs.getString("rn");
				System.out.println(line);
			}
		}catch(SQLException e){
			e.printStackTrace();
			
		}finally{
			ConnectionUtils.closeConnection(conn);
			ConnectionUtils.closeResultSet(rs);
			ConnectionUtils.closeStatement(pStmt);
		}
	}
}
上面的程序使用了四个方法

1.public static int getTotalNumber()

这个方法的功能是得到数据库中记录的条数

2.public static void getPageSimple(int pageSize, int page)

这个方法的功能是得到指定每页显示的记录条数已经得到的目标页

3.public static void getPage(int pageSize, int page)

这个方法的功能是得到指定每页显示的记录条数已经得到的目标页

public static void getResult(int m, int n)

这个方法的功能是得到[m,n)这个区间的记录。

这就是所谓的分页技术,主要就是找到指定的区间的记录。以及指定页面所显示记录的条数和指定的页面。其中程序中处理的一些异常情况。因为软件测试要以破坏的心态来做,才能找到程序的bug。对与这些异常,要有足够的处理。程序不仅仅能够处理正常的业务逻辑,对于异常情况,还应该有合理的应对方式。


下面是程序执行时候得到的结果集:

10-884000-10
11-149100-11
12-293800-12
13-574300-13
14-344200-14
15-110900-15
16-826200-16
17-425800-17
18-254000-18
19-719000-19

16-826200-16
17-425800-17
18-254000-18
19-719000-19
20-632200-20
21-732400-21
22-916900-22
23-647000-23
24-646000-24
25-313300-25
26-815400-26
27-810300-27
28-802300-28
29-974800-29
30-234500-30

11-149100-11
12-293800-12
13-574300-13
14-344200-14
15-110900-15
16-826200-16
17-425800-17
18-254000-18
19-719000-19
20-632200-20

数据库中的数据如下:

结果集和数据库中的数据做对比,可以说明,程序实现分页查找指定区间指定页记录的要求。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值