2017-01-02 167 views
0

你好我正在編寫一個代碼,人們可以使用jsp從數據庫中搜索活動。當我執行程序時,我可以根據類型,描述,位置,城市成功搜索。我可以在頁面上看到數據。但是當我選擇日期等選項時,它會返回空表。我的代碼中的問題在哪裏?什麼將是正確的代碼? (PS所有屬性,除了編號爲varchar)enter image description here在jsp上從數據庫中搜索

enter image description here

如果我填類型 '音樂' 我ETTING此表 enter image description here 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'>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> 

的success.jsp

<%-- 
Document : success 
Created on : 19.Ara.2016, 12:41:49 
Author  : BURAK NURÇİÇEK 
--%> 

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
<body background="http://www.teamarking.com/barcode/bar_background.jpg"> 

<marquee><h2 style="color:RED">WELCOME TO ONLINE RESERVATION SYSTEM </marquee></h2> 

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>JSP Page</title> 

</head> 
<form method = "get" action = "search.jsp"> 
<table align="right"> 
    <tr><td><b><font color='#D18603'>Type :</b></td><td><input type="text" name="type" /></td></tr> 
    <tr><td><b><font color='#D18603'>Description :</b></td><td><input type="text" name="description" /></td></tr> 
    <tr><td><b><font color='#D18603'>City :</b></td><td><input type="text" name="city" /></td></tr> 
    <tr><td><b><font color='#D18603'>Location :</b></td><td><input type="text" name="location" /></td></tr> 
    <tr><td><b><font color='#D18603'>Date(YYYY-MM-DD) :</b></td><td><select name='time'><option value='1'>Today</option><option value='2'>Tomorrow</option><option value='3'>This Week</option><option value='4'>This Weekend</option><option value='5'>Next Week</option><option value='6'>Next Weekend</option></option></select></b>; 
    <tr><td colspan="2" align="center"><input type="submit" value="search" /> <input type="reset" value="reset" /></td></tr></td></tr> 
    <% 
     String x = (String) application.getAttribute("id"); 
    //out.println(x); 
    %> 

</table> 

</div> 
</body> 
</html> 
+0

看來日期存儲在時間戳格式,而retrivinng,您提供您的搜索查詢,因爲只有YYYY-MM-DD格式。將確切的時間戳記作爲yyyy-mm-dd:ss格式,並檢查結果是否返回。 –

+0

不,它是字符串 – tripley

+1

我認爲這是由於日期格式不匹配。 –

回答

0

這是你的選擇

<select name='time'> 
    <option value='1'>Today</option> 
    <option value='2'>Tomorrow</option> 
    <option value='3'>This Week</option> 
    <option value='4'>This Weekend</option> 
    <option value='5'>Next Week</option> 
    <option value='6'>Next Weekend</option> 
    </option> 
</select> 

在JSP中,您收到value。不是日期而是數值(在字符串中)。

你應該根據這個數值來評估你想要的日期。使用開關和Calendar將值添加到當前日期。但你也有這裏的日期範圍週末本週

另外,我在您的查詢中看到datetime,但在表格中只有time。你應該檢查這些。

+0

PS:我沒有提到更新值的可能性,因爲阿森在同一時間寫了它。這也是如此。 – AxelH

0

只需更改選項標籤的值:從1到今天和2到明天。 ...

+1

它不起作用 – tripley

0

如果使用選項這樣它會工作:

<select name='time'> 
    <option value='2017-01-02'>Today</option> 
    <option value='2017-01-03'>Tommorow</option> 
    </option> 
</select>