按照计划本篇将为大家总结搜索功能的两种实现:确定搜索与模糊搜索。所谓精确搜索便是指,根据用户的输入的搜索内容,在数据库中寻找具有一一对应的关系的数据,一般都是用户在数据库中的主键值。而模糊搜索,是一种根据用户输入的信息,进行匹配,将具有相似信息的数据对象返回给用户。这两者在代码上的区别仅仅在于sql语句不同。下面就具体为大家介绍一下。
搜索输入框的设计,在上一篇中已经为大家介绍过,这里就不再赘述。下面直接开始我们本篇的内容。
1、精确搜索:
用于搜索的java方法:
public Student getName(String name) { Student student = new Student(); Connection conn = null; Statement st = null; ResultSet rs = null; conn = DBO.getConnection(); String sql = "select * from students where username='"+name+"'"; try { st = conn.createStatement(); rs = st.executeQuery(sql); while(rs.next()){ student.setId(rs.getInt("id")); student.setName(name); student.setSex(rs.getInt("sex")); student.setYear(rs.getInt("age")); student.setFrom(rs.getString("form")); student.setSchool(rs.getString("school")); } } catch (SQLException e) { e.printStackTrace(); } return student; }
2、模糊搜索:
用于进行模糊搜索的java方法:
public List<Student> getByName(String name) { List<Student> list = new ArrayList<Student>(); Connection conn = null; PreparedStatement pst = null; ResultSet rs = null; conn = DBO.getConnection(); String sql = "select * from students where username like ?"; try { pst = conn.prepareStatement(sql); pst.setString(1, "%"+name+"%"); rs = pst.executeQuery(); while(rs.next()){ Student student = new Student(); student.setId(rs.getInt("id")); student.setName(rs.getString("username")); student.setSex(rs.getInt("sex")); student.setYear(rs.getInt("age")); student.setFrom(rs.getString("form")); student.setSchool(rs.getString("school")); list.add(student); } } catch (SQLException e) { e.printStackTrace(); } return list; }
3、select代码:
这里的我将精确搜索和模糊搜索的select放在了一起。
public class get extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); StudentMaImp smi = new StudentMaImp(); Student student = new Student(); List<Student> list = new ArrayList<Student>(); if(request.getParameter("queding")!= null&&request.getParameter("queding").length()!=0){ String queding = request.getParameter("queding"); student = smi.getName(queding); if(student.getName()!=null&&student.getName().length()!=0){ request.setAttribute("student", student); request.getRequestDispatcher("Get.jsp").forward(request, response); }else{ list = smi.getAll(); request.setAttribute("list", list); request.setAttribute("new1", queding+"同学不存在"); request.getRequestDispatcher("All.jsp").forward(request, response); } }else{ String mohu = request.getParameter("mohu"); list = smi.getByName(mohu); if(list.size()!=0){ request.setAttribute("list", list); request.getRequestDispatcher("GetAll.jsp").forward(request, response); }else{ list = smi.getAll(); request.setAttribute("list", list); request.setAttribute("new2", "关键字:"+mohu+"不存在"); request.getRequestDispatcher("All.jsp").forward(request, response); } } } }
4、记住用户名和密码:
我们在很多需要进行登录的网站经常可以看到,当我们第一次登录成功后,以后我们再次打开这个页面时,我们的用户名和密码已经填写好了,这个就是一个记住密码功能,下面我就开始为大家介绍一下它的使用。这里需要使用一个关键字Cookie,通过这个关键字我们可以通过select后台将我们的账户信息保存的本地,当我们需要使用时通过调用Cookie的一个方法便可以了。
保存数据到本地方法:
Cookie cookie_name = new Cookie("name",name);//设置保存数据的名字 cookie_name.setMaxAge(60*60*24*7);//保存数据的期限这里设置的为一周 response.addCookie(cookie_name); Cookie cookie_pwd = new Cookie("pwd",password); cookie_pwd.setMaxAge(60*60*24*7); response.addCookie(cookie_pwd);
获取Cookie中的值:
<tr> <td>姓名:</td><td><input type="text" name="name" value="${cookie.name.value }" /></td> </tr> <tr> <td>密码:</td><td><input type="password" name="pwd" value="${cookie.pwd.value }" /></td> </tr>
到这里本篇的总结就写完了,内容不多,大家有什么疑问可以留言讨论。下一篇:类似百度分页的效果实现