生活随笔
收集整理的這篇文章主要介紹了
Java 模糊查询
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在學習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
);ps
.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
);ps
.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
) {e
.printStackTrace();}finally{DbUtil
.close(con
, ps
, rs
);}
}
總結
以上是生活随笔為你收集整理的Java 模糊查询的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。