本文结合一个小demo主要介绍如何利用原生的jdbc实现分页技术。在我的上一篇博客中,我已经介绍了如何利用struts2搭建一个简单的图书检索系统,并实现检索功能,本文主要介绍如何对查询到的结果进行分页显示。
利用jdbc进行分页显示的核心要点:
①.sql语句要进行特殊处理使其能查询出按一定顺序排列的第n行到第m行,形如:
select *from (select t.*,rownum rn from (select *from 表 where 条件 order by 字段)t)a where a.rn>=搜索开始行 and a.rn<= 搜索结束行
②.建好分页对应的实体类,实现相关方法。
具体实现代码如下:
1.新建个page类,实现分页的关键实体类
package com.books.database.page;
public class Page {
public final static int PAGESIZE = 10;
public final static int PAGENUM = 1;
private int total; //记录总数
private int pageSize=PAGESIZE; //每页的最大记录数
private int totalPage; //总页数
private int startItems; //开始的记录数
private int endItems; //结束的记录数
private int pageNum=PAGENUM; //当前页码
public int getEndItems() {
return endItems;
}
public void setEndItems(int endItems) {
this.endItems = endItems;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getStartItems() {
return startItems;
}
public void setStartItems(int startItems) {
this.startItems = startItems;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
/*
* 根据pageSize、pageNum、total计算开始行、结束行和总页数
*/
public void calculate(Page page){
int firstItem=page.getPageSize()*(page.getPageNum()-1)+1; //当前查询的开始行
int lastItem=page.getPageSize()*page.getPageNum(); //当前查询的结束行
page.setStartItems(firstItem);
page.setEndItems(lastItem);
//获取总页数
if(page.getTotal()/page.getPageSize()<=0){
page.setTotalPage(1);
}else{
if(page.getTotal()%page.getPageSize()==0){
page.setTotalPage(page.getTotal()/page.getPageSize());
}else{
page.setTotalPage((page.getTotal()/page.getPageSize())+1);
}
}
}
}
2.Book类继承该Page类
package com.books.vo;
import com.books.database.page.Page;
/*
* 实体类Book
*/
public class Book extends Page{
private String bookName; //书名
private String author; //作者
private String bookId; //ISBN
private String seriesBook; //丛书
private String publishTime; //出版时间
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getBookId() {
return bookId;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
public String getSeriesBook() {
return seriesBook;
}
public void setSeriesBook(String seriesBook) {
this.seriesBook = seriesBook;
}
public String getPublishTime() {
return publishTime;
}
public void setPublishTime(String publishTime) {
this.publishTime = publishTime;
}
}
3.action类的代码:
package com.books.search;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.books.common.BaseAction;
import com.books.database.page.Page;
import com.books.vo.Book;
public class BooksSearchAction extends BaseAction{
private Book book =new Book();
private Page page=new Page();
private BookSearchFacade bookSearchFacade=new BookSearchFacade();
public String booksSearch(){
try {
paging();
request.setAttribute("page", book);
} catch (Exception e) {
System.out.println(e.toString());
}
return "searchPage";
}
/*
* 查询
*/
@SuppressWarnings("null")
public void search(){
try {
JSONArray json1 =null;
JSONObject json2 =null;
JSONObject allJson =new JSONObject();
initBook();
List
4.server类的代码如下,由于没有使用框架,因此在sql语句写起来要麻烦一些:
package com.books.search;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import com.books.database.dao.CommonDao;
import com.books.vo.Book;
public class BookSearchServer {
private CommonDao dao=new CommonDao();
/*
* 图书检索
*/
public List
5.dao层新增获取查询结果总数的方法:
package com.books.database.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CommonDao {
/*
* 连接数据库
* 注意:这个需要自己配置!!!
*/
public Connection connection() throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver"); // 加载Oracle驱动程序
String url = "jdbc:oracle:thin:@10.157.244.72:1521:hrdbtest"; // 连接使用的url
String user = "recruit"; // 数据库用户名
String password = "1qaz@WSX"; // 密码
Connection con = DriverManager.getConnection(url, user, password);// 获取连接
return con;
}
/*
* 原生jdbc执行查询
*/
public List> excuteSQL(String sql) throws SQLException, ClassNotFoundException {
List> list=new ArrayList>();
Connection con =connection();
Statement sta = con.createStatement();
ResultSet rs = sta.executeQuery(sql);
ResultSetMetaData md = rs.getMetaData(); //获得结果集结构信息,元数据
int columnCount = md.getColumnCount(); //获得列数
while(rs.next()){
Map rowData=new HashMap();
for(int i=1;i<=columnCount;i++){
//String key=md.getColumnName(i);//获得表头
String orderKey =String.valueOf(i);//为了使hashmap中的数据存储顺序和数据库一致
Object value=null;
if(rs.getObject(i)!=null){
value=rs.getObject(i).toString();//为了解决从数据库中取出的Date格式的数据不能转化为json格式的问题
}
rowData.put(orderKey, value);
}
list.add(rowData);
}
con.close();
return list;
}
/*
* 计算数据总量
*/
public int getTotalData(String sql) throws ClassNotFoundException, SQLException{
Connection con =connection();
Statement sta = con.createStatement();
ResultSet rs = sta.executeQuery(sql);
int total=0;
while(rs.next()){
total++;
}
return total;
}
}
6.jsp代码
<%@ page language="java" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
图书检索系统
图书检索系统
书名:
作者:
ISBN:
丛书:
出版时间:
共条
当前页码:/
每页行数:
ISBN 书名 作者 丛书 出版时间
7.运行结果: