开发者社区> 欧阳鹏> 正文

JSP学习之------>JSP的查询和分页例子

简介: //这是index.jsp   编号: 姓名: 性别:男 女 日期: 到 搜索到结果:${map.
+关注继续查看

//这是index.jsp
 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
 <form action="${pageContext.request.contextPath}/stu.do?p=search" method="post">
 
编号:<input type="text" name="sid">  <br>
 姓名:<input type=text name="sname">  <br>
 性别:<input type=radio name="sex" value=1>男
      <input type=radio name="sex" value=0>女 <br>
 
日期:<input type=text name="start"> 到
      <input type=text name="end">   <br>
      
     <input type="hidden" name="page"/>
      <input type=submit value='搜索'>
      <input type=reset  value='重置'>    
</form>
 
<c:if test="${not empty map}">
 搜索到结果:${map.sum}条,当前${map.page}/${map.count}页
 <hr>
 <table border="1" align="center" width="80%">
   <tr>
     <th>编号</th>
     <th>姓名</th>
     <th>性别</th>
     <th>出生</th>
   </tr>
   
  <c:forEach items="${map.list}" var="s">
    <tr align="center">
      <td>${s.sid}</td>
      <td>${s.sname}</td>
      <td>${s.sex==1?"男":"女"}</td>
      <td>
         <fmt:formatDate value="${s.sbirthday}" pattern="yyyy年MM月dd日"/>
      </td>
    </tr>
   </c:forEach>
 </table>
  <a href="${pageContext.request.contextPath}/stu.do?p=search&page=1">【首 页】</a>
  <a href="${pageContext.request.contextPath}/stu.do?p=search&page=${map.page+1 }">【下一页】</a>
  <a href="${pageContext.request.contextPath}/stu.do?p=search&page=${map.page-1 }">【上一页】</a>
  <a href="${pageContext.request.contextPath}/stu.do?p=search&page=${map.count }">【末 页】</a>
 
</c:if>

 
 
 
 
 
//这是servlet的代码
 

import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.Map;
 
import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
import com.dao.StuDAO;
 
public class StuServlet extends HttpServlet {
 
 int size = 3;
  StuDAO stuDAO = new StuDAO();
  
  @Override
  public void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
         request.setCharacterEncoding("utf-8");
   response.setContentType("text/html;charset=utf-8");
   PrintWriter out = response.getWriter();
   
   String p = request.getParameter("p");
   if (p.equals("search"))
    doSearch(request, response);
   
  }
 
 //分页查询
  public void doSearch(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
      //要显示页数
             String pageString = request.getParameter("page");
             int    page       = 1;
             if (pageString!=null && pageString.trim().length()>0)
                 page       = Integer.parseInt(pageString);
             
            
     //其它条件
            String sid  = request.getParameter("sid");
            String sname = request.getParameter("sname");
            String sex   = request.getParameter("sex");
            String start = request.getParameter("start");
            String end   = request.getParameter("end");
            
           //查
            Map map  = stuDAO.fenye(page, size, sid, sname, sex, start, end);
            request.setAttribute("map",  map);
             
           request.getRequestDispatcher("/index.jsp").forward(request, response);
             
 }
 
}


 

 
 
//这是dao层
 

import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
import com.db.DbUtil;
 import com.entity.Student;
 
public class StuDAO {
  DbUtil db = new DbUtil();
  
  /*
   * 
  *    select top 3 * from student where 1=1 and sname like ? and sid not in 
  *                      (select top 3 sid from student where 1=1 and sname like ? )
   * 
  *    select count(*) from student where 1=1 and ....
   */
  
  public Map fenye(Integer page,Integer size,String sid,String sname,String sex,String  start,String end){
   List  ifList = new ArrayList(); //条件
   List  list   = new ArrayList(); //结果 
  Map   map    = new HashMap();   //所有结果
      StringBuffer sb = new StringBuffer();
      
     if (sid!=null && sid.trim().length()>0)
      {
       sb.append(" and sid=?");
       ifList.add(Integer.parseInt(sid));
      }
      
     if (sname!=null && sname.length()>0){
       sb.append(" and sname like ?");
       ifList.add("%"+sname+"%");
      }
      
     if (sex!=null && sex.trim().length()>0){
         sb.append(" and sex=?");
         ifList.add(Integer.parseInt(sex));
      }
      
     if (start!=null && start.trim().length()>0){
       sb.append(" and sbirthday>=?");
       ifList.add(java.sql.Date.valueOf(start));
      }
      
     if (end!=null && end.trim().length()>0){
       sb.append(" and sbirthday<=?");
       ifList.add(java.sql.Date.valueOf(end));
      }
      //总条数
      String sqlCount = "select count(*) from student where 1=1 "+sb.toString();
        
     ResultSet rsCount = db.getRs(sqlCount, ifList.toArray());
      int sum = 0;
   try {
    rsCount.next();
    sum = rsCount.getInt(1);
   } catch (SQLException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
      int      count    = sum%size==0?sum/size : sum/size+1;
      if (page<1) page = 1;
      if (page>count) page=count;
      
     String sqlList  = "select top "+size+" * from student where 1=1 "+sb.toString()+" and sid not in (select top "+(page-1)*size+" sid from student where 1=1 "+sb.toString()+")";
    
     System.out.println("count="+sqlCount);
      System.out.println("list="+sqlList);
      //这句代码很重要
      ifList.addAll(ifList);
      ResultSet rs = db.getRs(sqlList, ifList.toArray());
      try {
    while (rs!=null && rs.next()){
     Student stu = new Student();
     stu.setSid(rs.getInt("sid"));
     stu.setSname(rs.getString("sname"));
     stu.setSex(rs.getInt("sex"));
     stu.setSbirthday(rs.getDate("sbirthday"));
     list.add(stu);
     
     
    }
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
   map.put("page", page);
   map.put("size", size);
   map.put("count", count);
   map.put("list", list);
   map.put("sum", sum);
      
  return map;
  }
  
  
  
  //条件查询,不分页
  public List condition(String sid,String sname,String sex,String  start,String end)
  {
   List  ifList = new ArrayList(); //条件
   List  list   = new ArrayList(); //结果 
     StringBuffer sb = new StringBuffer("select * from student  where 1=1 ");
      if (sid!=null && sid.trim().length()>0)
      {
       sb.append(" and sid=?");
       ifList.add(Integer.parseInt(sid));
      }
      
     if (sname!=null && sname.length()>0){
       sb.append(" and sname like ?");
       ifList.add("%"+sname+"%");
      }
      
     if (sex!=null && sex.trim().length()>0){
         sb.append(" and sex=?");
         ifList.add(Integer.parseInt(sex));
      }
      
     if (start!=null && start.trim().length()>0){
       sb.append(" and sbirthday>=?");
       ifList.add(java.sql.Date.valueOf(start));
      }
      
     if (end!=null && end.trim().length()>0){
       sb.append(" and sbirthday<=?");
       ifList.add(java.sql.Date.valueOf(end));
      }
      
    
     ResultSet rs = db.getRs(sb.toString(), ifList.toArray());
      try {
    while (rs!=null && rs.next()){
     Student stu = new Student();
     stu.setSid(rs.getInt("sid"));
     stu.setSname(rs.getString("sname"));
     stu.setSex(rs.getInt("sex"));
     stu.setSbirthday(rs.getDate("sbirthday"));
     list.add(stu);
     
     
    }
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
      
     return list;
      
 }
  /**
   * @param args
   * jUnit
   */
  public static void main(String[] args) {
   // TODO Auto-generated method stub
   StuDAO  stuDAO = new StuDAO();
   /*List<Student> list = stuDAO.condition(null, "小", "1", null, null);
   for (Student student : list) {
    System.out.println(student.getSid()+"\t"+student.getSname()+"\t"+student.getSex());
   }*/
   
   stuDAO.fenye(1, 3, null, "小", null, null, null);
 
 }
 
}


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
分布式文件系统FastDFS看这一篇就够了(文件上传下载、单机部署及集群部署)(一)
分布式文件系统FastDFS看这一篇就够了(文件上传下载、单机部署及集群部署)
6 0
方法的递归调用 | 学习笔记
快速学习方法的递归调用
7 0
方法的定义 | 学习笔记
快速学习方法的定义
10 0
JavaWeb - SSO单点登录原理之基于CAS(一)
JavaWeb - SSO单点登录原理之基于CAS(一)
6 0
标识符与关键字 | 学习笔记
快速学习方法的定义 | 学习笔记
5 0
JavaWeb - 访问 WEB-INF 资源几种方式
JavaWeb - 访问 WEB-INF 资源几种方式
5 0
Java 8 新特性:Lambda 表达式
Java 8 新特性:Lambda 表达式
5 0
Day22 - Flutter - 混合开发(上)
Day22 - Flutter - 混合开发
5 0
+关注
欧阳鹏
一个人,如果你不逼自己一把,你根本不知道自己有多优秀! Talk is cheap, show me the code.
473
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
OceanBase 入门到实战教程
立即下载
阿里云图数据库GDB,加速开启“图智”未来.ppt
立即下载
实时数仓Hologres技术实战一本通2.0版(下)
立即下载