1,控制层的代码:
package DBControl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
//一个处理类 处理user表
public class UserBeanControl {
private ResultSet rs = null;
private PreparedStatement prestmt = null;
private Connection conn = null;
private int pageCount = 0;//共有几页(计算得到rowCount+pagesize-1/pageSize)
public int getPageCount() {
return pageCount;
}
public ArrayList getResultByPage(int pageNow, int pageSize) { //参数是当前页,每页的大小
ArrayList<UserBean> userList = new ArrayList<UserBean>();
int rowCount = 0;//通过查表得到记录的总条数
ConnDB conntion = new ConnDB();
conn = conntion.getConn();
try {
//执行sql语句
prestmt = conn.prepareStatement("select count(*) from users");
//得到结果集
rs = prestmt.executeQuery();
//得到rowCount,总共有的行数
if(rs.next()) {
rowCount = rs.getInt(1);
}
//计算pageCount
if(rowCount%pageSize==0) {
pageCount =rowCount/pageSize;
} else {
pageCount =rowCount/pageSize + 1;
}
// 比较简单的计算方法pageCount = (rowCount+pageSize-1)/pageSize;
prestmt = conn.prepareStatement("select * from users limit ?,"+pageSize+";");
//SELECT * FROM table LIMIT 5,10; // 检索记录第6行,再往后推10行
//给?赋值
prestmt.setInt(1, pageSize*(pageNow-1));
//得到分页的结果集
rs=prestmt.executeQuery();
while(rs.next()) {
//将rs结果集中的每条记录进行封装成为userBean
UserBean ub = new UserBean();
ub.setUserID(rs.getInt(1));
ub.setUsername(rs.getString(2));
ub.setPassword(rs.getString(3));
ub.setEmail(rs.getString(4));
ub.setGrade(rs.getInt(5));
//将userbean放入集合ArrayList中
userList.add(ub);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
rs.close();
prestmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return userList;
}
}
前台:welcom.jsp
<%@page import="DBControl.UserBean"%>
<%@page import="DBControl.UserBeanControl"%>
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
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>
<base href="<%=basePath%>">
<title>My JSP 'Welcome.jsp' starting page</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>
<center>
<h1>用户信息列表</h1>
<%
//===========分页查询功能========
int pageSize = 3;//每页显示3条记录
int pageNow = 1;//当前页面显示为第一页
//动态的接收pageNow
String pagenow = request.getParameter("pageNow");
if(pagenow != null) {
pageNow = Integer.parseInt(pagenow);
} else {
//表示用户第一次进入页面 不做处理
}
//调用userbeanContorl
UserBeanControl ubc = new UserBeanControl();
ArrayList<UserBean> userList = ubc.getResultByPage(pageNow, pageSize);
//显示表头
out.println("<table border=1>");
out.println("<tr><th>userID</th><th>username</th><th>password</th><th>E-mail</th><th>grade</th></tr>");
//显示结果集
for(int i=0; i<userList.size(); i++){
UserBean user = userList.get(i);
out.println("<tr>");
out.println("<td>"+user.getUserID()+"</td>");
out.println("<td>"+user.getUsername()+"</td>");
out.println("<td>"+user.getPassword()+"</td>");
out.println("<td>"+user.getEmail()+"</td>");
out.println("<td>"+user.getGrade()+"</td>");
out.println("</tr>");
}
out.println("</table>");
//添加超链接
//上一页
if(pageNow != 1) {
int upPage = pageNow-1;
out.println("<a href=Welcome.jsp?pageNow="+ upPage +">上一页</a>");
}
for(int i=pageNow;i<=pageNow+5; i++) {
out.println("<a href=Welcome.jsp?pageNow="+i+">"+ i +"</a>");
}
//下一页
int pageCount = ubc.getPageCount();
if(pageNow != pageCount) {
int downPage = pageNow+1;
out.println("<a href=Welcome.jsp?pageNow="+ downPage +">下一页</a>");
}
%>
</center>
</body>
</html>
全部代码例子:http://download.csdn.net/detail/asdfzjs/6270127