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="首页" onclick="showResult(1)">
    	 <c:if test="${pageNo gt 1}">
    	 <input type="button" value="上一页" onclick="showResult(${pageNo-1})">
    	 </c:if>
    	 <c:if test="${pageNo lt totalPage}">
    	 <input type="button" value="下一页" onclick="showResult(${pageNo+1})">
    	 </c:if>
    	 <input  type="button" value="尾页" onclick="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;
	}
}

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 &#39;EastRiver&#39; 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...