此分页程序只用到三个文件test.jsp(用于显示分页结果的JSP页面)和Pagination.java(用于封装分页程序)和DBConnect.java(用于连接SqlServer 2000数据库的JAVA类),和一个简单数据库user的表username,测试用的web发布服务器为Tomcat 5.5.20).而且DBConnect.java和Pagination.java放在WEB-INF下的classes目录下(注意,如果没有的话就新建一个),数据库用的是SqlServer 2000.
1、create database username ----------建立数据库username表
create table username (name varchar(25));---------建立数据表username有一个字段name类型是字符型( 有关sql的操作请注意: D:\学习资料\SQL server\课件\逍湘 SQL大全.doc 的数据库方面知识.)
2、DBConnect.java---------------用于连接sqlserver 2000数据库
package net.xiaoxiang; import java.sql.*; /** * 数据库连接类---(JDBC) * @author 逍湘 */ public class DBConnect { // 设置驱动变量 String drivename = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; // 创建连接,数据库名user String URL = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=user"; // 这里替换成你自已的数据库用户名 String user = "sa"; // 这里替换成你自已的数据库用户密码 String password = "123"; Connection conn = null; ResultSet rs = null; /** * 构造方法创建数据库连接 * */ public DBConnect ( ){ try { Class.forName ( drivename );// 创建数据库驱动 conn = DriverManager.getConnection ( URL, user, password );// 创建连接 } catch ( java.lang.ClassNotFoundException e ) { System.out.println ( "Jdbc_conn():" + e.getMessage ( ) ); } catch ( SQLException ex ) { System.out.println ( "sql.executeUpdate:" + ex.getMessage ( ) ); } } /** * 数据更新方法 * @param sql * @throws Exception */ public void executeUpdate ( String sql ) throws Exception { sql = new String ( sql.getBytes ( "GBK" ), "ISO8859_1" );// 字符的转换 try { Statement stmt = conn.createStatement ( ); stmt.executeUpdate ( sql ); conn.close ( ); stmt.close ( ); } catch ( SQLException ex ) { System.out.println ( "sql.executeUpdate:" + ex.getMessage ( ) ); } } /** * 数据查询方法 * @param sql * @return * @throws Exception */ public ResultSet executeQuery ( String sql ) throws Exception { rs = null; sql = new String ( sql.getBytes ( "GBK" ), "ISO8859_1" );// 字符的转换 try { Statement stmt = conn.createStatement ( );// 数据操作对象 rs = stmt.executeQuery ( sql );// 执行sql // conn.close();// 关闭连接 // stmt.close();// 关闭对象 } catch ( SQLException ex ) { System.out.println ( "sql.executeQuery:" + ex.getMessage ( ) ); } return rs; } /** * 测试 * @param args */ public static void main ( String args[] ) { try { ResultSet rs_count = new DBConnect ( ) .executeQuery ( "select count(*) as t from username" );// 传递进数据库处理的javabean rs_count.next ( ); int resultconts = rs_count.getInt ( "t" );// 取得总的数据数 System.out.print ( resultconts ); } catch ( Exception ex ) { System.out.println ( "sql.executeQuery:ok" + ex.getMessage ( ) ); } } }
用Pagination.java封装分页类,在test.jsp里显示
3、Pagination.java--------封装分页的类
package net.xiaoxiang; import java.sql.*; import javax.servlet.http.*; /** * 封装分页程序 * @author 逍湘 */ public class Pagination { private String strPage = null; // page参数变量 private int current_Pages; // 当前页数 private int page_record; // 设置每页显示记录数 private int total_Pages; // 总页数 /** * 取得xxx.jsp页面文件里的xxx.jsp?page=<%=current_Pages-1%> * 或是page=<%=current_Pages+1%>的值给变量strPage * @param request * @param page * 为跳转到的页号 * @return strPage */ public String strPage ( HttpServletRequest request, String page ) { try { strPage = request.getParameter ( page );// request对象取得page的值 } catch ( Exception e ) { System.out.println ( "delcolumn" + e.getMessage ( ) ); } return strPage; } /** * 设置要显示的当前页数 * @param strPage * @return current_Pages(返回页面数) */ public int current_Pages ( String strPage ) { try { if ( strPage == null ) { // 默认没有就设置是第一页 current_Pages = 1; } else { current_Pages = Integer.parseInt ( strPage );// 取得strPage的整数值 if ( current_Pages < 1 ) // 如果小于1,同样返回是第一页 current_Pages = 1; } } catch ( Exception e ) { System.out.print ( "current_Pages" ); } return current_Pages;// 返回页面数 } /** * @param page_record * 设置每页要显示的记录数 */ public void setPage_record ( int page_record ) { this.page_record = page_record; } /** * 取得总页数 * @param total_record * 总记录数(查询数据库获得) * @return total_Pages 返回总页数 */ public int getTotal_Pages ( int total_record ) { int test;// 变量 test = total_record % page_record;// 取得余数 if ( test == 0 ) total_Pages = total_record / page_record;// 每页显示的整数 else total_Pages = total_record / page_record + 1;// 不是整数就加一 return total_Pages; } /** * 结果集的返回 * @param rs * 结果集 * @param current_Pages * 页数 * @return rs 结果集 */ public ResultSet getPageSet ( ResultSet rs, int current_Pages ) { if ( current_Pages == 1 ) { return rs;// 如果就一页,就返回这个rs } else { int i = 1; try { while ( rs.next ( ) ) { i = i + 1; if ( i > ( ( current_Pages - 1 ) * page_record ) ) break;// 退出 } return rs;// 从退出开始将结果集返回 } catch ( Exception e ) { System.out.print ( e.getMessage ( ) ); } } return rs; } }
4.test.jsp --------显示页面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@ page import="java.sql.*"%> <!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> <jsp:useBean id="m_pages" scope="page" class="bean.Pagination"></jsp:useBean> <jsp:useBean id="sql" scope="page" class="bean.DBConnect" /> <% int curPages = m_pages.current_Pages ( m_pages.strPage ( request, "page" ) ); m_pages.setPage_record ( 10 );//设置每页显示10条 %> <% //传递进数据库处理的javabean ResultSet rs_count = sql.executeQuery ( "select count(*) as t from username" ); rs_count.next ( ); int resultconts = rs_count.getInt ( "t" );//取得总的数据数 int totalPages = m_pages.getTotal_Pages ( resultconts );//取出总页数 //获取指针的结果集参数是(结果集,页数) ResultSet rs = m_pages.getPageSet ( sql .executeQuery ( "select * from username" ), curPages ); %> <p> 分类表 </p> <table border="1"> <tr> <td> 姓名 </td> </tr> <% int i = 1; %> <% while ( rs.next ( ) ) { %> <tr> <!-- <td> <%-- <%=rs.getString("id")%> --%> </td> --> <td><%=rs.getString ( "name" )%> </td> </tr> <% i = i + 1; if ( i > 10 ) break; } %> </table> <p align="center"> <% if ( curPages > 1 ) { %><a href="test.jsp?page=<%=curPages - 1%>">上一页</a> <% } %> <% if ( curPages < totalPages ) { %><a href="test.jsp?page=<%=curPages + 1%>">下一页</a> <% } %> </p> </body> </html>