⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 searchstudent.java

📁 利用java编写的一个学生宿舍管理系统
💻 JAVA
字号:
/**
 * SearchStudent.java
 * author:liuhaijing
 * @version 1.0
 */
 
 package mydesign;//包名

import java.io.*;
import mydesign.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;

public class SearchStudent extends HttpServlet
{
	String strSQL="";     //用于操作学生信息表的查询语句
	String strSQL1="";     //用于操作入住信息表的查询语句
    
    //Initialize global variables
    public void init() throws ServletException 
    {
    }
    
    //接收POST请求,执行数据库操作,分页显示结果保存到session中,把视图派发到下一个页面
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
    	HttpSession session=request.getSession(true);
        request.setCharacterEncoding("gb2312");
        response.setContentType("text/html; charset=gb2312");
        PrintWriter out=response.getWriter();
        StudentControl studentcontrol=new StudentControl();
        HouseControl housecontrol=new HouseControl();
        PageBean pageCt1=new PageBean();
        int page=1;
      
        if(session.getAttribute("strSQL")!=null)    //判断客户机的请求是接受查询表单数据,还是执行翻页、修改或删除
        {
        	strSQL=(String)session.getAttribute("strSQL");
        	if(session.getAttribute("jumppage")==null)    //判断客户机的请求是翻页还是执行修改或删除后的显示
        	{
        		String x=request.getParameter("jumppage");    //得到要显示的页码  
        		page=Integer.parseInt(x);
   	     		String updatestudent=request.getParameter("updatestudent");
        		String delstudent=request.getParameter("delstudent");
        		String updatehouse=request.getParameter("updatehouse");
        		String delhouse=request.getParameter("delhouse");
      	  		if(updatestudent!=null||updatehouse!=null||delstudent!=null||delhouse!=null)    //判断客户机的请示是翻页还是修改或者删除,修改删除执行
        		{        	
        			int select=Integer.parseInt(request.getParameter("select"));
        			pageCt1=(PageBean)session.getAttribute("pageCt1");    //得到该页显示的数据
        			ArrayList searchstudent=pageCt1.getResult();
        			Student student=(Student)searchstudent.get(select);    //得到要操作的数据
        			session.setAttribute("jumppage",x);    //保存要显示的页码
        			try
        			{
        				if(updatestudent!=null)    //保存要修改的数据,并派发到修改页面
        				{
        					session.setAttribute("student",student);
        					out.print("<script>window.location.href='updatestudent.jsp'</script>");
        				}
        				if(updatehouse!=null)    //保存要修改的数据,并派发到修改页面
        				{
        					if(student.getDonghao()!=null)
        					{
        						session.setAttribute("student",student);
        						out.print("<script>window.location.href='updatestudenthouse.jsp'</script>");
        					}else out.print("<script>if(confirm('该学生尚未入住,是否现在入住?')==true) window.location.href='student_in.jsp';else window.location.href='/MyDesign/SearchStudent'</script>");
        				}
        				if(delstudent!=null)
        				{        //执行删除,重新查询
        					studentcontrol.DelStudent(student);
							//如果删除的数据是最后一页惟一一行数据,则将把要显示的页码减1
        					if(page==pageCt1.maxPage&&pageCt1.maxRowCount%pageCt1.rowsPerPage==1)    
        					{
        						session.setAttribute("jumppage",String.valueOf(page-1));
        					}
							studentcontrol.close();
							    //重新查询,并显示原页或前一页
        					out.print("<script>alert('删除成功!');window.location.href='/MyDesign/SearchStudent'</script>");
        				}
        				if(delhouse!=null)
        				{        //执行删除,重新查询
        					studentcontrol.DelStudentHouse(student);
        					housecontrol.delStudent(student.getDonghao(),student.getHouseid());
							studentcontrol.close();
							    //重新查询,并显示原页或前一页
        					out.print("<script>alert('删除成功!');window.location.href='/MyDesign/SearchStudent'</script>");
        				}
        			}catch(Exception e)
        			{
        			    System.out.print("SearchStudentServlet error:"+e.getMessage());           
        			}
        		}
        	}else{
        			page=Integer.parseInt((String)session.getAttribute("jumppage"));    //得到要显示的页码
         			session.setAttribute("jumppage",null);    //要显示的页码清空
        	}
        }else{    //得到表单数据
        	String a1=request.getParameter("studentid");
        	String a2=request.getParameter("name");
        	String a3=request.getParameter("sex");
        	String a4=request.getParameter("home");
        	String a5=request.getParameter("years");
        	String a6=request.getParameter("nianzhi");
        	String a7=request.getParameter("xueyuan");
        	String a8=request.getParameter("classid");
        	String a9=request.getParameter("donghao");
        	String a10=request.getParameter("houseid");
        	strSQL=getStrSQL(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10);
        }
        try
        {
        	session.setAttribute("strSQL",strSQL);
        	strSQL1="select count(*) from view_student"+strSQL;    //计算查询总数的查询语句
        	strSQL="select top "+page*pageCt1.rowsPerPage+" * from view_student"+strSQL;    //第一页到要显示页码的查询语句
         	if(page==0)  out.print("<script>alert('已经无符合条件的学生!');window.location.href='searchstudent.jsp'</script>");    //符合条件的数据为0时将页面派发到查询页面
        	pageCt1=studentcontrol.listData(page,this); 
        	studentcontrol.close();
         	if(pageCt1.getResult().size()==0)  out.print("<script>alert('无符合条件的学生!');window.location.href='searchstudent.jsp'</script>");    //符合条件的数据为0时将页面派发到查询页面
			session.setAttribute("pageCt1",pageCt1);    //保存要显示的数据
        	out.print("<script>window.location.href='searchstudentresult.jsp'</script>");    //将视图派发到下一个页面
        }
        catch(Exception e)
        {
            System.out.print("SearchStudent Servlet error:"+e.getMessage());            
        }
        finally
        {
        }
    }
    
    //和POST请求的处理一样
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
    	doPost(request,response);
    }
    
	//处理数据
    private String getStrSQL(String a1,String a2,String a3,String a4,String a5,String a6,String a7,String a8,String a9,String a10)
    {
    	strSQL="";
		if(!a1.equals(""))
		{
			strSQL=strSQL+" where studentid='"+a1+"'";
			if(!a2.equals("")) strSQL=strSQL+" and name='"+a2+"'";
			if(!a3.equals("请选择")) strSQL=strSQL+" and sex='"+a3+"'";
			if(!a4.equals("")) strSQL=strSQL+" and home='"+a4+"'";
			if(!a5.equals("")) strSQL=strSQL+" and years='"+a5+"'";
			if(!a6.equals("")) strSQL=strSQL+" and nianzhi='"+a6+"'";
			if(!a7.equals("请选择")) strSQL=strSQL+" and xueyuan='"+a7+"'";
			if(!a8.equals("")) strSQL=strSQL+" and classid='"+a8+"'";
			if(!a9.equals("")) strSQL=strSQL+" and donghao='"+a9+"'";
			if(!a10.equals("")) strSQL=strSQL+" and houseid='"+a10+"'";
		}else{
			if(!a2.equals(""))
			{
				strSQL=strSQL+" where name='"+a2+"'";
				if(!a3.equals("请选择")) strSQL=strSQL+" and sex='"+a3+"'";
				if(!a4.equals("")) strSQL=strSQL+" and home='"+a4+"'";
				if(!a5.equals("")) strSQL=strSQL+" and years='"+a5+"'";
				if(!a6.equals("")) strSQL=strSQL+" and nianzhi='"+a6+"'";
				if(!a7.equals("请选择")) strSQL=strSQL+" and xueyuan='"+a7+"'";
				if(!a8.equals("")) strSQL=strSQL+" and classid='"+a8+"'";
				if(!a9.equals("")) strSQL=strSQL+" and donghao='"+a9+"'";
				if(!a10.equals("")) strSQL=strSQL+" and houseid='"+a10+"'";
			}else{
				if(!a3.equals("请选择"))
				{
					strSQL=strSQL+" where sex='"+a3+"'";   
					if(!a4.equals("")) strSQL=strSQL+" and home='"+a4+"'";
					if(!a5.equals("")) strSQL=strSQL+" and years='"+a5+"'";
					if(!a6.equals("")) strSQL=strSQL+" and nianzhi='"+a6+"'";
					if(!a7.equals("请选择")) strSQL=strSQL+" and xueyuan='"+a7+"'";
					if(!a8.equals("")) strSQL=strSQL+" and classid='"+a8+"'";
					if(!a9.equals("")) strSQL=strSQL+" and donghao='"+a9+"'";
					if(!a10.equals("")) strSQL=strSQL+" and houseid='"+a10+"'";
				}else{
					if(!a4.equals(""))
					{
						strSQL=strSQL+" where home='"+a4+"'";  
						if(!a5.equals("")) strSQL=strSQL+" and years='"+a5+"'";
						if(!a6.equals("")) strSQL=strSQL+" and nianzhi='"+a6+"'";
						if(!a7.equals("请选择")) strSQL=strSQL+" and xueyuan='"+a7+"'";
						if(!a8.equals("")) strSQL=strSQL+" and classid='"+a8+"'";
						if(!a9.equals("")) strSQL=strSQL+" and donghao='"+a9+"'";
						if(!a10.equals("")) strSQL=strSQL+" and houseid='"+a10+"'";
					}else{
						if(!a5.equals(""))
						{
							strSQL=strSQL+" where years='"+a5+"'";	
							if(!a6.equals("")) strSQL=strSQL+" and nianzhi='"+a6+"'";
							if(!a7.equals("请选择")) strSQL=strSQL+" and xueyuan='"+a7+"'";
							if(!a8.equals("")) strSQL=strSQL+" and classid='"+a8+"'";
							if(!a9.equals("")) strSQL=strSQL+" and donghao='"+a9+"'";
							if(!a10.equals("")) strSQL=strSQL+" and houseid='"+a10+"'";
						}else{
							if(!a6.equals(""))
							{
								strSQL=strSQL+" where nianzhi='"+a6+"'";
								if(!a7.equals("请选择")) strSQL=strSQL+" and xueyuan='"+a7+"'";
								if(!a8.equals("")) strSQL=strSQL+" and classid='"+a8+"'";
								if(!a9.equals("")) strSQL=strSQL+" and donghao='"+a9+"'";
								if(!a10.equals("")) strSQL=strSQL+" and houseid='"+a10+"'";
							}else{
								if(!a7.equals("请选择"))
								{
									strSQL=strSQL+" where xueyuan='"+a7+"'";	
									if(!a8.equals("")) strSQL=strSQL+" and classid='"+a8+"'";
									if(!a9.equals("")) strSQL=strSQL+" and donghao='"+a9+"'";
									if(!a10.equals("")) strSQL=strSQL+" and houseid='"+a10+"'";
								}else{
									if(!a8.equals(""))
									{
										strSQL=strSQL+" where classid='"+a8+"'";									
										if(!a9.equals("")) strSQL=strSQL+" and donghao='"+a9+"'";
										if(!a10.equals("")) strSQL=strSQL+" and houseid='"+a10+"'";
									}else{
										if(!a9.equals(""))
										{
											strSQL=strSQL+" where donghao='"+a9+"'";
											if(!a10.equals("")) strSQL=strSQL+" and houseid='"+a10+"'";	
										}else{
											if(!a10.equals("")) strSQL=strSQL+" where houseid='"+a10+"'";
										}
									}
								}
							}
						}
					}
				}
			}
		}	
		strSQL=strSQL+";";
		return strSQL;
    }
    
    //Clean up resources
    public void destroy()
    {
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -