JavaBean+Servlet+jsp实现分页显示_第1页
JavaBean+Servlet+jsp实现分页显示_第2页
JavaBean+Servlet+jsp实现分页显示_第3页
JavaBean+Servlet+jsp实现分页显示_第4页
JavaBean+Servlet+jsp实现分页显示_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、JavaBean+Servlet+jsp实现分页显示(原创)实现效果图代码:(1)JavaBeanPageDivide.javapackage com.bean;import java.io.UnsupportedEncodingException;import java.sql.*;import java.util.ArrayList;import java.util.List;import com.myutil.DBCon;public class PageDivide int sumRecord=0; /显示的总记录条数 int pageRecord=5; /每页显示的记录数 int p

2、ageNum=0; /显示的总页码数 int showPage=1; /所要显示的页码数 private Connection con=null; private PreparedStatement pstm=null; private ResultSet rs=null; private String sql="select * from category" public PageDivide() /数据库连接 con=DBCon.getConn(); try pstm=con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSE

3、NSITIVE,ResultSet.CONCUR_UPDATABLE);rs=pstm.executeQuery(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();this.sumRecord=this.getSumRecord();/this.setSumRecord(this.getSumRecord();this.pageNum=this.getPageNum(); /获取总记录数public int getSumRecord() try rs=pstm.executeQuery(

4、);rs.last(); sumRecord=rs.getRow(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();return sumRecord;public List getData(int n)List list=new ArrayList();if(n<1) n=1; if(n>=pageNum) n=pageNum; int begin=(n-1)*pageRecord+1;/获取第n页的第一条记录的位置值 try rs=pstm.executeQuery();r

5、s.absolute(begin);/将结果集的游标定位到第n页的第一条记录 for(int i=0;i<pageRecord&&(begin+i)<=sumRecord;i+) /处理最后一页的特殊情况 try int id = rs.getInt(1);String categoryName = rs.getString(2);/中文处理categoryName=new String(categoryName.getBytes("ISO8859_1"),"GB2312") ;int parentID = rs.getInt

6、(3);int layer = rs.getInt(4);String bz = rs.getString(5);/中文处理bz=new String(bz.getBytes("ISO8859_1"),"GB2312") ;/ 封装信息到实体中Category category = new Category();category.setId(id);category.setCategoryName(categoryName);category.setParentID(parentID);category.setLayer(layer);category.

7、setBz(bz);list.add(category); rs.next(); catch (UnsupportedEncodingException e) / TODO Auto-generated catch blocke.printStackTrace(); / if (rs != null)/pstm.close();/if (pstm != null)/pstm.close();/if (con != null)/con.close(); rs.close(); pstm.close(); con.close(); catch (SQLException e) / TODO Aut

8、o-generated catch blocke.printStackTrace();return list;public void setSumRecord(int sumRecord) this.sumRecord = sumRecord;public int getPageRecord() return pageRecord;public void setPageRecord(int pageRecord) this.pageRecord = pageRecord;/计算总页码数public int getPageNum() if(sumRecord%pageRecord=0) page

9、Num=sumRecord/pageRecord; else pageNum=sumRecord/pageRecord+1; System.out.println("总页数"+pageNum +" 总记录条数"+sumRecord+" 每页显示的记录数"+pageRecord);return pageNum;public void setPageNum(int pageNum) this.pageNum = pageNum;public int getShowPage() return showPage;public void set

10、ShowPage(int showPage) this.showPage = showPage; 附数据库连接类:DBCon.javapackage com.myutil;import java.sql.*;/数据库驱动加载及连接public class DBCon int x;public static Connection getConn()Connection con=null;String driver;String url;driver="com.microsoft.jdbc.sqlserver.SQLServerDriver" url="jdbc:mi

11、crosoft:sqlserver:/localhost:1433;databaseName=bookManager" try Class.forName(driver);con=DriverManager.getConnection(url,"sa","*"); catch(Exception e) e.printStackTrace(); finally return con; /数据源方式连接public static Connection getConn2()Connection con=null;String driver;Strin

12、g url;driver="sun.jdbc.odbc.JdbcOdbcDriver" url="jdbc:odbc:book" try Class.forName(driver);con=DriverManager.getConnection(url); catch(Exception e) e.printStackTrace(); finally return con; public static void main(String args)getConn2();-(2) ServletPageServlet.javapackage com.serv

13、let;import java.io.IOException;import java.io.PrintWriter;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.bean.PageDivide;public class PageServlet e

14、xtends HttpServlet /* * Constructor of the object. */public PageServlet() super();/* * Destruction of the servlet. <br> */public void destroy() super.destroy(); / Just puts "destroy" string in log/ Put your code here/* * The doGet method of the servlet. <br> * * This method is

15、called when a form has its tag value method equals to get. * * param request the request send by the client to the server * param response the response send by the server to the client * throws ServletException if an error occurred * throws IOException if an error occurred */public void doGet(HttpSe

16、rvletRequest request, HttpServletResponse response)throws ServletException, IOException response.setContentType("text/html;charset=GB2312");PrintWriter out = response.getWriter();/1.创建分页Bean对象PageDivide pd=new PageDivide();String showPage=request.getParameter("showPage");/获取所要显示的

17、页码数 字符串int n;/获取所要显示的页码数 int pageNum=pd.getPageNum();/获取总页码数if(showPage=null|"".equals(showPage)n=1;else if(Integer.parseInt(showPage)>pageNum)n=pageNum;elsen=Integer.parseInt(showPage);/获取第n页数据List list=pd.getData(n); request.setAttribute("showPage", new Integer(n);/保存当前所显示的页

18、码request.setAttribute("pageNum", new Integer(pageNum);/保存总页数request.setAttribute("data", list); /保存第n页的数据request.getRequestDispatcher("/PageDivide/pagedivide2.jsp").forward(request, response);/页面的转向out.flush();out.close();/* * The doPost method of the servlet. <br>

19、; * * This method is called when a form has its tag value method equals to post. * * param request the request send by the client to the server * param response the response send by the server to the client * throws ServletException if an error occurred * throws IOException if an error occurred */pu

20、blic void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException this.doGet(request, response);/* * Initialization of the servlet. <br> * * throws ServletException if an error occurs */public void init() throws ServletException / Put your code here

21、-(3) Jsp显示页面pagedivide2.jsp<% page language="java" import="java.util.*,com.bean.*" pageEncoding="GB2312"%><% page contentType="text/html;GB2312" %><html> <head> <title>My JSP 'pagedivide2.jsp' starting page</title> &

22、lt;/head> <body> <%! int showPage=1; int pageNum=0; %> <% Integer spstr=(Integer)request.getAttribute("showPage");/获取当前所显示的页码 Integer pnstr=(Integer)request.getAttribute("pageNum");/获取总页数 List list=(ArrayList)request.getAttribute("data"); /获取第n页的数据 /处理当前页码值 if(spstr=

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论