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);//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);
 }	
}
  • 10
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值