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