`
qxmcool
  • 浏览: 90616 次
  • 性别: Icon_minigender_1
  • 来自: 石家庄
社区版块
存档分类
最新评论

jsp(脚本)+javabean+servlet+dao分页

    博客分类:
  • jsp
阅读更多
po


package com.qxm.po;

import java.io.Serializable;

public class Student implements Serializable {

private static final long serialVersionUID = 1L;

private int id;

private String name;

private String gender;

private String email;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

}


----------------------------------------------------------

jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.qxm.control.PageControl"%>
<%@page import="com.qxm.po.Student"%>
<html>
  <head>
    <title>人员信息</title>
  </head>
  <body>
  人员信息
   <hr>
   <table>
   <tr>
   <td>id</td>
   <td>name</td>
   <td>gender</td>
   <td>email</td>
   </tr>

   <%
   PageControl pc = (PageControl)request.getAttribute("pc");
   ArrayList<Student> tempList = pc.getSmallList();
   for(int i = 0;i<tempList.size();i++){
   %>
   <tr>
   <td><%=tempList.get(i).getId() %></td>
   <td><%=tempList.get(i).getName() %></td>
   <td><%=tempList.get(i).getGender() %></td>
   <td><%=tempList.get(i).getEmail() %></td>
   </tr>
   <%
   }
   %>
    </table>
  
<a href="Display?pageindex=1">首页</a>
   <%
if(pc.isFirstPage() == false){
%>
<a href="Display?pageindex=<%=pc.getPreviousPageCount() %>">上一页</a>
<%
}
   %>
   <%
if(pc.isLastPage() == false){
%>
<a href="Display?pageindex=<%=pc.getNextPagecount() %>">下一页</a>
<%
}
   %>
   <a href="Display?pageindex=<%=pc.getPageSize() %>">尾页</a>
 
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.qxm.control.PageControl"%>
<%@page import="com.qxm.po.Student"%>
<html>
  <head>
    <title>人员信息</title>
  </head>
  <body>
  人员信息
   <hr>
   <table>
   <tr>
   <td>id</td>
   <td>name</td>
   <td>gender</td>
   <td>email</td>
   </tr>

   <%
   PageControl pc = (PageControl)request.getAttribute("pc");
   ArrayList<Student> tempList = pc.getSmallList();
   for(int i = 0;i<tempList.size();i++){
   %>
   <tr>
   <td><%=tempList.get(i).getId() %></td>
   <td><%=tempList.get(i).getName() %></td>
   <td><%=tempList.get(i).getGender() %></td>
   <td><%=tempList.get(i).getEmail() %></td>
   </tr>
   <%
   }
   %>
    </table>
  
<a href="Display?pageindex=1">首页</a>
   <%
if(pc.isFirstPage() == false){
%>
<a href="Display?pageindex=<%=pc.getPreviousPageCount() %>">上一页</a>
<%
}
   %>
   <%
if(pc.isLastPage() == false){
%>
<a href="Display?pageindex=<%=pc.getNextPagecount() %>">下一页</a>
<%
}
   %>
   <a href="Display?pageindex=<%=pc.getPageSize() %>">尾页</a>
 
</html>


----------------------------------------------------------
servlet

package com.qxm.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qxm.control.PageControl;
import com.qxm.dao.ConnectionDB;
import com.qxm.po.Student;

public class Display extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String currentpage = request.getParameter("pageindex");
if(currentpage == null){
currentpage = "1";
}
int pageindex = Integer.parseInt(currentpage);
PageControl pc = (PageControl)request.getAttribute("pc");
if(pc == null){
pc = new PageControl();
ConnectionDB condb = new ConnectionDB();
ArrayList<Student> allList = condb.getResultSet();
pc.setBigList(allList);
request.setAttribute("pc", pc);
}
pc.setCurrentPage(pageindex);
this.getServletContext().getRequestDispatcher("/display1.jsp").forward(request, response);
}
}
----------------------------------------------------------
dao层

package com.qxm.dao;

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

import com.qxm.po.Student;
@SuppressWarnings("unchecked")
public class ConnectionDB {

public static Connection conn = null;
public static PreparedStatement ps = null;
public static ResultSet rs = null;

public void getconnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/info",
"root", "root");
} catch (ClassNotFoundException e) {
System.out.println("数据库驱动找不到");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("数据库连接异常");
e.printStackTrace();
}
}

/**
* id name gender email
* @return
*/

public ArrayList<Student> getResultSet(){
this.getconnection();
ArrayList<Student> list = null;
list = new ArrayList<Student>();

String sql = "select id,name,gender,email from student";
try {
rs = conn.prepareStatement(sql).executeQuery();
while(rs.next()){
Student stu = new Student();
stu.setId(rs.getInt(1));
stu.setName(rs.getString(2));
stu.setGender(rs.getString(3));
stu.setEmail(rs.getString(4));
list.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.closeConnection();
}
return list;
}

public void closeConnection(){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
System.out.println("rs关闭异常");
e.printStackTrace();
}
rs = null;
}
if(ps != null){
try {
ps.close();
} catch (SQLException e) {
System.out.println("ps关闭异常");
e.printStackTrace();
}
ps = null;
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
System.out.println("conn关闭异常");
e.printStackTrace();
}
}
conn = null;
}
}

----------------------------------------------------------
分页逻辑

package com.qxm.control;

import java.util.ArrayList;

import com.qxm.po.Student;

public class PageControl {

private ArrayList<Student> bigList; // 总集合
private int currentPage = 1; // 当前页数
private int pageCount = 2; // 每页数据的条数
private int pageSize; // 总页数
private int valueCount; // 总数据的条数
private ArrayList<Student> smallList;// 分页集合
private int previousPageCount;// 上一页的页数
private int nextPagecount; // 下一页的页数
private boolean isFirstPage; // 是否是第一页
private boolean isLastPage; // 是否是最后一页

public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
// 上一页
previousPageCount = currentPage - 1;
// 下一页
nextPagecount = currentPage + 1;
// 判断是否为第一页
if (currentPage == 1) {
isFirstPage = true;
} else {
isFirstPage = false;
}
// 判断是否为最后一页
if (currentPage == pageSize) {
isLastPage = true;
} else {
isLastPage = false;
}
smallList = new ArrayList<Student>();
for (int i = (currentPage - 1) * pageCount; i < currentPage * pageCount
&& i < valueCount; i++) {
smallList.add(bigList.get(i));
}
}

public void setBigList(ArrayList<Student> bigList) {
this.bigList = bigList;
valueCount = bigList.size();
pageSize = valueCount % pageCount == 0 ? valueCount / pageCount
: valueCount / pageCount + 1;
}

public int getCurrentPage() {
return currentPage;
}

public ArrayList<Student> getBigList() {
return bigList;
}

public int getPageCount() {
return pageCount;
}

public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

public int getValueCount() {
return valueCount;
}

public void setValueCount(int valueCount) {
this.valueCount = valueCount;
}

public ArrayList<Student> getSmallList() {
return smallList;
}

public void setSmallList(ArrayList<Student> smallList) {
this.smallList = smallList;
}

public int getPreviousPageCount() {
return previousPageCount;
}

public void setPreviousPageCount(int previousPageCount) {
this.previousPageCount = previousPageCount;
}

public int getNextPagecount() {
return nextPagecount;
}

public void setNextPagecount(int nextPagecount) {
this.nextPagecount = nextPagecount;
}

public boolean isFirstPage() {
return isFirstPage;
}

public void setFirstPage(boolean isFirstPage) {
this.isFirstPage = isFirstPage;
}

public boolean isLastPage() {
return isLastPage;
}

public void setLastPage(boolean isLastPage) {
this.isLastPage = isLastPage;
}
}

分享到:
评论
1 楼 zhima 2011-09-04  
好多阿   没人留言

相关推荐

Global site tag (gtag.js) - Google Analytics