5月20日 CMS 周日

本文介绍了一个学生信息管理系统的实现,该系统使用Java Servlet技术处理HTTP请求,通过DAO模式操作数据库,支持模糊查询及分页显示学生信息。
package com.www.servlet;


import java.io.IOException;
import java.util.List;

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

import com.www.entity.Student;
import com.www.service.StudentService;
import com.www.util.PageUtils;

public class ServletStudent extends HttpServlet {
    private StudentService service = new StudentService();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        String m = request.getParameter("m");

        if("list".equals(m)){
            list(request,response);
        }

    }
    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String mohu = request.getParameter("mohu");
        String cpage = request.getParameter("cpage");

        if(cpage==null){
            cpage="1";
        }

        Integer pageSize = 2;
        Integer count = service.getStudentCount(mohu);
        PageUtils pu = new PageUtils(Integer.valueOf(cpage),pageSize,count);
        List<Student> stuList = service.getStudentList(pu,mohu);

        request.setAttribute("mohu", mohu);
        request.setAttribute("pu", pu);
        request.setAttribute("stuList", stuList);

        request.getRequestDispatcher("list.jsp").forward(request, response);
    }
}
package com.www.service;

import java.util.List;

import com.www.dao.StudentDao;
import com.www.entity.Student;
import com.www.util.PageUtils;

public class StudentService {
    private StudentDao dao = new StudentDao();
    public Integer getStudentCount(String mohu) {
        // TODO Auto-generated method stub
        return dao.getStudentCount(mohu);
    }

    public List<Student> getStudentList(PageUtils pu, String mohu) {
        // TODO Auto-generated method stub
        return dao.getStudentList(pu,mohu);
    }

}
package com.www.dao;

import java.util.List;

import com.www.entity.Student;
import com.www.util.JdbcUtils;
import com.www.util.PageUtils;

public class StudentDao {

    public Integer getStudentCount(String mohu) {
        // TODO Auto-generated method stub
        String sql = "select count(*) from t_student s left join t_dept c on s.cid = c.cid";
        if(mohu != null){
            sql += " where name like '%"+mohu+"%' ";
        }
        return JdbcUtils.getListCount(sql);
    }

    public List<Student> getStudentList(PageUtils pu, String mohu) {
        // TODO Auto-generated method stub
        String sql = "select s.*,c.dname from t_student s left join t_dept c on s.cid=c.cid ";
        if(mohu != null){
            sql += " where name like '%"+mohu+"%' ";
        }
        sql += " limit "+pu.getStartIndex()+","+pu.getPageSize();
        return JdbcUtils.getList(Student.class, sql);
    }

}

当你凝视深渊时,深渊也在凝视着你。
《善恶的彼岸》
——尼采

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值