1. First, define two text boxes on the front-end interface, and both text boxes have their own name values. The style of the text boxes is designed by themselves. When you click the query, you will jump to the action in the form form.
<DIV class="am-input-group am-input-group-sm" style="width:400px;"> <form action="${pageContext.request.contextPath}/employee?action=select2" method="post"> <div> <p>Name:</p> <SPAN class="am-input-group-btn"> <INPUT name="name" type="text" > //Define the type of the text box <input type="submit" value="query">//query button </SPAN> </div> <div> <p>Employee ID:</p> <SPAN> <INPUT name="no" type="text"> </SPAN> </div> </form> </DIV>
2. Write the action=select2 method in the servlet file in java. When you click the query, jump to this servlet file
else if(action != null && action.equals("select2")) { //Receive the value of the form String no = ""; String name = ""; / / Determine whether the no and name text boxes are empty no=request.getParameter("no")==null?"":request.getParameter("no"); name=request.getParameter("name")==null?"":request.getParameter("name"); //Define employeeDao EmployeeDao employeeDao = new EmployeeDao(); try { //Define a collection and call the query method from employeeDao List employeeList = employeeDao.chaxun(no, name); /*System.out.println(employeeList);*/ //Assign the value of employeeList to employeeList request.setAttribute("employeeList",employeeList); } catch (Exception e) { e.printStackTrace (); } //Jump to the manager.jsp file in employee after completion request.getRequestDispatcher("/employee/manager.jsp").forward(request, response); }
3. The method of writing the query in the Dao file, and the sql statement
public List chaxun(String no,String name) throws Exception{//Pass no and name as parameters //Write a sql statement not a String statement StringBuffer sql = new StringBuffer("select ID,TASK_USER_ID,TASK_DEPT_ID,EM_NO,EM_XM,EM_XB,EM_SR,EM_ZW,EM_ZZ,EM_SFZH from task_employee"); Connection conn = null; PreparedStatement stmt = null; conn = getConn(); //Define a collection of employeeList List employeeList = new ArrayList(); try { / / Judging no is not empty, when name is empty if(!no.equals("") && name.equals("")) { sql.append(" where EM_NO like '%"+no+"%'"); } // Judging that name is not empty, when no is empty if(!name.equals("") && no.equals("")) { sql.append(" where EM_XM like '%"+name+"%'"); } //When it is judged that no and name are not empty if(!name.equals("") && !no.equals("")) { sql.append(" where EM_NO like'%"+no+"%'and EM_XM like '%"+name+"%'"); } //Convert sql statement to String type String sql1 = sql.toString(); conn = getConn(); stmt = conn.prepareStatement(sql1); ResultSet rs = stmt.executeQuery(); // Take the value from the result set and put it in the employee while (rs.next()) { Employee e = new Employee(); e.setId (rs.getInt (1)); e.setUserid (rs.getInt (2)); e.setDeptide (rs.getInt (3)); e.setNo(rs.getString(4)); e.setName(rs.getString(5)); e.setSex(rs.getString(6)); e.setBirthday(rs.getDate(7)); e.setDuty(rs.getString(8)); e.setAddress(rs.getString(9)); e.setNumber(rs.getString(10)); employeeList.add(e); } } catch (SQLException e) { e.printStackTrace (); } //Return the collection of employeeList return employeeList; }