赞
踩
主要需求:
根据上述三个input框,选择输入部分内容,实现多条件查询。
前端:
提交input内容
- <form class="form-inline" style="float: left" action="Servlet8" method="post">
- <div class="form-group">
- <label for="exampleInputName2">姓名</label>
- <input type="text" class="form-control" id="exampleInputName2" name="name">
- </div>
- <div class="form-group">
- <label for="exampleInputEmail2">籍贯</label>
- <input type="text" class="form-control" id="exampleInputEmail2" name="location">
- </div>
- <div class="form-group">
- <label for="exampleInputEmail3">邮箱</label>
- <input type="email" class="form-control" id="exampleInputEmail3" name="email">
- </div>
- <button type="submit" class="btn btn-default">查询</button>
- </form>
后端:
接收input内容
实现内容管理,操作数据库
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- request.setCharacterEncoding("utf-8");
- String name = request.getParameter("name");
- System.out.println(name);
- String location = request.getParameter("location");
- System.out.println(location);
- String email=request.getParameter("email");
- System.out.println(email);
- String currentPage = request.getParameter("currentPage");
- String rows = request.getParameter("rows");
- if(currentPage==null||"".equals(currentPage)){
- currentPage="1";
- }
- if(rows==null||"".equals(rows)){
- rows="5";
- }
- UserService u = new UserServiceImpl();
- Pagebean<User> pb = null;
- try {
- pb = u.fuzzyQuery(name,location,email,currentPage,rows);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- request.setAttribute("pb", pb);
- request.getRequestDispatcher("/2.6.jsp").forward(request,response);
- }

操作数据库,比较关键,主要是实现动态sql
- public static int fuzzyQueryCount(String name,String location,String email) throws SQLException {
- String sql="select count(*) from user1 where 1=1 ";
- String a= String.valueOf(' ');
- if(!Objects.equals(name, a)){
- sql=sql+"and name like '%"+name+"%' ";
- }
- if(!Objects.equals(location, a)){
- sql=sql+"and location like '%"+location+"%' ";
- }
- if(!Objects.equals(email, a)){
- sql=sql+"and email like '%"+email+"%' ";
- }
- System.out.println(sql);
- Connection cnn = JDBCUtils.getConnection();
- Statement stat = cnn.createStatement();
- ResultSet rs = stat.executeQuery(sql);
- if (rs.next()){
- int totalCount=rs.getInt(1);
- JDBCUtils.close(rs,stat,cnn);
- return totalCount;
- }
- JDBCUtils.close(rs,stat,cnn);
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。