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

📄 searchrepair.java

📁 利用java编写的一个学生宿舍管理系统
💻 JAVA
字号:
/**
 * SearchRepair.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 SearchRepair 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();
        RepairControl repaircontrol=new RepairControl();
        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 update=request.getParameter("update");
        		String del=request.getParameter("del");
      	  		if(update!=null||del!=null)    //判断客户机的请示是翻页还是修改或者删除,修改删除执行
        		{        	
        			int select=Integer.parseInt(request.getParameter("select"));
        			pageCt1=(PageBean)session.getAttribute("pageCt1");    //得到该页显示的数据
        			ArrayList searchrepair=pageCt1.getResult();
        			Repair repair=(Repair)searchrepair.get(select);    //得到要操作的数据
        			session.setAttribute("jumppage",x);    //保存要显示的页码
        			try
        			{
        				if(update!=null)    //保存要修改的数据,并派发到修改页面
        				{
        					session.setAttribute("repair",repair);
        					out.print("<script>window.location.href='updaterepair.jsp'</script>");
        				}else{        //执行删除,重新查询
        					repaircontrol.DelRepair(repair);
							//如果删除的数据是最后一页惟一一行数据,则将把要显示的页码减1
        					if(page==pageCt1.maxPage&&pageCt1.maxRowCount%pageCt1.rowsPerPage==1)    
        					{
        						session.setAttribute("jumppage",String.valueOf(page-1));
        					}
							repaircontrol.close();
        					out.print("<script>alert('删除成功!');window.location.href='/MyDesign/SearchRepair'</script>");    //重新查询,并显示原页或前一页
        				}
        			}catch(Exception e)
        			{
        			    System.out.print("SearchRepairServlet error:"+e.getMessage());           
        			}
        		}
        	}else{
        			page=Integer.parseInt((String)session.getAttribute("jumppage"));    //得到要显示的页码
         			session.setAttribute("jumppage",null);    //要显示的页码清空
        	}
        }else{    //得到表单数据
        	String a1=request.getParameter("donghao");
        	String a2=request.getParameter("houseid");
        	String a3=request.getParameter("things");
        	String a4=request.getParameter("isrepair");
        	String a5=request.getParameter("calldate");
        	String a6=request.getParameter("repairdate");
        	String a7=request.getParameter("peichang");
        	strSQL=getStrSQL(a1,a2,a3,a4,a5,a6,a7);
        }
        try
        {
        	session.setAttribute("strSQL",strSQL);
        	strSQL1="select count(*) from repair"+strSQL;    //计算查询总数的查询语句
        	strSQL="select top "+page*pageCt1.rowsPerPage+" * from repair"+strSQL;    //第一页到要显示页码的查询语句
         	if(page==0)  out.print("<script>alert('已经无符合条件的记录!');window.location.href='searchrepair.jsp'</script>");    //符合条件的数据为0时将页面派发到查询页面
        	out.print(strSQL+"<br>");
        	pageCt1=repaircontrol.listData(page,this);    //得到该页要显示的数据
        	repaircontrol.close();
        	out.print("ok<br>");
         	if(pageCt1.getResult().size()==0)  out.print("<script>alert('无符合条件的记录!');window.location.href='searchrepair.jsp'</script>");    //符合条件的数据为0时将页面派发到查询页面
			session.setAttribute("pageCt1",pageCt1);    //保存要显示的数据
        	out.print("<script>window.location.href='searchrepairresult.jsp'</script>");    //将视图派发到下一个页面
        }
        catch(Exception e)
        {
            System.out.print("SearchRepair 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)
    {
    	strSQL="";
		if(!a1.equals(""))
		{
			strSQL=strSQL+" where donghao='"+a1+"'";
			if(!a2.equals("")) strSQL=strSQL+" and houseid='"+a2+"'";
			if(!a3.equals("")) strSQL=strSQL+" and things='"+a3+"'";
			if(!a4.equals("请选择")) strSQL=strSQL+" and isrepair='"+a4+"'";
			if(!a5.equals("")) strSQL=strSQL+" and calldate='"+a5+"'";
			if(!a6.equals("")) strSQL=strSQL+" and repairdate='"+a6+"'";
			if(!a7.equals("请选择")) strSQL=strSQL+" and peichang='"+a7+"'";
		}else{
			if(!a2.equals(""))
			{
				strSQL=strSQL+" where houseid='"+a2+"'";
				if(!a3.equals("")) strSQL=strSQL+" and things='"+a3+"'";
				if(!a4.equals("请选择")) strSQL=strSQL+" and isrepair='"+a4+"'";
				if(!a5.equals("")) strSQL=strSQL+" and calldate='"+a5+"'";
				if(!a6.equals("")) strSQL=strSQL+" and repairdate='"+a6+"'";
				if(!a7.equals("请选择")) strSQL=strSQL+" and peichang='"+a7+"'";
			}else{
				if(!a3.equals(""))
				{
					strSQL=strSQL+" where things='"+a3+"'";
					if(!a4.equals("请选择")) strSQL=strSQL+" and isrepair='"+a4+"'";
					if(!a5.equals("")) strSQL=strSQL+" and calldate='"+a5+"'";
					if(!a6.equals("")) strSQL=strSQL+" and repairdate='"+a6+"'";
					if(!a7.equals("请选择")) strSQL=strSQL+" and peichang='"+a7+"'";
				}else{
					if(!a4.equals("请选择"))
					{
						strSQL=strSQL+" where isrepair='"+a4+"'";
						if(!a5.equals("")) strSQL=strSQL+" and calldate='"+a5+"'";
						if(!a6.equals("")) strSQL=strSQL+" and repairdate='"+a6+"'";
						if(!a7.equals("请选择")) strSQL=strSQL+" and peichang='"+a7+"'";
					}else{
						if(!a5.equals(""))
						{
							strSQL=strSQL+" where calldate='"+a5+"'";
							if(!a6.equals("")) strSQL=strSQL+" and repairdate='"+a6+"'";
							if(!a7.equals("请选择")) strSQL=strSQL+" and peichang='"+a6+"'";
						}else{
							if(!a6.equals("")) 
							{
								strSQL=strSQL+" where repairdate='"+a6+"'";
								if(!a7.equals("请选择")) strSQL=strSQL+" and peichang='"+a7+"'";
							}else{
								if(!a7.equals("请选择")) strSQL=strSQL+" where peichang='"+a7+"'";
							}
						}
					}
				}   
			}
		}	
		strSQL=strSQL+";";
		return strSQL;
    }
    
    //Clean up resources
    public void destroy()
    {
    }
}

⌨️ 快捷键说明

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