本文實(shí)例講述了jsp+servlet+javabean實(shí)現(xiàn)數(shù)據(jù)分頁(yè)方法。分享給大家供大家參考,具體如下:
這里秉著且行且記的心態(tài),記錄下學(xué)習(xí)過(guò)程,學(xué)得快忘得快,生怕遺忘,以備日后使用。
用到的部分代碼是自己在網(wǎng)上查找,并自己修改,加上自己的理解。也不知道算不算原創(chuàng),只做自己學(xué)習(xí)記錄。
使用相關(guān):PostgreSQL數(shù)據(jù)庫(kù)、dom4j、JSP、Servlet
一、首先是工程格局,來(lái)個(gè)全局視圖方便讀者與自己查看與使用
思路為:
以config.xml文件記錄配置信息,以方便數(shù)據(jù)庫(kù)更改,方便移植與重用。
DOM4JUtil.java用于解析xml屬性文件以獲得需要數(shù)據(jù)
PostgreSQL_Util.java分裝數(shù)據(jù)連接與數(shù)據(jù)庫(kù)操作
PageProperties.java為表格分頁(yè)屬性javaBean
PageProperties.java封裝分頁(yè)操作
Page.java為分頁(yè)主要操作
tablePage.jsp為效果顯示界面
用到的第三方j(luò)ar包:
dom4j-1.6.1.jar用于xml文件解析
postgresql-9.3-1101.jdbc4.jar用于JDBC連接postgreSQL數(shù)據(jù)庫(kù)
分頁(yè)效果如下:能通過(guò)點(diǎn)擊上頁(yè)下頁(yè)實(shí)現(xiàn)翻頁(yè),輸入指定頁(yè)面跳轉(zhuǎn)(超出范圍跳轉(zhuǎn)到第1或最后頁(yè))。具體實(shí)現(xiàn)請(qǐng)參見(jiàn)詳細(xì)代碼,我都貼上來(lái)了。小菜鳥(niǎo)一名,處于正在學(xué)習(xí)階段,有大神能指點(diǎn)下當(dāng)然更好,希望不吝賜教!
二、具體代碼實(shí)現(xiàn)
1、config.xml數(shù)據(jù)庫(kù)連接信息屬性文件
?xml version="1.0" encoding="utf-8"?>
!DOCTYPE postgres[
!ELEMENT postgres (driver,url,username,pwd)>
!ELEMENT driver (#PCDATA)>
!ELEMENT url (#PCDATA)>
!ELEMENT username (#PCDATA)>
!ELEMENT pwd (#PCDATA)>
]>
postgres>
driver>org.postgresql.Driver/driver>
url>jdbc:postgresql://localhost:5432/java/url>
username>admin/username>
pwd>k42jc/pwd>
/postgres>
2、DOM4JUtil.java
package util;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* 用于解析xml屬性文件
* @author JohsonMuler
*
*/
public class DOM4JUtil {
private static Element root=null;
static{//靜態(tài)代碼塊
//創(chuàng)建解析對(duì)象
SAXReader sr=new SAXReader();
//獲取當(dāng)前工程路徑
// String url=System.getProperty("user.dir");
String url=DOM4JUtil.class.getResource("").getPath();
// System.out.println(url);
try {
//通過(guò)文件路徑獲取配置文件信息
Document doc=sr.read(url+"config.xml");
//獲取根節(jié)點(diǎn)
root=doc.getRootElement();
} catch (DocumentException e) {
e.printStackTrace();
}
}
public static String getPostgresData(String str){
//以根節(jié)點(diǎn)為基礎(chǔ),獲取配置文件數(shù)據(jù)
Element e=root.element(str);
String data=e.getText();
return data;
}
public static void main(String[] args) {
// String url=DOM4JUtil.class.getResource("..").getPath();
// System.out.println(System.getProperty("user.dir"));
// System.out.println(url);
String driver=getPostgresData("driver");
String url=getPostgresData("url");
System.out.println(driver);
System.out.println(url);
}
}
3、PostgreSQL_Util.java
package util;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class PostgreSQL_Util {
private static DOM4JUtil dom=new DOM4JUtil();
private static Connection c=null;
private static ResultSet rs=null;
private static String driver=dom.getPostgresData("driver");
private static String url=dom.getPostgresData("url");
private static String username=dom.getPostgresData("username");
private static String pwd=dom.getPostgresData("pwd");
public PostgreSQL_Util(){
try {
Class.forName(driver);
c=DriverManager.getConnection(url);
} catch (ClassNotFoundException e) {
System.out.println("未找到指定類(lèi):"+e.getMessage());
} catch (SQLException e) {
System.out.println("獲取連接異常:"+e.getMessage());
}
}
/**
* 數(shù)據(jù)查詢(xún)方法(Statement)
* @param sql
* @return
* @throws SQLException
*/
public ResultSet executeQuery(String sql) throws SQLException{
Statement s=c.createStatement();
rs=s.executeQuery(sql);
return rs;
}
/**
* 重載方法(PreparedStatement)
* @param sql
* @param list
* @return
* @throws SQLException
*/
public ResultSet executeQuery(String sql,ListObject> list) throws SQLException{
PreparedStatement ps=c.prepareStatement(sql);
for(int i=0;ilist.size();i++){
System.out.println(list.get(i));
System.out.println(i+1);
ps.setObject(i+1, list.get(i));
}
rs=ps.executeQuery();
c.close();
return rs;
}
/**
* 數(shù)據(jù)更新方法(添加,刪除,更改)(Statement)
* @param sql
* @throws SQLException
*/
public int executeUpdate(String sql) throws SQLException{
Statement s=c.createStatement();
int i=s.executeUpdate(sql);
c.close();
return i;
}
/**
* 重載方法(PreparedStatement)
* @param sql
* @param list
* @throws SQLException
*/
public int executeUpdate(String sql,ListObject> list) throws SQLException{
PreparedStatement ps=c.prepareStatement(sql);
for(int i=0;ilist.size();i++){
ps.setObject(i+1, list.get(i));
}
int i=ps.executeUpdate();
c.close();
return i;
}
/**
* 單獨(dú)的獲取連接
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
public static Connection getConnection() throws ClassNotFoundException, SQLException{
Class.forName(driver);
c=DriverManager.getConnection(url);
return c;
}
}
4、PageProperties.java
package bean;
import java.sql.ResultSet;
public class PageProperties {
private int currentPage;//當(dāng)前頁(yè)號(hào)
private int totalPages;//總頁(yè)數(shù)
private int totalRecords;//總數(shù)據(jù)條數(shù)
private ResultSet rs;//動(dòng)態(tài)結(jié)果集
public PageProperties() {
super();
}
public PageProperties(int currentPage, int totalPages, int totalRecords,
ResultSet rs) {
super();
this.currentPage = currentPage;
this.totalPages = totalPages;
this.totalRecords = totalRecords;
this.rs = rs;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
public ResultSet getRs() {
return rs;
}
public void setRs(ResultSet rs) {
this.rs = rs;
}
}
5、TablePage.java
package bean;
import java.sql.ResultSet;
public class PageProperties {
private int currentPage;//當(dāng)前頁(yè)號(hào)
private int totalPages;//總頁(yè)數(shù)
private int totalRecords;//總數(shù)據(jù)條數(shù)
private ResultSet rs;//動(dòng)態(tài)結(jié)果集
public PageProperties() {
super();
}
public PageProperties(int currentPage, int totalPages, int totalRecords,
ResultSet rs) {
super();
this.currentPage = currentPage;
this.totalPages = totalPages;
this.totalRecords = totalRecords;
this.rs = rs;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
public ResultSet getRs() {
return rs;
}
public void setRs(ResultSet rs) {
this.rs = rs;
}
}
6、Page.java這是主要處理類(lèi),Servlet
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.PostgreSQL_Util;
import bean.PageProperties;
import bean.TablePage;
public class Page extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
/**
* 通過(guò)TablePage設(shè)置分頁(yè)屬性
*
*/
TablePage tb=new TablePage();
//獲取當(dāng)前表格顯示的頁(yè)碼
int currentPage=tb.currentPage(tb.getStrPage(request, "page"));
System.out.println(currentPage);
//設(shè)置每頁(yè)顯示數(shù)據(jù)條數(shù)
tb.setPageRecord(10);//設(shè)置每頁(yè)顯示10條數(shù)據(jù)
/**
* 通過(guò)xxSQL_Util設(shè)置JDBC連接及數(shù)據(jù)處理
*/
PostgreSQL_Util postgres=new PostgreSQL_Util();
try {
ResultSet rs_count=postgres.executeQuery("select count(*) as c from student");
rs_count.next();
//獲得總的數(shù)據(jù)條數(shù)
int totalRecords=rs_count.getInt("c");
//根據(jù)數(shù)據(jù)表的總數(shù)據(jù)條數(shù)獲取頁(yè)面顯示表格的總頁(yè)數(shù)
int totalPages=tb.getTotalPages(totalRecords);
if(currentPage>totalPages){
currentPage=totalPages;//保證最后一頁(yè)不超出范圍
}
//根據(jù)數(shù)據(jù)庫(kù)表信息和當(dāng)前頁(yè)面信息獲得動(dòng)態(tài)結(jié)果集
ResultSet rs=tb.getPageResultSet(postgres.executeQuery("select * from student"), currentPage);
/**
* 將數(shù)據(jù)加入javaBean
*/
PageProperties pp=new PageProperties(currentPage, totalPages, totalRecords, rs);
/**
* 將javaBean轉(zhuǎn)發(fā)至前端
*/
request.setAttribute("result", pp);
request.getRequestDispatcher("tablePage.jsp").forward(request, response);
} catch (SQLException e) {
System.out.println("Class Page:"+e.getMessage());
// e.printStackTrace();
}
}
}
7、tablePage.jsp前臺(tái)顯示效果
%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
%@page import="java.sql.ResultSet"%>
%@page import="bean.PageProperties"%>
%@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>
title>簡(jiǎn)單數(shù)據(jù)分頁(yè)/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">
-->
/head>
body>
table>
tr>
td>姓名/td>
td>性別/td>
td>年齡/td>
td>分?jǐn)?shù)/td>
/tr>
%
PageProperties pp=(PageProperties)request.getAttribute("result");
ResultSet rs=pp.getRs();
%>
%
int i=1;
while(rs.next()){
%>
tr>
td>%=rs.getObject(1) %>/td>
td>%=rs.getObject(2) %>/td>
td>%=rs.getObject(3) %>/td>
td>%=rs.getObject(4) %>/td>
/tr>
%
i++;
if(i>10)
break;
}
%>
br/>
span>%=pp.getTotalPages() %>頁(yè)/span>
span>共%=pp.getTotalRecords() %>條數(shù)據(jù)/span>
span>本頁(yè)%=i-1 %>條/span>
span>第%=pp.getCurrentPage() %>頁(yè)/span>
p align="center">
%
if ( pp.getCurrentPage() > 1 )
{
%>a href="%=path %>/page?page=%=pp.getCurrentPage() - 1%>">上一頁(yè)/a>
%
}
%>
%
if ( pp.getCurrentPage() pp.getTotalPages() )
{
%>a href="%=path %>/page?page=%=pp.getCurrentPage() + 1%>">下一頁(yè)>>/a>
%
}
%>
input type="text" name="input_text" id="input_text" size="1" />
input type="button" name="skip" id="skip" value="跳轉(zhuǎn)" onclick="skip();"/>
script>
function skip(){
var v=document.getElementById("input_text").value;
location.href="page?page="+v;
}
/script>
/p>
/table>
/body>
/html>
初步看,感覺(jué)后臺(tái)代碼實(shí)在是繁瑣,但這是考慮到程序健壯性與可移植性,方便代碼重用。以后要用,根據(jù)自己的需要在屬性文件(config.xml)中配置相關(guān)JDBC驅(qū)動(dòng),在jsp頁(yè)面通過(guò)request獲得后臺(tái)Servlet(Page.jsp)的轉(zhuǎn)發(fā)結(jié)果("result"),結(jié)合頁(yè)面屬性(PageProperties.java類(lèi))即可實(shí)現(xiàn)效果。
當(dāng)然,這也是因?yàn)閭€(gè)人學(xué)習(xí),傾向于多用點(diǎn)東西。
希望本文所述對(duì)大家jsp程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- jsp頁(yè)面數(shù)據(jù)分頁(yè)模仿百度分頁(yè)效果(實(shí)例講解)
- nodejs個(gè)人博客開(kāi)發(fā)第六步 數(shù)據(jù)分頁(yè)
- JSP數(shù)據(jù)分頁(yè)導(dǎo)出下載顯示進(jìn)度條樣式
- JS代碼實(shí)現(xiàn)table數(shù)據(jù)分頁(yè)效果
- 無(wú)JS,完全php面向過(guò)程數(shù)據(jù)分頁(yè)實(shí)現(xiàn)代碼
- JSP數(shù)據(jù)庫(kù)操數(shù)據(jù)分頁(yè)顯示
- jquery+json實(shí)現(xiàn)數(shù)據(jù)列表分頁(yè)示例代碼
- js前臺(tái)分頁(yè)顯示后端JAVA數(shù)據(jù)響應(yīng)
- JS實(shí)現(xiàn)table表格數(shù)據(jù)排序功能(可支持動(dòng)態(tài)數(shù)據(jù)+分頁(yè)效果)
- js對(duì)象實(shí)現(xiàn)數(shù)據(jù)分頁(yè)效果