1
你好我正在編寫一個代碼,人們可以使用jsp從數據庫中搜索活動。當我執行程序時,我可以根據類型,描述,位置,城市和日期成功搜索。我可以在頁面上看到數據。但是,當我填寫兩個或三個標準或等,程序不能很好地工作。例如,如果我填寫類型爲'音樂'和城市爲'伊斯坦布爾',程序會查找所有音樂活動和伊斯坦布爾的所有活動。我認爲我的sql查詢是錯誤的。如果我將查詢更改爲OR,則必須填寫所有字段。否則,它將返回空表。但用戶可以填寫兩個標準或三個標準或等等,這取決於用戶。如果用戶填寫類型爲'音樂'和城市爲'伊斯坦布爾',那麼只能在伊斯坦布爾展示音樂活動。我該如何解決?什麼將是正確的代碼? 從數據庫中檢索相關標準
search.jsp的
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*" %>
<!DOCTYPE html>
<html>
<body background="http://www.teamarking.com/barcode/bar_background.jpg">
<form method="post" action="reservations.jsp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Book Ticket</title>
</head>
<center>
<table border="1" width="30%" height="30%">
<th><font color='#D18603'>id</font>
<th><font color='#D18603'>Type</font></th>
<th><font color='#D18603'>Description</font></th>
<th><font color='#D18603'>City</font></th>
<th><font color='#D18603'>Location</font></th>
<th><font color='#D18603'>Date</font></th>
<th><font color='#D18603'>Price</font></th>
<th><font color='#D18603'>Time</font></th>
<th><font color='#D18603'>Buy</font>
<%
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/users", "users", "123");
Statement st = con.createStatement();
ResultSet rs;
PreparedStatement ps = con.prepareStatement("select * from activities where type=? OR description=? OR city=? OR location=? OR date=? OR time=?");
ps.setString(1, request.getParameter("type"));
ps.setString(2, request.getParameter("description"));
ps.setString(3, request.getParameter("city"));
ps.setString(4, request.getParameter("location"));
ps.setString(5, request.getParameter("date"));
ps.setString(6, request.getParameter("time"));
rs = ps.executeQuery();
while (rs.next()) {
out.println("<tr>");
out.println("<form action='reservations.jsp'>");
out.println("<td>" + rs.getString("id") + "<input type='hidden' name='id' value='" + rs.getString("id") + "'></td>");
out.println("<td>" + rs.getString("type") + "<input type='hidden' name='type' value='" + rs.getString("type") + "'></td>");
out.println("<td>" + rs.getString("description") + "<input type='hidden' name='description' value='" + rs.getString("description") + "'></td>");
out.println("<td>" + rs.getString("city") + "<input type='hidden' name='city' value='" + rs.getString("city") + "'></td>");
out.println("<td>" + rs.getString("location") + "<input type='hidden' name='location' value='" + rs.getString("location") + "'></td>");
out.println("<td>" + rs.getString("date") + "<input type='hidden' name='date' value='" + rs.getString("date") + "'></td>");
out.println("<td>" + rs.getString("price") + "<input type='hidden' name='price' value='" + rs.getString("price") + "'></td>");
out.println("<td>" + rs.getString("time") + "<input type='hidden' name='time' value='" + rs.getString("time") + "'></td>");
out.println("<td><b><form action='reservations.jsp'><select name='buy'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option></select><input type='submit' value='Submit'></form></b>");
out.println("</tr>");
}
st.close();
%>
</center>
</table>
<br> <br><a href='success.jsp'>Back</a>
<br><br><a href='logout.jsp'>Log out</a>
</form>
</body>
</html>
當我執行像你說的,它返回emoty表@GurwinderSingh – tripley
理想它應該工作。儘管我無法確定,但最可能的原因是數據。爲什麼不在問題中發佈一些示例數據,以及您期望得到的輸出結果。 – GurV
我寫道。例如,如果用戶輸入類型爲'音樂'音樂數據來,如果用戶作爲伊斯坦布爾進入城市。伊斯坦布爾的活動應該到來。現在它正在工作,但當用戶都進入音樂和伊斯坦布爾,其他城市即將到來的其他活動 – tripley