微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐