1、PageModel.java

package com.javaweb;

import java.util.List;

public class PageModel<E> {

 private List<E> list; //结果集
private int totalRecords; //查询记录数
private int pageSize; //每页多少记录
private int pageNo; //当前页 //总页数
public int getTotalPages(){
return (totalRecords + pageSize - 1) / pageSize;
} //取得首页
public int getTopPageNo(){
return 1;
} //取得上一页
public int getPreviousPageNo(){
if(pageNo<=1)
return 1;
return pageNo-1;
} // 取得下一页
public int getNextPageNo(){
if(pageNo >= getBottomPageNo())
return getBottomPageNo();
return pageNo+1;
} //取得尾页
public int getBottomPageNo(){
return getTotalPages();
} public List<E> getList() {
return list;
} public void setList(List<E> list) {
this.list = list;
} public int getTotalRecords() {
return totalRecords;
} public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
} public int getPageSize() {
return pageSize;
} public void setPageSize(int pageSize) {
this.pageSize = pageSize;
} public int getPageNo() {
return pageNo;
} public void setPageNo(int pageNo) {
this.pageNo = pageNo;
} }

2、Book.java

package com.javaweb.book;
public class Book {
private String id;
private String name;
private String title;
private float price;
private int year;
private String description;
private int saleAmount; public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getSaleAmount() {
return saleAmount;
}
public void setSaleAmount(int saleAmount) {
this.saleAmount = saleAmount;
}
}

3、BookDb.java

package com.javaweb.book;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import com.javaweb.*;
public class BookDb { // 返回所有book
public List<Book> getAllBooks(){
String sql = "select * from books order by id";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Book> bookList = null;
try{
conn = DbUtil.getConnection();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
bookList = new ArrayList<Book>();
while(rs.next()){
Book book = new Book();
book.setId(rs.getString("id"));
book.setName(rs.getString("name"));
book.setTitle(rs.getString("title"));
book.setPrice(rs.getFloat("price"));
book.setYear(rs.getInt("yr"));
book.setDescription(rs.getString("description"));
book.setSaleAmount(rs.getInt("sale_amount"));
bookList.add(book);
}
}catch(SQLException e){
e.printStackTrace();
}finally{
DbUtil.close(rs);
DbUtil.close(pstmt);
DbUtil.close(conn);
}
return bookList;
} //返回id查询book
//@param id book的id
public Book getBookById(String id){
String sql = "select * from books where id=?";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Book book = null;
try{
conn = DbUtil.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
rs = pstmt.executeQuery();
while(rs.next()){
book = new Book();
book.setId(rs.getString("id"));
book.setName(rs.getString("name"));
book.setTitle(rs.getString("title"));
book.setPrice(rs.getFloat("price"));
book.setYear(rs.getInt("yr"));
book.setDescription(rs.getString("description"));
book.setSaleAmount(rs.getInt("sale_amount"));
}
}catch(SQLException e){
e.printStackTrace();
}finally{
DbUtil.close(rs);
DbUtil.close(pstmt);
DbUtil.close(conn);
}
return book;
} //分页查询(上一页、下一页)
//@param pageNo 第几页
//@param pageSize 每页多少记录
public PageModel<Book> findBookList(int pageNo,int pageSize){
String sql = "select * from books order by id limit ?,?";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
PageModel<Book> pageModel = null;
try{
conn = DbUtil.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, (pageNo-1)*pageSize);
pstmt.setInt(2, pageSize);
rs = pstmt.executeQuery();
List<Book> bookList = new ArrayList<Book>();
while(rs.next()){
Book book = new Book();
book.setId(rs.getString("id"));
book.setName(rs.getString("name"));
book.setTitle(rs.getString("title"));
book.setPrice(rs.getFloat("price"));
book.setYear(rs.getInt("yr"));
book.setDescription(rs.getString("description"));
book.setSaleAmount(rs.getInt("sale_amount"));
bookList.add(book);
}
pageModel = new PageModel<Book>();
pageModel.setList(bookList);
pageModel.setPageNo(pageNo);
pageModel.setPageSize(pageSize);
pageModel.setTotalRecords(getTotalRecords(conn));
}catch(SQLException e){
e.printStackTrace();
}finally{
DbUtil.close(rs);
DbUtil.close(pstmt);
DbUtil.close(conn);
}
return pageModel;
} //取得总记录数
private int getTotalRecords(Connection conn)
throws SQLException{
String sql = "select count(*) from books";
PreparedStatement pstmt = null;
ResultSet rs = null;
int count = 0;
try{
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
rs.next();
count = rs.getInt(1);
}finally{
DbUtil.close(rs);
DbUtil.close(pstmt);
}
return count;
}
}

4、DbUtil.java

package com.javaweb.book;
import java.sql.*; public class DbUtil { private final static String driver = "com.mysql.jdbc.Driver";
private final static String url = "jdbc:mysql://localhost:3306/bookdb";
private final static String user = "root";
private final static String password = "123456"; public static Connection getConnection(){
try{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,user,password);
return conn;
}catch(ClassNotFoundException e){
System.out.println("找不到数据库驱动");
System.out.println(e.getMessage());
}catch(SQLException e){
System.out.println("数据库连接失败");
System.out.println(e.getMessage());
}
return null;
} public static void close(Connection conn){
if(conn!=null){
try{
conn.close();
}catch(SQLException e){
System.out.println("关闭数据库连接出错");
System.out.println(e.getMessage());
}
}
} public static void close(ResultSet rs){
if(rs!=null){
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
}
} public static void close(PreparedStatement pstmt){
if(pstmt!=null){
try{
pstmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}

5、bookstore.jsp

<%@ page contentType="text/html;charset=GBK" %>
<html>
<head><title>Bookstore</title> </head>
<body>
<center>
<p><b><a href="http://blog.163.com/jesse_zy/blog/<%=request.getContextPath()%>/catalog.jsp">查看所有书目</a></b>
</center> </body>
</html> 6、catalog.jsp <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="java.util.*" %>
<%@ page import="com.javaweb.*" %>
<%@ page import="com.javaweb.book.*" %>
<%
int pageNo = 1;
int pageSize = 4;
String pageNoString = request.getParameter("pageNo");
if(pageNoString != null){
pageNo = Integer.parseInt(pageNoString);
}
PageModel<Book> pageModel = new BookDb().findBookList(pageNo,pageSize);
%> <html>
<head>
<title>图书详细信息</title>
<link rel="stylesheet" href="http://blog.163.com/jesse_zy/blog/style.css">
</head> <script type="text/javascript"> function topPage() {
window.self.location = "catalog.jsp?pageNo=<%=pageModel.getTopPageNo()%>";
} function previousPage() {
window.self.location = "catalog.jsp?pageNo=<%=pageModel.getPreviousPageNo()%>";
} function nextPage() {
window.self.location = "catalog.jsp?pageNo=<%=pageModel.getNextPageNo()%>";
} function bottomPage() {
window.self.location = "catalog.jsp?pageNo=<%=pageModel.getBottomPageNo()%>";
}
</script> <body>
<div align="center">
<form>
<table width="900" height="30" border="0" cellspacing="0">
<tr>
<td>图书列表</td>
<td align="right"><a href="http://blog.163.com/jesse_zy/blog/bookstore.jsp">返回主页</a></td>
</tr>
</table>
<table width="900" border="1" cellspacing="0" cellpadding="0">
<tr>
<th>ID</th>
<th>作者</th>
<th>书名</th>
<th>价格</th>
<th>出版年份</th>
<th>描述</th>
<th>销量</th>
</tr>
<%
List<Book> bookList = pageModel.getList();
for(Book book:bookList){
%>
<tr>
<td><%= book.getId()%></td>
<td><%= book.getName()%></td>
<td><%= book.getTitle()%></td>
<td><%= book.getPrice()%></td>
<td><%= book.getYear()%></td>
<td><%= book.getDescription()%></td>
<td><%= book.getSaleAmount()%></td>
</tr>
<%
}
%>
</table>
<table width="900" height="35" border="0" cellspacing="0">
<tr>
<td>
<div>
 共 <%=pageModel.getTotalPages() %> 页</font>    
当前第 <font color="red"><%=pageModel.getPageNo() %></font> 页
</div>
</td>
<td>
<div align="right">
<input type="button" value="首页" onClick="topPage()"
<% if(pageNo<=1) {
%>disabled="disabled"
<% }%>>
<input type="button" value="上一页" onClick="previousPage()"
<% if(pageNo<=1) {
%>disabled="disabled"
<% }%>>
<input type="button" value="下一页" onClick="nextPage()"
<% if(pageNo>=pageModel.getBottomPageNo()) {
%>disabled="disabled"
<% }%>>
<input type="button" value="尾页" onClick="bottomPage()"
<% if(pageNo>=pageModel.getBottomPageNo()) {
%>disabled="disabled"
<% }%>> 
</div>
</td>
<tr>
</table>
</form>
</div>
</body>
</html>

7、style.css

th{
font-family : 宋体;
font-size : 12pt;
}
td{
font-family : 宋体;
font-size : 11pt;
}

8、books.sql

drop database BookDB;
create database BookDB;
use BookDB; create table BOOKS(
ID varchar(8) primary key,
NAME varchar(24),
TITLE varchar(96),
PRICE float,
YR int,
DESCRIPTION varchar(128),
SALE_AMOUNT int); insert into BOOKS values('001','李建',
'Java面向对象编程',65,
2006, '让读者由浅入深掌握Java语言', 20000); insert into BOOKS values('001','李建',
'Java面向对象编程',65,
2006, '让读者由浅入深掌握Java语言', 20000); insert into BOOKS values('002', '李建',
'精通Struts', 49,
2004, '真的很棒', 80000); insert into BOOKS values('003', '李建',
'Tomcat与JavaWeb开发技术详解',
45, 2004, '关于Tomcat与JavaWeb开发的技术书', 40000); insert into BOOKS values('004', '李建',
'Java网络编程精解',
55, 2007, '很值得一看', 20000); insert into BOOKS values('005', '李建',
'精通Hibernate',
59, 2005, '权威的Hibernate技术资料', 50000); insert into BOOKS values('006', '李建', 'Java2认证考试指南与试题解析',
88, 2002, '权威的Java技术资料', 8000);

Java-分页实例的更多相关文章

  1. java分页实例Demo

    前两天测试过的一个分页的demo,在网上看到的,挺好的,就写了下来. 分页也是web里面必须的,有使用的价值. demo文件打包上传了,链接:http://pan.baidu.com/s/1o6sME ...

  2. java:(json,ajax,path,Oracle的分页实例,Filter拦截器)

    1.json: <%@ page language="java" import="java.util.*" pageEncoding="UTF- ...

  3. Struts+jdbc+分页 实例

    根据项目里分页实例,带有注解. package org.tarena.netctoss.dao.impl; import java.sql.Connection; import java.sql.Pr ...

  4. JAVA RMI 实例

    下面我将介绍一个完整的实例,让初学者能快速体验RMI的功用. 分为以下四个步骤 1. 创建远程接口及声明远程方法(HelloInterface.java)2. 实现远程接口及远程方法(继承Unicas ...

  5. RDIFramework.NET 中多表关联查询分页实例

    RDIFramework.NET 中多表关联查询分页实例 RDIFramework.NET,基于.NET的快速信息化系统开发.整合框架,给用户和开发者最佳的.Net框架部署方案.该框架以SOA范式作为 ...

  6. php分页实例附代码

    一个典型的PHP分页实例代码分享,学习php的朋友肯定用得到,主要是了解思路: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transit ...

  7. Jsp分页实例---真分页

    网页的分页功能的实现比较简单,实现方法也多种多样. 今天总结一个简单的Jsp真分页实例. 首先,提到分页就要先明确一个概念,何为真分页何谓假分页. 假分页:一次性从数据库读出表的所有数据一次性的返回给 ...

  8. Java多线程-实例解析

    Java多线程实例 3种实现方法Java中的多线程有三种实现方式:1.继承Thread类,重写run方法.Thread本质上也是一个实现了Runnable的实例,他代表一个线程的实例,并且启动线程的唯 ...

  9. 数据你把它的金额-JAVA分页

    数据量你造吗-JAVA分页 原创地址:   http://www.cnblogs.com/Alandre/  (泥沙砖瓦浆木匠),须要转载的,保留下! Thanks 学习的心态第一,解行要对应. 事实 ...

  10. 浅谈Java分页技术

    话不多言.我们要实现java分页技术,我们首先就需要定义四个变量,他们是: int  pageSize;//每页显示多少条记录 int pageNow;//希望现实第几页 int pageCount; ...

随机推荐

  1. 小游戏 Lights Out (关灯) 的求解 —— 异或方程组

    Author : Evensgn  Blog Link : http://www.cnblogs.com/JoeFan/ Article Link : http://www.cnblogs.com/J ...

  2. Android List<Map<String,String>转json(例子)

    package com.armslee.json.test.cases; import java.util.ArrayList; import java.util.HashMap; import ja ...

  3. Tomcat默认打开项目设置

    Tomcat设置默认启动项目 Tomcat设置默认启动项目,顾名思义,就是让可以在浏览器的地址栏中输入ip:8080,就能访问到我们的项目.具体操作如下: 1.打开tomcat的安装根目录,找到Tom ...

  4. 【JS】JS外联不执行,内联执行

    匹配域名http://lb.qq.com 或 http://lb.l.qq.com

  5. eclipse配置j2ee项目

    1.下载jdk (1.5,1.6) 安装 从sun的官方网站下载,我下的是jdk-1_5_0_19-nb-6_5_1-windows-ml.exe,集成netbean的版本,下载后一路默认安装. 配置 ...

  6. (java) Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

    /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * Lis ...

  7. 【索引】Objective-C基础教程-读书笔记

    第1章 启程 http://www.cnblogs.com/duxiuxing/p/5492219.html 第2章 对C的扩展 第3章  面向对象编程的基础知识 第4章 继承 第5章 复合 第6章 ...

  8. vijosP1285 佳佳的魔法药水

    vijosP1285 佳佳的魔法药水 链接:https://vijos.org/p/1285 [思路] 图论思想. 很巧妙. 如A+B=C,将AB之间连边,边权为C,用以找相连物品与合成物. 用Dij ...

  9. php将SQL查询结果赋值给变量

    2012-03-25 12:12 a786013819 | 分类:数据库DB | 浏览1393次 $sql = "select field1 from pre_common_member_p ...

  10. Ural 1043 Cover the Arc

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1043 题目大意:一个2000*2000方格坐标,x,y范围都是[-1000,1000]. ...