日期:2014-05-16  浏览次数:20388 次

JSP+Servlet+Oracle实现分页
菜鸟学javaweb,拼凑一个JSP+Servlet+oracle实现分页的实例,其中有明显不足之处,贴在此主要是方便自己记忆。
数据库建表语句:
-- Create table
create table BLOGINFO
(
  ID          NUMBER not null,
  BLOGTITLE   VARCHAR2(50) not null,
  BLOGCONTENT VARCHAR2(4000) not null,
  AUTHOR      VARCHAR2(30) not null,
  BLOGTIME    LONG not null
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64
    minextents 1
    maxextents unlimited
  );
-- Add comments to the columns 
comment on column BLOGINFO.ID
  is '表id';
comment on column BLOGINFO.BLOGTITLE
  is '博客标题';
comment on column BLOGINFO.BLOGCONTENT
  is '博客内容';
comment on column BLOGINFO.AUTHOR
  is '作者,用户昵称';
-- Create/Recreate primary, unique and foreign key constraints 
alter table BLOGINFO
  add constraint BLOGINFO_ID primary key (ID)
  using index 
  tablespace USERS
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );
alter table BLOGINFO
  add constraint AUTHOR_NAME foreign key (AUTHOR)
  references USERINFO (LOGINNAME);


加上一个javaVO,也如下:
package com.dylan.vo;

public class BlogInfo {

	int id;
	String blogtitle;
	String blogcontent;
	String author;
	long blogtime;
	String blogtimes;
	String blogt;

	public String getBlogtimes() {
		return blogtimes;
	}

	public void setBlogtimes(String blogtimes) {
		this.blogtimes = blogtimes;
	}

	public String getBlogt() {
		return blogt;
	}

	public void setBlogt(String blogt) {
		this.blogt = blogt;
	}

	public BlogInfo() {
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getBlogtitle() {
		return blogtitle;
	}

	public void setBlogtitle(String blogtitle) {
		this.blogtitle = blogtitle;
	}

	public String getBlogcontent() {
		return blogcontent;
	}

	public void setBlogcontent(String blogcontent) {
		this.blogcontent = blogcontent;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public long getBlogtime() {
		return blogtime;
	}

	public void setBlogtime(long blogtime) {
		this.blogtime = blogtime;
	}

}



新建一个PageModel类,List是为封装BlogInfo做准备的,还增加了为分页做准备的一些属性和方法:

package com.dylan.vo;

import java.util.List;

public class PageModel {

	// 结果集
	private List list;

	// 查询总记录数
	private int totalRecords;

	// 每页多少条数据
	private int pageSize;

	// 第几页
	private int pageNo;

	/**
	 * 总页数
	 * 
	 * @return
	 */
	public int getTotalPages() {
		return (totalRecords + pageSize - 1) / pageSize;
	}

	/**
	 * 取得首页
	 * 
	 * @return
	 */
	public int getTopPageNo() {
		return 1;
	}

	/**
	 * 上一页
	 * 
	 * @return
	 */
	public int getPreviousPageNo() {
		if (pageNo <= 1) {
			return 1;
		}
		return pageNo - 1;
	}

	/**
	 * 下一页
	 * 
	 * @return
	 */
	public int getNextPageNo() {
		if (pageNo >= getBottomPageNo()) {
			return getBottomPageNo();
		}
		return pageNo + 1;
	}

	/**
	 * 取得尾页
	 * 
	 * @return
	 */
	public int getBottomPageNo() {
		return getTotalPages();
	}

	public List getList() {
		return list;
	}

	public void setList(List 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;
	}
}


事务处理的类:
package com.dylan.service;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Ar