一个简单的分页显示

2 篇文章 0 订阅
1 篇文章 0 订阅
public class User {
	private String username;
	private String userpass;
	private String sex;
	private String hiredate;
	private String interest;
	public User() {
		super();
	}
	public User(String username, String userpass, String sex, String hiredate,
			String interest) {
		super();
		this.username = username;
		this.userpass = userpass;
		this.sex = sex;
		this.hiredate = hiredate;
		this.interest = interest;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getUserpass() {
		return userpass;
	}
	public void setUserpass(String userpass) {
		this.userpass = userpass;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getHiredate() {
		return hiredate;
	}
	public void setHiredate(String hiredate) {
		this.hiredate = hiredate;
	}
	public String getInterest() {
		return interest;
	}
	public void setInterest(String interest) {
		this.interest = interest;
	}
	
	
}

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import bean.User;

public class StuDao {
	private static String dbClassName = "oracle.jdbc.driver.OracleDriver";// 定义保存数据库驱动的变量
	private static String dbUrl = "jdbc:oracle:thin:@192.168.1.208:1521:orcl";
	private static String dbUser = "abc";
	private static String dbPwd = "abc";
	private static Connection conn=null;
	//每页显示几行纪录
	private static int pagesize=10;
	public static int totalpage=0;
	User user=null;
	
	public StuDao(){
		
			getConnection();
		
		
	}
	
	public int getPagesize(){
		return pagesize;
	}
	
	//得到页码数(要先执行)
	public void setTotalpage(){
		int rowcounts=0;
		String sql = "select count(*) from zqq_user";
		Statement stmt;
		try {
			stmt = getConnection().createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			if(rs!=null){
				while(rs.next()){
					rowcounts = rs.getInt(1);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		totalpage = (rowcounts%pagesize==0)?(rowcounts/pagesize):(rowcounts/pagesize+1);
	}
	
	public int getTotalpage(){
		return totalpage;
	}
	
	public List<User> getUserByPage(int currentPage){
		List<User> users = new ArrayList<User>();
		User user=null;
		String sql = "select * from (select s.*,rownum rn from zqq_user s where rownum<=?) where rn>?";
		PreparedStatement pstmt;
		try {
			pstmt = getConnection().prepareStatement(sql);
			pstmt.setInt(1, currentPage*pagesize);
			pstmt.setInt(2, (currentPage-1)*pagesize);
			
			ResultSet rs = pstmt.executeQuery();
			
			if(rs!=null){
				while(rs.next()){
					user = new User(rs.getString(1),rs.getString(2),rs.getString(3),rs.getDate(4).toString(),rs.getString(5));
					users.add(user);
				}
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		

		return users;
	}
	
	//测试mian方法
	public static void main(String[] args){
		StuDao studao = new StuDao();
		List<User> users = null;
		
			studao.setTotalpage();
			
			users = studao.getUserByPage(2);
			for(User user:users){
				
				System.out.println(user.getUsername());
			}
			
	
	}
	private Connection getConnection(){
		
		
		try {
			Class.forName(dbClassName);
			conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return conn;
	}
}

<%@ page contentType="text/html" contentType="text/html" import="java.io.*,dao.StuDao,java.util.*,bean.User,java.sql.*" pageEncoding="utf-8" isELIgnored="false"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


<% 
	StuDao studao = new StuDao();
	//设置总的页码数
	studao.setTotalpage();
	//得到总的页面数
	int pageCount = studao.getTotalpage();
	//设置当前页码值
	String currentPage = request.getParameter("currentPage");
	if(currentPage==null){
		currentPage ="1";
	}
 %>
 <html>
 	<head>
 		<title>显示用户信息</title>
 		<style type="text/css">
 			.jishu{
 				background-color:white;
 			}
 			.oushu{
 				background-color:lightgray;
 			}
 		</style>
 		<script type="text/javascript">
 		
 		</script>
 	</head>
 	<body>
 		<table border="1" align="center" width="600">
 			
 			<tr>
 				<th>序号</th>
 				<th>姓名</th>
 				<th>密码</th>
 				<th>性别</th>
 				<th>出生日期</th>
 				<th>兴趣爱好</th>
 			</tr>
 			<%
 			//得到当前页面的用户列表
 			List<User> users = studao.getUserByPage(Integer.parseInt(currentPage));
 			request.setAttribute("users",users);
 			request.setAttribute("pageCount",pageCount);
 			//out.println(pageCount);
 			 %>
 			<%--判断用户列表是否为null --%>
 			<c:if test="${users!=null}">
 			<c:forEach items="${users}" var="user" varStatus="status">
				
 				<tr class="${status.count%2==0?'oushu':'jishu'}" onMouseOver="this.style.backgroundColor='pink'" onMouseOut="this.style.backgroundColor=''">
 					<td>${status.count }</td>
 					<td>${user.username }</td>
 					<td>${user.userpass }</td>
 					<td>${user.sex }</td>
 					<td>${user.hiredate }</td>
 					<td>${user.interest }</td>
 				</tr>
				
			</c:forEach>
			</c:if>

			<tr>
				<td colspan="6" align="center">
				<a href="showuserinfo.jsp?currentPage=1">首页</a>
				<a href="showuserinfo.jsp?currentPage=<%=(Integer.parseInt(currentPage)-1)==0?1:(Integer.parseInt(currentPage)-1) %>">上一页</a>
				<%--用页面来跳转 --%>
				<%
				for(int i=1;i<=pageCount;i++){
				 %>
					<a href="showuserinfo.jsp?currentPage=<%=i %>"><%=i %></a>
				<%
				}
				 %>
				<a href="showuserinfo.jsp?currentPage=<%=(Integer.parseInt(currentPage)+1)>pageCount?pageCount:(Integer.parseInt(currentPage)+1) %>">下一页</a>
				<a href="showuserinfo.jsp?currentPage=${pageCount} ">尾页</a>
				</td>
			</tr>
 		</table>
 	</body>
 </html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值