我正在开发一个jsp 页面,用户可以通过使用允许他按类别、标题或发行日期搜索的搜索表单来浏览电影集合。如果没有任何搜索条件,则应显示所有电影。我的问题是,只有*按发布年份搜索可以正常工作,但按标题搜索不起作用,它什么也不显示,没有结果*当我把它放在年份搜索的底部时,按标题搜索不起作用,并且反之亦然
<%
String s1 = request.getParameter("Ttl");
String s2 = request.getParameter("Yr");
out.println("<TABLE >");
out.println("<TR>");
out.println("<TH>Category</TH>");
out.println("<TH>Title</TH>");
out.println("<TH>Release Year</TH>");
out.println("<TH>Rental Price</TH>");
out.println(" <TH></TH>");
out.println("<TH></TH>");
out.println(" </TR>");
String sql = //search if no search condition
"SELECT *" +
" FROM users" +
" WHERE ";
if (s1!=null && !(s1.equals(""))){ //search by title
sql = sql +
"title LIKE '%" +
s1 + "%'";
}
if (s2!=null){ //search by year
sql = sql +
" year LIKE '%" +
s2 + "%'";
}
try{
Connection con =
DriverManager.getConnection("jdbc:odbc:UsersData");
System.out.println("got connection");
Statement s = con.createStatement();
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
out.println("<TR>");
String id = rs.getString(5);
out.println("<TD>" + rs.getString(1) + "</TD>");
out.println("<TD>" + rs.getString(2) + "</TD>");
out.println("<TD>" + rs.getString(3) + "</TD>");
out.println("<TD>" + rs.getString(4) + "</TD>");
out.println("<TD><A HREF=Add to shoping cart?id=" + id + ">Add to shoping cart</A></TD>");
out.println("</TR>");
}
s.close();
con.close();
}
catch (SQLException e) {
}
catch (Exception e) {
}
%>