JavaWeb不定条件クエリー

7136 ワード

 Web            ,                     ,                 ,      SQL     ,     SQL    ,     SQL              ,             ,        ,          , CSDN xulu_258     (  ,               JSP  
 
  




  MainPerson.jsp
  





[B] [E]
${rows }
Servlet :
, queryperson();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	
	String pname=request.getParameter("qpname");
	String pnumber=request.getParameter("qpumber");
	String xl=request.getParameter("qxl");
	String jszc=request.getParameter("qjszc");
	String bsal=request.getParameter("bsal");
	String esal=request.getParameter("esal");
	
	String val[]={pname,pnumber,xl,jszc,bsal,esal};
	PersonServices service=new PersonServices();
	try {
		List> rows=service.queryperson(val);
		request.setAttribute("rows", rows);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	}
StringBuilder new , :"select * from person( ) where 1=1", where 1=1 and, if where and.    if sql.append("and name=?"), 。 , , List , List , , List , List 。

public List> queryperson(String...val)throws Exception
	
	{
		Connection conn=null;
		PreparedStatement psmt=null;
		ResultSet rs=null;
		try{
			
			String pname=val[0];
			String pnumber=val[1];
			String xl=val[2];
			String jszc=val[3];
			String bsal=val[4];
			String esal=val[5];
			
			conn=DBUtils.getConnection();
			
			List pars=new ArrayList<>();
			StringBuilder sql=new StringBuilder()
			 .append("SELECT X.PID,X.PNAME,X.PNUMBER,X.PSEX,")
			 .append("       TO_CHAR(X.PDATE,'YYYY-MM-DD') PDATE,")
			 .append("       X.MZ,X.XL,X.PMAIL,X.PHONE")
			 .append("  FROM PERSON X")
			 .append(" WHERE 1=1")
			;
			if(pname!=null&&pname.equals("")){
				sql.append("AND pname like ?");
				pars.add("%"+pname+"%");
				
			}
			if(pnumber!=null&&pnumber.equals("")){
				sql.append("AND X.PNUMBER= ?");
				pars.add(pnumber);
				
			}
			if(xl!=null&&xl.equals("")){
				sql.append("AND X.XL= ?");
				pars.add(xl);
				
			}
			if(jszc!=null&&jszc.equals("")){
				sql.append("AND X.JSZC= ?");
				pars.add(jszc);
				
			}
			if(bsal!=null&&bsal.equals("")){
				sql.append("AND X.BSAL>= ?");
				pars.add(bsal);
				
			}
			if(esal!=null&&esal.equals("")){
				sql.append("AND X.ESAL<= ?");
				pars.add(esal);
				
			}
		     
			sql.append(" ORDER BY X.PNAME");
			System.out.println(sql);
			System.out.println(pars);
			psmt=conn.prepareStatement(sql.toString());
			int index=1;
			for (Object  parm : pars) {
				psmt.setObject(index++, parm);
			}
			rs=psmt.executeQuery();
			ResultSetMetaData rsmd=rs.getMetaData();
			int count=rsmd.getColumnCount();
			int init_size=((int)(count/0.75))+2;
			
			
			List> rows=new ArrayList<>();
			
			Map ins=null;
			while(rs.next()){
				ins=new HashMap<>(init_size);
				for(int i=1;i<=count;i++){
					ins.put(rsmd.getColumnLabel(i).toLowerCase(),rs.getString(i));
				}
				
				
				rows.add(ins);
			}
			return rows;
			
		}finally{
		
			
		}
		//return null;
	}

, ResultSetMetaData List> ,ResultSetMetaData ResultSet , , , Map , map , ,

, ,