数据分页显示

来源:互联网 发布:智慧办公软件下载 编辑:程序博客网 时间:2024/06/06 20:30

 代码:

PageEmployee.java

package com.employee;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import com.bean.User;import com.connect.DButil;import com.mysql.jdbc.Connection;import com.mysql.jdbc.PreparedStatement;public class PageEmployee {public List<User> getEmloyeeList(int start,int length){Connection conn=null;PreparedStatement psmt=null;ResultSet rs=null;String sql="SELECT * FROM infomation LIMIT ?,?";List<User> result=new ArrayList<User>();try {conn=DButil.open();psmt=(PreparedStatement) conn.prepareStatement(sql);psmt.setInt(1, start);psmt.setInt(2, length);rs=psmt.executeQuery();//执行查询while(rs.next()){User U=new User();U.setId(rs.getInt("id"));U.setTitle(rs.getString("title"));U.setTime(rs.getString("time"));U.setPlace(rs.getString("Place"));U.setCompany(rs.getString("Company"));U.setPname(rs.getString("Pname"));U.setFabuName(rs.getString("fabuName"));result.add(U);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {//关闭连接if(conn!=null){try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}return result;}public List<User> getEloyeeListGeneral(int start,int length){Connection conn=null;PreparedStatement psmt=null;ResultSet rs=null;String sql="SELECT * FROM infomation";List<User> result=new ArrayList<User>();try {conn=DButil.open();psmt=(PreparedStatement) conn.prepareStatement(sql);rs=psmt.executeQuery();//执行查询rs.absolute(start+1);for(int i=0;i<length;i++){User U=new User();U.setId(rs.getInt("id"));U.setTitle(rs.getString("title"));U.setTime(rs.getString("time"));U.setPlace(rs.getString("Place"));U.setCompany(rs.getString("Company"));U.setPname(rs.getString("Pname"));U.setFabuName(rs.getString("fabuName"));result.add(U);if(!rs.next()){break;}}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return result;}public int totalCount(){Connection conn=null;PreparedStatement psmt=null;ResultSet rs=null;int result=0;try {conn=DButil.open();String sql="SELECT COUNT(*) from infomation";psmt=(PreparedStatement) conn.prepareStatement(sql);rs=psmt.executeQuery();//执行查询if(rs.next()){result=rs.getInt(1);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return result;}}

User.java

package com.bean;public class User {int id;String title;String time;String Place;String Company;String Pname;String fabuName;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getTime() {return time;}public void setTime(String time) {this.time = time;}public String getPlace() {return Place;}public void setPlace(String place) {Place = place;}public String getCompany() {return Company;}public void setCompany(String company) {Company = company;}public String getPname() {return Pname;}public void setPname(String pname) {Pname = pname;}public String getFabuName() {return fabuName;}public void setFabuName(String fabuName) {this.fabuName = fabuName;}}
 

 index.jsp

<%@page import="com.employee.PageEmployee"%><%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ page import="com.bean.User,java.util.List" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>分页示例</title></head><body>  <%   String pageNumber=request.getParameter("pageNumber");      if(pageNumber==null||pageNumber.equals("")){   pageNumber="1";//默认显示第一页   }   int number=Integer.parseInt(pageNumber);   int pageRecords=3;//每页显示的数据条数   int start=0;//开始的记录序号      start=(number-1)*pageRecords;   PageEmployee dao=new PageEmployee();   List<User> employees=dao.getEmloyeeList(start, pageRecords);   int count=dao.totalCount();//总条数   int total=count/pageRecords;//总页数   //调整总页数   if(count%pageRecords!=0){   total++;   }    %>    <table width="90%" border="1">     <tr bgcolor="blue">        <td width="10%">编号</td>        <td width="10%">关键字</td>        <td width="20%">更新时间</td>        <td width="10%">区域</td>        <td width="10%">行业</td>        <td width="20%">项目名称</td>        <td width="20%">发布单位</td>     </tr>     <%       for(User U :employees){     %>     <tr>        <td width="10%"><%=U.getId() %></td>        <td width="10%"><%=U.getTitle() %></td>        <td width="20%"><%=U.getTime() %></td>        <td width="10%"><%=U.getPlace() %></td>        <td width="10%"><%=U.getCompany() %></td>        <td width="20%"><%=U.getPname() %></td>        <td width="20%"><%=U.getFabuName() %></td>     </tr>     <%       }     %>  </table>    <br/>页码    <%      for(int i=0;i<=total;i++){    %>    <a href="index.jsp?pageNumber=<%=i%>"><%=i%></a>      <%      }    %></body></html>

截图:




0 0
原创粉丝点击