查询页面:
<form action="search" method="post" name="myform"> <table> <tr> <td>学生学号:</td> <td><input type="text" class="sid"></td> </tr> <tr> <td>学生姓名:</td> <td><input type="text" class="name"></td> </tr> <tr> <td>学生性别:</td> <td><input type="text" class="sex"></td> </tr> <tr> <td>学生政治面貌:</td> <td><input type="text" name="politicsstatus"></td> </tr> <tr> <td align="center"><input type="submit" name="submit" value="查询"></td> <td><input type="reset" name="reset" value="重置"></td> </tr> </table> </form>
执行的servlet:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String sid=req.getParameter("sid"); String name=req.getParameter("name"); String sex=req.getParameter("sex"); String politicsstatus=req.getParameter("politicsstatus"); dbconnection dbc=new dbconnection(); List<Student> list=new ArrayList<Student>(); try { list=dbc.searchstudent(sid,name,sex,politicsstatus); req.setAttribute("lists",list); RequestDispatcher requestDispatcher = req.getRequestDispatcher("out.jsp"); requestDispatcher.forward(req,resp); } catch (Exception e) { e.printStackTrace(); } }
//dbconnetion部分:
//模糊查询学生信息 public List<Student> searchstudent(String sid,String name,String sex,String politicsstatus) throws Exception{ List<Student> list=new ArrayList<Student>(); String sql=" SELECT * FROM student WHERE (1=1 "; if(name != null){ sql+=" AND Sname LIKE '%"+name+"%' "; } if (sid !=null){ sql+=" AND Sid LIKE '%"+sid+"%' "; } if (sex !=null){ sql+=" AND SSex LIKE '%"+sex+"%' "; } if (politicsstatus != null){ sql+=" AND SPoliticsStatus LIKE '%"+politicsstatus+"%' "; } sql+=")"; ResultSet rs=this.executeQuery1(sql); try { while (rs.next()){ Student student=new Student(); student.setSid(rs.getString(1)); student.setName(rs.getString(2)); student.setClasses(((rs.getInt(3)))); student.setSex(rs.getString(4)); student.setEmail(rs.getString(5)); student.setPoliticsstatus(rs.getString(6)); student.setPassword(rs.getString(7)); student.setAge(rs.getInt(8)); student.setRole(rs.getString(rs.getString(9))); list.add(student); } } catch (SQLException e){ return null; } return list; }