八、各种查询的Servlet

查询全部信息

TimeManageServlet.java

package servlet;
import java.io.IOException;  
import java.util.List;  
import java.util.*;
import java.io.*;
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  

import bean.AddMessageBean;
import dao.AddMessageDao;


public class TimeManageServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;  
	  
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
        this.doPost(req, resp);  
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {
    	req.setCharacterEncoding("UTF-8"); 
    	AddMessageDao dao = new AddMessageDao();
    	List<AddMessageBean> TimeManage = dao.findAll();  
        //保存查询的班级信息    
        req.setAttribute("TimeManage", TimeManage);
        //转发请求  
        req.getRequestDispatcher("/TimeManage.jsp").forward(req, resp);  
    }
}

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

按学号查询

SearchID.java

package servlet;
import java.io.*;
import java.sql.*;
import java.util.List;

import javax.servlet.*;
import javax.servlet.http.*;
import dao.AddMessageDao;
import bean.AddMessageBean;

public class SearchID extends HttpServlet{
	private static final long serialVersionUID = 1L;  
	  
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
        this.doPost(req, resp);  
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {
    	req.setCharacterEncoding("UTF-8"); 
    	resp.setCharacterEncoding("UTF-8");
//    	PrintWriter out = resp.getWriter();
    	String num=req.getParameter("num");
    	AddMessageDao dao = new AddMessageDao();
    	//System.out.println(num);
    	String sql = "select * from stumessage where num ='";
        sql += num + "'";
//        out.println(sql);
    	List<AddMessageBean> SearchID = dao.SearchID(num);  
        //保存查询的班级信息    
        req.setAttribute("SearchID", SearchID);
        //转发请求  
        req.getRequestDispatcher("/SearchID.jsp").forward(req, resp);  
    }
}

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

按学院查询

SearchIns.java

package servlet;

import java.io.*;
import java.sql.*;
import java.util.List;

import javax.servlet.*;
import javax.servlet.http.*;
import dao.AddMessageDao;
import bean.AddMessageBean;
public class SearchIns extends HttpServlet {
	private static final long serialVersionUID = 1L;  
	  
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
        this.doPost(req, resp);  
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {
    	req.setCharacterEncoding("UTF-8"); 
    	resp.setCharacterEncoding("UTF-8");
//    	PrintWriter out = resp.getWriter();
    	String college=req.getParameter("college");
    	AddMessageDao dao = new AddMessageDao();
 //   	System.out.println(num);
    	String sql = "select * from stumessage where college ='";
        sql += college + "'";
//        out.println(sql);
    	List<AddMessageBean> SearchIns = dao.SearchIns(college);  
        //保存查询的班级信息    
        req.setAttribute("SearchIns", SearchIns);
        //转发请求  
        req.getRequestDispatcher("/SearchIns.jsp").forward(req, resp);  
    }
}

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

按贫困等级查询

SearchLevel.java

package servlet;
import java.io.*;
import java.sql.*;
import java.util.List;

import javax.servlet.*;
import javax.servlet.http.*;
import dao.AddMessageDao;
import bean.AddMessageBean;
public class SearchLevel extends HttpServlet{
	private static final long serialVersionUID = 1L;  
	  
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
        this.doPost(req, resp);  
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {
    	req.setCharacterEncoding("UTF-8"); 
    	resp.setCharacterEncoding("UTF-8");
//    	PrintWriter out = resp.getWriter();
    	String poor=req.getParameter("poor");
    	AddMessageDao dao = new AddMessageDao();
    	//System.out.println(num);
    	String sql = "select * from stumessage where poor ='";
        sql += poor + "'";
//        out.println(sql);
    	List<AddMessageBean> SearchLevel = dao.SearchLevel(poor);  
        //保存查询的班级信息    
        req.setAttribute("SearchLevel", SearchLevel);
        //转发请求  
        req.getRequestDispatcher("/SearchLevel.jsp").forward(req, resp);  
    }
}

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

按姓名查询

SearchName.java

package servlet;
import java.io.*;
import java.sql.*;
import java.util.List;

import javax.servlet.*;
import javax.servlet.http.*;
import dao.AddMessageDao;
import bean.AddMessageBean;
public class SearchName extends HttpServlet{
	private static final long serialVersionUID = 1L;  
	  
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
        this.doPost(req, resp);  
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {
    	//设置编码,防止乱码
    	req.setCharacterEncoding("UTF-8"); 
    	resp.setCharacterEncoding("UTF-8");
    	//将JSP中name="name"的值赋给Sring类型的name
    	String name=req.getParameter("name");
    	//新建一个AddMessageDao
    	AddMessageDao dao = new AddMessageDao();
    	String sql = "select * from stumessage where name ='";
        sql += name + "'";
        //创建一个列表,调用dao的SearchName
    	List<AddMessageBean> SearchName = dao.SearchName(name);  
        //保存查询的信息用setAttribute将值给SearchName    
        req.setAttribute("SearchName", SearchName);
        //转发请求  
        req.getRequestDispatcher("/SearchName.jsp").forward(req, resp);  
    }
}

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

按专业查询

SearchPro.java

package servlet;
import java.io.*;
import java.sql.*;
import java.util.List;

import javax.servlet.*;
import javax.servlet.http.*;
import dao.AddMessageDao;
import bean.AddMessageBean;
public class SearchPro extends HttpServlet{
	private static final long serialVersionUID = 1L;  
	  
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
        this.doPost(req, resp);  
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {
    	req.setCharacterEncoding("UTF-8"); 
    	resp.setCharacterEncoding("UTF-8");
//    	PrintWriter out = resp.getWriter();
    	String mes=req.getParameter("major");
    	AddMessageDao dao = new AddMessageDao();
    	//System.out.println(num);
    	String sql = "select * from stumessage where major ='";
        sql += mes + "'";
//        out.println(sql);
    	List<AddMessageBean> SearchPro = dao.SearchPro(mes);  
        //保存查询的班级信息    
        req.setAttribute("SearchPro", SearchPro);
        //转发请求  
        req.getRequestDispatcher("/SearchPro.jsp").forward(req, resp);  
    }
}

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

按性别查询

SearchSex.java

package servlet;
import java.io.*;
import java.sql.*;
import java.util.List;

import javax.servlet.*;
import javax.servlet.http.*;
import dao.AddMessageDao;
import bean.AddMessageBean;
public class SearchSex extends HttpServlet{
	private static final long serialVersionUID = 1L;  
	  
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
        this.doPost(req, resp);  
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {
    	req.setCharacterEncoding("UTF-8"); 
    	resp.setCharacterEncoding("UTF-8");
//    	PrintWriter out = resp.getWriter();
    	String mes=req.getParameter("sex");
    	AddMessageDao dao = new AddMessageDao();
    	//System.out.println(num);
    	String sql = "select * from stumessage where sex ='";
        sql += mes + "'";
//        out.println(sql);
    	List<AddMessageBean> SearchSex = dao.SearchSex(mes);  
        //保存查询的班级信息    
        req.setAttribute("SearchSex", SearchSex);
        //转发请求  
        req.getRequestDispatcher("/SearchSex.jsp").forward(req, resp);  
    }
}

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

按入学年份查询

SearchYear.java

package servlet;
import java.io.*;
import java.sql.*;
import java.util.List;

import javax.servlet.*;
import javax.servlet.http.*;
import dao.AddMessageDao;
import bean.AddMessageBean;
public class SearchYear extends HttpServlet{
	private static final long serialVersionUID = 1L;  
	  
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
        this.doPost(req, resp);  
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {
    	req.setCharacterEncoding("UTF-8"); 
    	resp.setCharacterEncoding("UTF-8");
//    	PrintWriter out = resp.getWriter();
    	String mes=req.getParameter("year");
    	AddMessageDao dao = new AddMessageDao();
    	//System.out.println(num);
    	String sql = "select * from stumessage where year ='";
        sql += mes + "'";
//        out.println(sql);
    	List<AddMessageBean> SearchYear = dao.SearchYear(mes);  
        //保存查询的班级信息    
        req.setAttribute("SearchYear", SearchYear);
        //转发请求  
        req.getRequestDispatcher("/SearchYear.jsp").forward(req, resp);  
    }
}




阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页