我的SQL查询
PreparedStatement ps = con.prepareStatement("select * from Application where name like '"+ name+"%'");
你能建议我如何使它不区分大小写,并根据搜索,它应该显示多个记录,以搜索关键字开头,因为我使用了‘喜欢’。
假设我在“搜索”按钮中按“C”搜索。当提交时,它应该在jsp页面中显示Car、cat等(在我的数据库中),但它只返回一条记录。
发布于 2016-12-11 09:56:47
使用以下命令:
PreparedStatement ps = con.prepareStatement("select * from Application where LOWER(name) like '"+ name..toLowerCase() +"%'");
发布于 2016-12-11 10:02:30
您尝试使用已准备好的语句是正确的。但是,您随后决定不实际使用该语句,而是使用易于注入的WHERE
子句。一些类似的东西应该可以解决你的问题:
String sql = "select * from Application where LOWER(name) like ?");
PreparedStatement preparedStatement = dbConnection.prepareStatement(sql);
preparedStatement.setString(1, "%" + name.toLowerCase() + "%");
ResultSet rs = preparedStatement.executeQuery(sql);
然而,从性能的角度来看,降低单个查询中的每条记录并不理想。如果您计划经常支持按名称进行不区分大小写的搜索,则可能需要考虑将名称的小写版本存储在Application
表中。这应该会加快查询速度。
发布于 2016-12-11 10:48:30
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DBConnection db=new DBConnection();
HttpSession hs = request.getSession();
String name=request.getParameter("appname");
List<Details> dbean=db.getAllDetails(name);
hs.setAttribute("detbean",dbean);
if(dbean.isEmpty())
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("<caption>Sorry, No Record Found</caption>");
}
else
response.sendRedirect("Application.jsp");
}
我的jsp代码
<TABLE BORDER="1" >
<TR>
<TH> <h4 >Application Name</h4> </TH>
<TH> <h4 >Abbrebation</h4> </TH>
<TH> <h4 >Primary Contact Name</h4> </TH>
<TH> <h4 >Primary Contact Number</h4> </TH>
<TH> <h4 >Seconday Contact Name</h4> </TH>
<TH> <h4 >Secondary Contact Number</h4> </TH>
</TR>
集合dbean = (ArrayList)request.getSession().getAttribute("detbean");迭代器i=dbean.iterator();while(i.hasNext()) { beanclasses.Details item= (beanclasses.Details)i.next();
<%=item.getAPPLICATIONNAME()%> <%=item.getAPPABBR()%> <%=item.getPRIMARYPERSON()%> <%=item.getPRICONTACT()%> <%=item.getSECONDAEYPERSON() %> <%=item.getSECCONTACT() %>
https://stackoverflow.com/questions/41084886
复制