当前位置:   article > 正文

数据库,软工,Java综合项目高级功能02-实现模糊查询,多条件查询_javaweb完成emp列表查询三层架构,实现姓名模糊查询,年龄区间查询,年级精准查询 ,

javaweb完成emp列表查询三层架构,实现姓名模糊查询,年龄区间查询,年级精准查询 ,

主要需求:

根据上述三个input框,选择输入部分内容,实现多条件查询。

前端:

提交input内容

  1. <form class="form-inline" style="float: left" action="Servlet8" method="post">
  2. <div class="form-group">
  3. <label for="exampleInputName2">姓名</label>
  4. <input type="text" class="form-control" id="exampleInputName2" name="name">
  5. </div>
  6. <div class="form-group">
  7. <label for="exampleInputEmail2">籍贯</label>
  8. <input type="text" class="form-control" id="exampleInputEmail2" name="location">
  9. </div>
  10. <div class="form-group">
  11. <label for="exampleInputEmail3">邮箱</label>
  12. <input type="email" class="form-control" id="exampleInputEmail3" name="email">
  13. </div>
  14. <button type="submit" class="btn btn-default">查询</button>
  15. </form>

后端:

接收input内容

实现内容管理,操作数据库

  1. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  2. request.setCharacterEncoding("utf-8");
  3. String name = request.getParameter("name");
  4. System.out.println(name);
  5. String location = request.getParameter("location");
  6. System.out.println(location);
  7. String email=request.getParameter("email");
  8. System.out.println(email);
  9. String currentPage = request.getParameter("currentPage");
  10. String rows = request.getParameter("rows");
  11. if(currentPage==null||"".equals(currentPage)){
  12. currentPage="1";
  13. }
  14. if(rows==null||"".equals(rows)){
  15. rows="5";
  16. }
  17. UserService u = new UserServiceImpl();
  18. Pagebean<User> pb = null;
  19. try {
  20. pb = u.fuzzyQuery(name,location,email,currentPage,rows);
  21. } catch (SQLException e) {
  22. e.printStackTrace();
  23. }
  24. request.setAttribute("pb", pb);
  25. request.getRequestDispatcher("/2.6.jsp").forward(request,response);
  26. }

操作数据库,比较关键,主要是实现动态sql

  1. public static int fuzzyQueryCount(String name,String location,String email) throws SQLException {
  2. String sql="select count(*) from user1 where 1=1 ";
  3. String a= String.valueOf(' ');
  4. if(!Objects.equals(name, a)){
  5. sql=sql+"and name like '%"+name+"%' ";
  6. }
  7. if(!Objects.equals(location, a)){
  8. sql=sql+"and location like '%"+location+"%' ";
  9. }
  10. if(!Objects.equals(email, a)){
  11. sql=sql+"and email like '%"+email+"%' ";
  12. }
  13. System.out.println(sql);
  14. Connection cnn = JDBCUtils.getConnection();
  15. Statement stat = cnn.createStatement();
  16. ResultSet rs = stat.executeQuery(sql);
  17. if (rs.next()){
  18. int totalCount=rs.getInt(1);
  19. JDBCUtils.close(rs,stat,cnn);
  20. return totalCount;
  21. }
  22. JDBCUtils.close(rs,stat,cnn);
  23. return 0;
  24. }
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/769089
推荐阅读
相关标签
  

闽ICP备14008679号