sqlserver 纯分页

 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.dao.EbookDao"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript">
		function showResult(no){
			location.href="index.jsp?no="+no;
		}
	</script>
  </head>
  
  <body>
    	<%
    		EbookDao dao = new EbookDao();
    		String no = request.getParameter("no");
    		int pageNo = 0;
    		if(no==null||"".equals(no)){
    			pageNo = 1;
    		}else{
    			pageNo = Integer.parseInt(no);
    		}
    		pageContext.setAttribute("pageNo",pageNo);
    		List list = dao.selectAll(pageNo);
			pageContext.setAttribute("booList",list);
			int totalPage = dao.getTotalPage();
			pageContext.setAttribute("totalPage",totalPage);
    	 	
    	 %>
    	 <center>
    	 <table border="1">
    	 	<tr>
    	 		<th>图书名称</th>
    	 		<th>图书类别</th>
    	 		<th>图书状态</th>
    	 	</tr>
    	 	<c:forEach items="${booList}" var="book">
    	 		<tr>
    	 			<td>${book.bookname }</td>
    	 			<td>${book.booktype }</td>
    	 			<td>${book.bookstate }</td>
    	 		</tr>
    	 	</c:forEach>
    	 </table>
    	 <input  type="button" value="首页" οnclick="showResult(1)">
    	 <c:if test="${pageNo gt 1}">
    	 <input type="button" value="上一页" οnclick="showResult(${pageNo-1})">
    	 </c:if>
    	 <c:if test="${pageNo lt totalPage}">
    	 <input type="button" value="下一页" οnclick="showResult(${pageNo+1})">
    	 </c:if>
    	 <input  type="button" value="尾页" οnclick="showResult(${totalPage })">
    	 </center>
  </body>
</html>

package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.entity.Ebook;
//1页====0(pageNo-1)*pageSize
//2页====2(pageNo-1)*pageSize
//3页====4(pageNo-1)*pageSize
//select top pageSize * from table
//where id not in(select top (pageNo-1)*pageSize id from table)
public class EbookDao extends BaseDao{
	public List<Ebook> selectAll(int pageNo){
		Connection conn = this.getConn();
		List<Ebook> list = new ArrayList<Ebook>();
		try {
			String sql = "select top 2 * from bookinfo where bookid not in " +
					"(select top "+(pageNo-1)*2+" bookid from bookinfo)";
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				Ebook book = new Ebook();
				book.setBookid(rs.getInt(1));
				book.setBookname(rs.getString(2));
				book.setBooktype(rs.getString(3));
				book.setBookstate(rs.getString(4));
				list.add(book);
			}
			rs.close();
			ps.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			this.closeConn(conn);
		}
		return list;
	}
	
	/**
	 * 计算总页数
	 */
	public int getTotalPage(){
		Connection conn = this.getConn();
		String sql="select count(*) from bookinfo";
		int count = 0;
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			rs.next();
			count = rs.getInt(1);
			rs.close();
			ps.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			this.closeConn(conn);
		}
		return count%2==0?count/2:count/2+1;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值