JSP分页实现(Javabean+JSP)
本例实现jsp分页,可设定最大显示页数,每页显示条目数,在选取页码时到数据库中取相应页码内容并显示。
databaseBean类为分页中的数据库操作bean,包含数据库连接,查询结果,得到结果页数,返回指定页码内容的结果集等功能。
后面给出测试jsp页面。
分页相关数据库操作bean
package com.test.bean;
import java.sql.*;
public class databaseBean {
// 数据库连接字符串
private String DBLocation = "jdbc:mysql://localhost:3306/test?user=root&password=root&useUnicode=true&characterEncoding=GB2312";
// 数据库连接驱动
private String DBDriver = "com.mysql.jdbc.Driver";
// 数据库连接
private Connection conn = null;
public databaseBean() {
}
public String getDBLocation() {
return (DBLocation);
}
public void setDBLocation(String location) {
DBLocation = location;
}
public String getDBDriver() {
return (DBDriver);
}
public void setDBDriver(String driver) {
DBDriver = driver;
}
public Connection getconn() {
return (conn);
}
public void setconn(Connection conn) {
this.conn = conn;
}
/**
* 建立连接
*
* @return 连接建立状态
*/
public String DBConnect() {
String strExc = "Success!";
try {
Class.forName(DBDriver);
conn = DriverManager.getConnection(DBLocation);
} catch (ClassNotFoundException e) {
strExc = "数据库驱动没有找到,错误提示:<br>" + e.toString();
} catch (SQLException e) {
strExc = "sql语句错误,错误提示<br>" + e.toString();
} catch (Exception e) {
strExc = "错误提示:<br>" + e.toString();
}
return (strExc);
}
/**
* 断开连接
*
* @return 连接断开状态
*/
public String DBDisconnect() {
String strExc = "Success!";
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
strExc = e.toString();
}
return (strExc);
}
/**
* 通过传入sql语句来返回一个结果集
*
* @param sql
* sql查询语句
* @return 查询结果集
* @throws SQLException
* @throws Exception
*/
public ResultSet query(String sql) throws SQLException, Exception {
ResultSet rs = null;
if (conn == null) {
DBConnect();
}
if (conn == null) {
rs = null;
} else {
try {
Statement s = conn.createStatement();
rs = s.executeQuery(sql);
} catch (SQLException e) {
throw new SQLException("Cound not execute query.");
} catch (Exception e) {
throw new Exception("Cound not execute query.");
}
}
return (rs);
}
/**
* 执行sql语句,并返回结果的页数
*
* @param sql
* sql查询语句
* @param pageSize
* 每页所显示的结果数目
* @return 总页数
*/
public int getTotalPage(String sql, int pageSize) {
ResultSet rs = null; // 查询后的结果集
int totalRows = 0; // 结果集中条目数
int pages = 0; // 总页数
if (conn == null) {
DBConnect();
}
if (conn == null) {
rs = null;
} else {
try {
Statement s = conn.createStatement();
rs = s.executeQuery(sql);
while (rs.next())
totalRows++;
} catch (SQLException e) {
}
pages = ((totalRows - 1) / pageSize + 1);
rs = null;
}
return pages;
}
/**
* 得到指定页码的查询结果集
*
* @param sql
* sql查询语句
* @param pageSize
* 每页显示的条目数
* @param pageNumber
* 页码
* @return 查询结果集
*/
public ResultSet getPagedRs(String sql, int pageSize, int pageNumber) {
ResultSet rs = null;
int absoluteLocation;
if (conn == null) {
DBConnect();
}
if (conn == null) {
rs = null;
} else
try {
Statement s = conn.createStatement();
// 计算出最后一行结果的编号,
// 任何编号大于这个maxrows的结果都会被drop
s.setMaxRows(pageSize * pageNumber);
rs = s.executeQuery(sql);
} catch (SQLException e) {
}
// 计算出上一页最后一个结果的编号
absoluteLocation = pageSize * (pageNumber - 1);
try {
//让结果集rs定位到本页之前的最后一个结果处
for (int i = 0; i < absoluteLocation; i++) {
rs.next();
}
} catch (SQLException e) {
}
// 返回该页要显示的结果
return (rs);
}
/**
* 执行sql语句
*
* @param sql
* sql查询语句
* @return 标识状态的字符串
*/
public String execute_sql(String sql) {
String strExc = "Success!";
if (conn != null) {
try {
PreparedStatement update;
update = conn.prepareStatement(sql);
update.execute();
} catch (SQLException e) {
strExc = e.toString();
} catch (Exception e) {
strExc = e.toString();
}
} else {
strExc = "Connection Lost!";
}
return (strExc);
}
}
package com.test.bean;
import java.sql.*;
public class databaseBean {
// 数据库连接字符串
private String DBLocation = "jdbc:mysql://localhost:3306/test?user=root&password=root&useUnicode=true&characterEncoding=GB2312";
// 数据库连接驱动
private String DBDriver = "com.mysql.jdbc.Driver";
// 数据库连接
private Connection conn = null;
public databaseBean() {
}
public String getDBLocation() {
return (DBLocation);
}
public void setDBLocation(String location) {
DBLocation = location;
}
public String getDBDriver() {
return (DBDriver);
}
public void setDBDriver(String driver) {
DBDriver = driver;
}
public Connection getconn() {
return (conn);
}
public void setconn(Connection conn) {
this.conn = conn;
}
/**
* 建立连接
*
* @return 连接建立状态
*/
public String DBConnect() {
String strExc = "Success!";
try {
Class.forName(DBDriver);
conn = DriverManager.getConnection(DBLocation);
} catch (ClassNotFoundException e) {
strExc = "数据库驱动没有找到,错误提示:<br>" + e.toString();
} catch (SQLException e) {
strExc = "sql语句错误,错误提示<br>" + e.toString();
} catch (Exception e) {
strExc = "错误提示:<br>" + e.toString();
}
return (strExc);
}
/**
* 断开连接
*
* @return 连接断开状态
*/
public String DBDisconnect() {
String strExc = "Success!";
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
strExc = e.toString();
}
return (strExc);
}
/**
* 通过传入sql语句来返回一个结果集
*
* @param sql
* sql查询语句
* @return 查询结果集
* @throws SQLException
* @throws Exception
*/
public ResultSet query(String sql) throws SQLException, Exception {
ResultSet rs = null;
if (conn == null) {
DBConnect();
}
if (conn == null) {
rs = null;
} else {
try {
Statement s = conn.createStatement();
rs = s.executeQuery(sql);
} catch (SQLException e) {
throw new SQLException("Cound not execute query.");
} catch (Exception e) {
throw new Exception("Cound not execute query.");
}
}
return (rs);
}
/**
* 执行sql语句,并返回结果的页数
*
* @param sql
* sql查询语句
* @param pageSize
* 每页所显示的结果数目
* @return 总页数
*/
public int getTotalPage(String sql, int pageSize) {
ResultSet rs = null; // 查询后的结果集
int totalRows = 0; // 结果集中条目数
int pages = 0; // 总页数
if (conn == null) {
DBConnect();
}
if (conn == null) {
rs = null;
} else {
try {
Statement s = conn.createStatement();
rs = s.executeQuery(sql);
while (rs.next())
totalRows++;
} catch (SQLException e) {
}
pages = ((totalRows - 1) / pageSize + 1);
rs = null;
}
return pages;
}
/**
* 得到指定页码的查询结果集
*
* @param sql
* sql查询语句
* @param pageSize
* 每页显示的条目数
* @param pageNumber
* 页码
* @return 查询结果集
*/
public ResultSet getPagedRs(String sql, int pageSize, int pageNumber) {
ResultSet rs = null;
int absoluteLocation;
if (conn == null) {
DBConnect();
}
if (conn == null) {
rs = null;
} else
try {
Statement s = conn.createStatement();
// 计算出最后一行结果的编号,
// 任何编号大于这个maxrows的结果都会被drop
s.setMaxRows(pageSize * pageNumber);
rs = s.executeQuery(sql);
} catch (SQLException e) {
}
// 计算出上一页最后一个结果的编号
absoluteLocation = pageSize * (pageNumber - 1);
try {
//让结果集rs定位到本页之前的最后一个结果处
for (int i = 0; i < absoluteLocation; i++) {
rs.next();
}
} catch (SQLException e) {
}
// 返回该页要显示的结果
return (rs);
}
/**
* 执行sql语句
*
* @param sql
* sql查询语句
* @return 标识状态的字符串
*/
public String execute_sql(String sql) {
String strExc = "Success!";
if (conn != null) {
try {
PreparedStatement update;
update = conn.prepareStatement(sql);
update.execute();
} catch (SQLException e) {
strExc = e.toString();
} catch (Exception e) {
strExc = e.toString();
}
} else {
strExc = "Connection Lost!";
}
return (strExc);
}
}
测试分页jsp
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<jsp:useBean id="basicDB" class="com.test.bean.databaseBean"
scope="page" />
<%
String sql; //sql语句
ResultSet rs; //结果集
int id; //记录的id
String reply, Exc;
Exc = basicDB.DBConnect(); //建立连接,若成功,则返回Success!若失败,则返回相应出错信息
if (!Exc.equals("Success!")) {
basicDB.DBDisconnect();
throw new Exception(Exc);
}
int pageSize = 10; //每页显示的数据条数
int currentPage = 1; //初始显示页的页码
int allPage = -1; //总页码数
String pages = request.getParameter("pages"); //取得页面中pages参数,此参数代表要显示的“当前页面”
if (pages != null)
currentPage = Integer.valueOf(pages).intValue(); //获取待显示的当前页面的页码
sql = "select * from userinfo order by id asc";
allPage = basicDB.getTotalPage(sql, pageSize); //得到总页码数
rs = basicDB.getPagedRs(sql, pageSize, currentPage); //得到当前页面要显示的结果集
%>
<table border="0" cellspacing="1" cellpadding="3" width="590" bgcolor="#ffffff">
<%
while (rs.next()) {
id = rs.getInt("id");//得到数据库(结果集)中id编号
%>
<tr bgcolor="#FF6600" style="color: white">
<td>
Name:<%=rs.getString("name")%></td>
<td>
Password:<%=rs.getString("password")%></td>
</tr>
<%
}
%>
<tr>
<td height="1"></td>
</tr>
<tr>
<td colspan=4 align=right bgcolor="#FF6600" style="color: white;">
现在是第<%=currentPage%>页,
<%
if (currentPage > 1) {
%>
<!--如果不在第一页,则显示出“首页”链接-->
<A HREF="admin_show.jsp?pages=<%=(currentPage - 1)%>">首页</A>
<%
}
for (int i = 1; i <= allPage; i++) {
//显示出1、2、3、4……到最后一页的链接
out.println("<a href=admin_show.jsp?pages=" + i + ">" + i
+ "</a>");
}
%>
<%
if (currentPage < allPage) {
%>
<!--如果不在最后一页,则显示出“末页”链接-->
<A HREF="admin_show.jsp?pages=<%=(currentPage + 1)%>">末页</A>
<%
}
%>
</td>
</tr>
</table>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<jsp:useBean id="basicDB" class="com.test.bean.databaseBean"
scope="page" />
<%
String sql; //sql语句
ResultSet rs; //结果集
int id; //记录的id
String reply, Exc;
Exc = basicDB.DBConnect(); //建立连接,若成功,则返回Success!若失败,则返回相应出错信息
if (!Exc.equals("Success!")) {
basicDB.DBDisconnect();
throw new Exception(Exc);
}
int pageSize = 10; //每页显示的数据条数
int currentPage = 1; //初始显示页的页码
int allPage = -1; //总页码数
String pages = request.getParameter("pages"); //取得页面中pages参数,此参数代表要显示的“当前页面”
if (pages != null)
currentPage = Integer.valueOf(pages).intValue(); //获取待显示的当前页面的页码
sql = "select * from userinfo order by id asc";
allPage = basicDB.getTotalPage(sql, pageSize); //得到总页码数
rs = basicDB.getPagedRs(sql, pageSize, currentPage); //得到当前页面要显示的结果集
%>
<table border="0" cellspacing="1" cellpadding="3" width="590" bgcolor="#ffffff">
<%
while (rs.next()) {
id = rs.getInt("id");//得到数据库(结果集)中id编号
%>
<tr bgcolor="#FF6600" style="color: white">
<td>
Name:<%=rs.getString("name")%></td>
<td>
Password:<%=rs.getString("password")%></td>
</tr>
<%
}
%>
<tr>
<td height="1"></td>
</tr>
<tr>
<td colspan=4 align=right bgcolor="#FF6600" style="color: white;">
现在是第<%=currentPage%>页,
<%
if (currentPage > 1) {
%>
<!--如果不在第一页,则显示出“首页”链接-->
<A HREF="admin_show.jsp?pages=<%=(currentPage - 1)%>">首页</A>
<%
}
for (int i = 1; i <= allPage; i++) {
//显示出1、2、3、4……到最后一页的链接
out.println("<a href=admin_show.jsp?pages=" + i + ">" + i
+ "</a>");
}
%>
<%
if (currentPage < allPage) {
%>
<!--如果不在最后一页,则显示出“末页”链接-->
<A HREF="admin_show.jsp?pages=<%=(currentPage + 1)%>">末页</A>
<%
}
%>
</td>
</tr>
</table>
参考:http://blog.csdn.net/bingki/archive/2008/09/24/2975008.aspx