jsp+servlet+javabean实现数据分页方法完整实例
本文实例讲述了jsp+servlet+javabean实现数据分页方法。分享给大家供大家参考,具体如下:
这里秉着且行且记的心态,记录下学习过程,学得快忘得快,生怕遗忘,以备日后使用。
用到的部分代码是自己在网上查找,并自己修改,加上自己的理解。也不知道算不算原创,只做自己学习记录。
使用相关:postgresql数据库、dom4j、jsp、servlet
一、首先是工程格局,来个全局视图方便读者与自己查看与使用
思路为:
以config.xml文件记录配置信息,以方便数据库更改,方便移植与重用。
dom4jutil.java用于解析xml属性文件以获得需要数据
postgresql_util.java分装数据连接与数据库操作
pageproperties.java为表格分页属性javabean
pageproperties.java封装分页操作
page.java为分页主要操作
tablepage.jsp为效果显示界面
用到的第三方jar包:
dom4j-1.6.1.jar用于xml文件解析
postgresql-9.3-1101.jdbc4.jar用于jdbc连接postgresql数据库
分页效果如下:能通过点击上页下页实现翻页,输入指定页面跳转(超出范围跳转到第1或最后页)。具体实现请参见详细代码,我都贴上来了。小菜鸟一名,处于正在学习阶段,有大神能指点下当然更好,希望不吝赐教!
二、具体代码实现
1、config.xml数据库连接信息属性文件
<?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{//静态代码块 //创建解析对象 saxreader sr=new saxreader(); //获取当前工程路径 // string url=system.getproperty("user.dir"); string url=dom4jutil.class.getresource("").getpath(); // system.out.println(url); try { //通过文件路径获取配置文件信息 document doc=sr.read(url+"config.xml"); //获取根节点 root=doc.getrootelement(); } catch (documentexception e) { e.printstacktrace(); } } public static string getpostgresdata(string str){ //以根节点为基础,获取配置文件数据 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("未找到指定类:"+e.getmessage()); } catch (sqlexception e) { system.out.println("获取连接异常:"+e.getmessage()); } } /** * 数据查询方法(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,list<object> list) throws sqlexception{ preparedstatement ps=c.preparestatement(sql); for(int i=0;i<list.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; } /** * 数据更新方法(添加,删除,更改)(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,list<object> list) throws sqlexception{ preparedstatement ps=c.preparestatement(sql); for(int i=0;i<list.size();i++){ ps.setobject(i+1, list.get(i)); } int i=ps.executeupdate(); c.close(); return i; } /** * 单独的获取连接 * @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;//当前页号 private int totalpages;//总页数 private int totalrecords;//总数据条数 private resultset rs;//动态结果集 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;//当前页号 private int totalpages;//总页数 private int totalrecords;//总数据条数 private resultset rs;//动态结果集 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这是主要处理类,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(); /** * 通过tablepage设置分页属性 * */ tablepage tb=new tablepage(); //获取当前表格显示的页码 int currentpage=tb.currentpage(tb.getstrpage(request, "page")); system.out.println(currentpage); //设置每页显示数据条数 tb.setpagerecord(10);//设置每页显示10条数据 /** * 通过xxsql_util设置jdbc连接及数据处理 */ postgresql_util postgres=new postgresql_util(); try { resultset rs_count=postgres.executequery("select count(*) as c from student"); rs_count.next(); //获得总的数据条数 int totalrecords=rs_count.getint("c"); //根据数据表的总数据条数获取页面显示表格的总页数 int totalpages=tb.gettotalpages(totalrecords); if(currentpage>totalpages){ currentpage=totalpages;//保证最后一页不超出范围 } //根据数据库表信息和当前页面信息获得动态结果集 resultset rs=tb.getpageresultset(postgres.executequery("select * from student"), currentpage); /** * 将数据加入javabean */ pageproperties pp=new pageproperties(currentpage, totalpages, totalrecords, rs); /** * 将javabean转发至前端 */ 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前台显示效果
<%@ 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>简单数据分页</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>分数</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() %>页</span> <span>共<%=pp.gettotalrecords() %>条数据</span> <span>本页<%=i-1 %>条</span> <span>第<%=pp.getcurrentpage() %>页</span> <p align="center"> <% if ( pp.getcurrentpage() > 1 ) { %><a href="<%=path %>/page?page=<%=pp.getcurrentpage() - 1%>"><<上一页</a> <% } %> <% if ( pp.getcurrentpage() < pp.gettotalpages() ) { %><a href="<%=path %>/page?page=<%=pp.getcurrentpage() + 1%>">下一页>></a> <% } %> <input type="text" name="input_text" id="input_text" size="1" /> <input type="button" name="skip" id="skip" value="跳转" onclick="skip();"/> <script> function skip(){ var v=document.getelementbyid("input_text").value; location.href="page?page="+v; } </script> </p> </table> </body> </html>
初步看,感觉后台代码实在是繁琐,但这是考虑到程序健壮性与可移植性,方便代码重用。以后要用,根据自己的需要在属性文件(config.xml)中配置相关jdbc驱动,在jsp页面通过request获得后台servlet(page.jsp)的转发结果("result"),结合页面属性(pageproperties.java类)即可实现效果。
当然,这也是因为个人学习,倾向于多用点东西。
希望本文所述对大家jsp程序设计有所帮助。