在学习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 查询列表总行数sql
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);//inserprove 查询表格数据sql
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DbUtil.close(con, ps, rs);
}
}