首页  >  文章  >  数据库  >  数据库连接池

数据库连接池

WBOY
WBOY原创
2016-06-07 15:36:031420浏览

计算确定数据表一共分几页:101/pageSize=100/10=10页。rows = select count(1) from users;方法1:pageCount = rows/pageSize (rows%pageSize==0?0:1); 方法2:pageCount = (rows (pageSize-1))/(pageSize);public void doGet(HttpServletRequest req,

<span>计算确定数据表一共分几页:
	101/pageSize=100/10=10页。
	rows = select count(1) from users;
	方法1:pageCount = rows/pageSize + (rows%pageSize==0?0:1);
  方法2:pageCount = (rows+ (pageSize-1))/(pageSize);
public void doGet(HttpServletRequest req, HttpServletResponse response)
			throws ServletException, IOException {
		//第一步:定义每页显示多少行
		int pageSize = 10;
		try{
			//第二步:获取数据表中有多少行
			QueryRunner run = new QueryRunner(DataSourceUtils.getDatasSource());
			String sql = "select count(*) from users";
			Object o = run.query(sql,new ScalarHandler());
			int rows = Integer.parseInt(o.toString());
			//第三步:计算一共分多少页
			int pageCount= rows/pageSize+(rows%pageSize==0?0:1);
			//将页数放到req
			req.setAttribute("pageCount",pageCount);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		//转发到
		req.getRequestDispatcher("/jsps/show.jsp").forward(req, response);
		
	}
</span>

第四步:分析limit的开始位置

用户请求的面码

start

算法:

1

Limit 0,pageSize

Start

= (currentPage-1)*pageSize

2

Limit 10,pageSize

3

Limit 20,pageSize

用户请求的面码

start

算法:

1

Limit 0,pageSize

Start

= (currentPage-1)*pageSize

2 Limit 10,pageSize

当前页码

页码范围

算法

1

1~10

If(currentPage<=pageNum/2)

  1~10

2

 

3

 

4

 

5

 

6

2~11

6-4=2 =

6-(pageNum/2-1)=2 = startNo

endNo = startNo+(pageNum-1)=11

7

3~12

8

4~13

9

 

 

10

 

 

11

 

11-4 = 7

endNo = 7+9=16

endNo = 11;

startNo = 2 = endNo-(pageNum-1);

 

 

 

3

Limit 20,pageSize

  第五步:对分页以后页码再分页

       每个页面,最多显示10个页码        pageNum=10;

startNo endNo

 

当前页码

页码范围


算法
1 1~10 If(currentPage<=pageNum/2)   1~10
2  
3  
4  
5  
6 2~11 6-4=2 = 6-(pageNum/2-1)=2 = startNo endNo = startNo+(pageNum-1)=11
7 3~12
8 4~13
9    
10    
11   11-4 = 7 endNo = 7+9=16 endNo = 11; startNo = 2 = endNo-(pageNum-1);  
   
             
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn