这个功能一共创建了两个javabean组件和一个JSp页面显示分页页面,第一个是处理以数据库连接的javabean,第一个javabean是处理分页查询结果的代码,第三个jsp是调用第二个javabean,显示分页查询结果!
//下面是连接mySQL数据库的一个javabean的代码(可以更改下面的数据库,不影响代码的执行):
package data; import java.sql.*; public class LoginData{ Connection conn=null; public LoginData(){ this.connect(); } public Connection getConn(){ return this.conn; } public boolean connect(){ try{ //使用JDBC桥创建数据库连接 Class.forName("org.gjt.mm.mysql.Driver").newInstance(); //使用DriverManager类的getConnection()方法建立连接 //第一个参数定义用户名,第二个参数定义密码 this.conn=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/logindemo?useUnicode=true&characterEncoding=gb2312","root","123456"); }catch(Exception ex){ ex.printStackTrace(); return false; } return true; } }
//分页查询处理javabean
package split; import java.sql.*; import java.util.*; import data.LoginData; public class splitPage { private Connection conn=null; private Statement stmt=null; private ResultSet rs=null; private ResultSetMetaData rsmd=null; //sql 查询语句 private String sqlStr; //总纪录数目 public int rowCount; //所分得逻辑页数 public int pageCount; //每页显示的纪录数目 private int pageSize; //定义表的列数目 private int columnCount; public void initialize(String sqlStr,int pageSize) { this.sqlStr=sqlStr; this.pageSize=pageSize; try { LoginData loginData=new data.LoginData(); this.conn=loginData.getConn(); this.stmt=this.conn.createStatement(); this.rs=this.stmt.executeQuery(this.sqlStr); this.rsmd=this.rs.getMetaData(); if(this.rs!=null) { this.rs.last(); this.rowCount=this.rs.getRow(); this.rs.first(); this.columnCount=this.rsmd.getColumnCount(); this.pageCount=(this.rowCount/this.pageSize==0)?(this.rowCount/this.pageSize):(this.rowCount/this.pageSize+1); } }catch(Exception ex) { ex.printStackTrace(); } } public Vector getPage(int ipage) { Vector vData=new Vector(); int n=ipage; int m=0; m=(n-1)*this.pageSize+1; try { if(this.rs!=null) { if(n!=1) { this.rs.absolute(m); } for(int i=0;i<this.pageSize;i++) { String[] sData=new String[this.columnCount]; for(int j=0;j<this.columnCount;j++) { sData[j]=this.rs.getString(j+1); } if(sData==null) { break; } vData.addElement(sData); this.rs.next(); } this.rs.close(); this.stmt.close(); this.conn.close(); } }catch(Exception ex) { ex.printStackTrace(); } return vData; } //获得页面总数 public int getPageCount() { return this.pageCount; } //获得数据表中总纪录数 public int getRowCount() { return this.rowCount; } }
//jsp显示分页查询页面
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> <%@ page import="java.io.*" %> <%@ page import="java.util.*" %> <%@ page import="data.*"%> <jsp:useBean id="pages" scope="page" class="split.splitPage" /> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%! //显示每页的纪录数 int pageSize=10; String sqlStr=""; //当前页 int showPage=1; %> <% sqlStr="select * from userinfo order by id "; String strPage=null; //获得跳转到的页面 pages.initialize(sqlStr,pageSize); strPage=request.getParameter("showPage"); if(strPage==null){ showPage=1; }else{ try{ showPage=Integer.parseInt(strPage); }catch(NumberFormatException ex){ showPage=1; } if(showPage<1){ showPage=1; } if(showPage>pages.getPageCount()){ showPage=pages.getPageCount(); } } //取得要显示的数据集合 Vector vData=pages.getPage(showPage); %> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>分页显示</title> </head> <body bgcolor="#ffffff" text="#000000"> <h1 align=center>个人基本信息</h1> <div align=center> <table border="1" cellspacing="0" cellpadding="0" width="80%"> <tr> <th width="20%">编号</th> <th width="40%">学号</th> <th width="40%">姓名</th> </tr> <% for(int i=0;i<vData.size();i++) { //显示数据数 String[] sData=(String[])vData.get(i); %> <tr> <td><%=sData[0]%></td> <td><%=sData[1]%></td> <td><%=sData[2]%></td> </tr> <% } %> </table> <p> <form action="word_list_javabean.jsp" method="get" target="_self"> <p>共<font color=red><%=pages.getRowCount()%></font>条 <%=pageSize%>条/页 第<font color=red><%=showPage%></font>页/共<font color=red><%=pages.getPageCount()%></font>页 [<a href="word_list_javabean.jsp?showPage=1" target="_self">首页</a>] <% //判断“上一页”链接是否要显示 if(showPage>1){ %> [<a href="word_list_javabean.jsp?showPage=<%=showPage-1%>" target="_self">上一页</a>] <% } else{ %> [上一页] <% } //判断“下一页”链接是否显示 if(showPage<pages.getPageCount()) { %> [<a href="word_list_javabean.jsp?showPage=<%=showPage+1%>" target="_self">下一页</a>] <% } else{ %> [下一页] <% } %> [<a href="word_list_javabean.jsp?showPage=<%=pages.getPageCount()%>" target="_self">尾页</a>] 转到 <select name="select"> <% for(int x=1;x<=pages.getPageCount();x++) { %> <option value="<%=x%>" <% if(showPage==x){ out.println("selected"); } %> ><%=x%></option> <% } %> </select> 页 <input type="submit" name="go" value="提交" /> </p> </form> </p> </div> </body> </htm>