在學習Java 這門語言過程中,會遇到無數的知識點與錯誤,最重要的是我們能夠在茫茫的代碼中找到突破口,并用心去汲取精華。
在很多時候我們會用到模糊查詢,這里是我在編碼過程中用到的模糊查詢。
JSP :
<input value="${athenticationname}"id="athenticationname" type="text" class="text_add" style=" width:200px">
<button type="button" class="btn_search"><i class="fa fa-search"></i>查詢</button>
<script>
$("#chax").click(function(){var Athenticationname = $("#athenticationname").val();window.location.href="servlet/LoginServlet?fun=selectprove&athenticationname="+ Athenticationname + ""; });
</script>
除了在點擊查詢按鈕的時候把名稱傳去Servlet,同時在查詢表格數據的時候也需要把模糊查詢的名稱傳過去,這里不一一詳說。
Servlet :
public void selectprove(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String Athenticationname=request.getParameter("athenticationname");request.setAttribute("athenticationname", Athenticationname);request.getRequestDispatcher("/jsp/prove.jsp").forward(request, response);}
同時在查詢表格數據的時候也要有接受頁面傳來的值:
String Athenticationname =request.getParameter("athenticationname");
Dao :
這是分頁查詢列表里面的模糊查詢名稱:
public Bsgrid<Userautonym> selectprove(String Athenticationname, int startIndex,int pageSize) {
String str;
List<Userautonym> userautonyms = new ArrayList<Userautonym>();
Bsgrid<Userautonym> bsgrid=new Bsgrid<Userautonym>();
Userautonym userautonym = null;
try {con=DbUtil.getConnection();if (Athenticationname != null && Athenticationname !="") {//名稱不為空進行模糊查詢str =" where Athenticationname like ? " + " limit " + startIndex + "," + pageSize;ps=con.prepareStatement(getTotalRow + str);//getTotalRow 查詢列表總行數sqlps.setString(1,'%' + Athenticationname +'%');rs=ps.executeQuery();while (rs.next()) {bsgrid.setTotalRows(rs.getInt("count"));}str =" and Athenticationname like ? " + " limit " + startIndex + "," + pageSize;ps=con.prepareStatement(inserprove + str);//inserprove 查詢表格數據sqlps.setString(1,'%' + Athenticationname +'%');} else {//不模糊查詢的時候執行ps = con.prepareStatement(getTotalRow);rs = ps.executeQuery();while (rs.next()) {bsgrid.setTotalRows(rs.getInt("count"));} str=" limit " + startIndex +","+ pageSize;ps=con.prepareStatement(inserprove + str);}rs=ps.executeQuery();while (rs.next()) {userautonym = new Userautonym();userautonym.setAthenticationID(rs.getInt("AthenticationID"));userautonym.setExamineStateID(rs.getInt("ExamineStateID"));userautonym.setExamineStatename(rs.getString("ExamineStatename")); userautonym.setAthenticationname(rs.getString("Athenticationname")); userautonyms.add(userautonym);} bsgrid.setData(userautonyms);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{DbUtil.close(con, ps, rs);}
}