2016-01-08 56 views
1

我所提供的開始日期和結束日期日期選擇器是如下STARTDATE日期和結束日期時,有一年的變化

 <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> 
      <script src="//code.jquery.com/jquery-1.10.2.js"></script> 
      <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
      <link rel="stylesheet" href="/resources/demos/style.css"> 
      <script type="text/javascript"> 
      $(function() { 
       $("#startDate").datepicker({ 
         changeMonth: true, 
         changeYear: true, 
         dateFormat: 'dd-mm-yy' 
        }); 

       $("#endDate").datepicker({ 
         changeMonth: true, 
         changeYear: true, 
         dateFormat: 'dd-mm-yy' 
        }); 
      }); 
      </script> 
     <style type="text/css"> 
       .search_textbx 
     { 
     background-image:url('/SalesPropeller/calendar.jpg'); 
      background-repeat:no-repeat; 
      background-position:right; 

     } 
       </style> 
    <form name="form" action="quotationSearchResult.jsp" method="post"> 
    <input type="text" id="startDate" name="startDate" class="search_textbx" readonly="readonly" placeholder="&nbsp &nbsp dd-mm-yyyy"/> 
     <input type="text" id="endDate" name="endDate" class="search_textbx" readonly="readonly" placeholder="&nbsp &nbsp dd-mm-yyyy"/></td> 
</form> 

quotationSearchResult.jsp

<div id="divRight"> 
       &nbsp; 
       <% 
        int count = 0; 
        int SlNo=1; 
       %> 
       <hr /> 
       <h3>Search Result for <u><%= request.getParameter("companyName")%></u> is : </h3> 
       <hr/> 
       <div id="divtable_wrapper"> 
        <div id="divtbody" style="height: 470px;"> 
         <form method="post" name="form"> 
          <table class="divtable" width="1140"> 
           <div id="divheader"> 
            <thead> 
             <tr> 
              <th style="padding:1px 5px;border-right:1px solid #d47f66;border-bottom:1px solid #d47f66;background: beige;">Sl.NO</th> 
              <th style="padding:1px 5px;border-right:1px solid #d47f66;border-bottom:1px solid #d47f66;background: beige;">CompanyName</th> 
              <th style="padding:1px 5px;border-right:1px solid #d47f66;border-bottom:1px solid #d47f66;background: beige;">RefNo</th> 
              <th style="padding:1px 5px;border-right:1px solid #d47f66;border-bottom:1px solid #d47f66;background: beige;">Quot.Date</th> 
              <th style="padding:1px 5px;border-right:1px solid #d47f66;border-bottom:1px solid #d47f66;background: beige;">UserName</th> 
              <th style="padding:1px 5px;border-right:1px solid #d47f66;border-bottom:1px solid #d47f66;background: beige;">Grand Total</th> 

              <th style="padding:1px 5px;border-right:1px solid #d47f66;border-bottom:1px solid #d47f66;background: beige;">CST</th> 
              <th style="padding:1px 5px;border-right:1px solid #d47f66;border-bottom:1px solid #d47f66;background: beige;">VAT5.5</th> 
              <th style="padding:1px 5px;border-right:1px solid #d47f66;border-bottom:1px solid #d47f66;background: beige;">VAT14.5</th> 
              <th style="padding:1px 5px;border-right:1px solid #d47f66;border-bottom:1px solid #d47f66;background: beige;">ServiceTax</th> 


              <th colspan="1" style="padding:1px 5px;border-right:1px solid #d47f66;border-bottom:1px solid #d47f66;background: beige;">Action</th> 
             </tr> 
            </thead> 
           </div> 

           <% 
            //Connection con = null; 
            Object o2 = session.getAttribute("email"); 
            String email = o2.toString(); 
            String companyName = request.getParameter("companyName"); 
            String startDate=request.getParameter("startDate"); 

            String endDate=request.getParameter("endDate"); 
            int cId=0; 
            //Statement st, st1; 
            try { 
             DBConnect db = new DBConnect(); 
             Connection con = db.getCon(); 

             Statement st = con.createStatement(); 
             // System.out.println("At SearchResult.jsp:" + email + "," + category + "," + search + "," + search_tf); 
             ResultSet rs = st.executeQuery("Select CId,CompanyName,RefNo,Date,UserName,GrandTotal,CST,VAT5,VAT14,ServiceTax from marketing_database.quotationclient_details where companyName='"+companyName+"' AND Date BETWEEN '"+startDate+"' AND '"+endDate+"';"); 
             // String q2 = "update dummy set category='" + category + "' where id = '1';"; 
             // st1.executeUpdate(q2);     // To insert the category name to dummy table in database 
%> 

           <% 
            while (rs.next()) { 
           %> 
           <tr><td style="padding:1px 5px;border-right:1px solid #d47f66;border-bottom:1px solid #d47f66;" class="td1"><%=SlNo%></td> 
            <td style="padding:1px 5px;border-right:1px solid #d47f66;border-bottom:1px solid #d47f66;" class="td2"><%=rs.getString(2)%></td> 
            <td style="padding:1px 5px;border-right:1px solid #d47f66;border-bottom:1px solid #d47f66;" class="td3"><%=rs.getString(3)%></td> 
            <td style="padding:1px 5px;border-right:1px solid #d47f66;border-bottom:1px solid #d47f66;" class="td4"><%=rs.getString(4)%></td> 
            <td style="padding:1px 5px;border-right:1px solid #d47f66;border-bottom:1px solid #d47f66;" class="td5"><%=rs.getString(5)%></td> 
            <td style="padding:1px 5px;border-right:1px solid #d47f66;border-bottom:1px solid #d47f66;" class="td6"><%=rs.getString(6)%></td> 
            <td style="padding:1px 5px;border-right:1px solid #d47f66;border-bottom:1px solid #d47f66;" class="td7"><%=rs.getString(7)%></td> 
            <td style="padding:1px 5px;border-right:1px solid #d47f66;border-bottom:1px solid #d47f66;" class="td8"><%=rs.getString(8)%></td> 
            <td style="padding:1px 5px;border-right:1px solid #d47f66;border-bottom:1px solid #d47f66;" class="td9"><%=rs.getString(9)%></td> 
            <td style="padding:1px 5px;border-right:1px solid #d47f66;border-bottom:1px solid #d47f66;" class="td10"><%=rs.getString(10)%></td> 


            <td style="padding:1px 5px;border-bottom:1px solid #d47f66;" class="td12"><input type="button" name="edit" value="Report" style="background-color:#49743D;font-weight:bold;color:#ffffff;" onclick="generateReport(<%= rs.getString(1)%>);" ></td> 

             <% count++; 
             SlNo++;%> 
           </tr> 
           <% 
            } 
           %> 
           <% 
            } catch (Exception e) { 
             e.printStackTrace(); 
            } 
           %> 
          </table> 
         </form> 
         <br> 
         <span style="float: left;"> &nbsp;<b><%= count%></b> Search Result Found.</span> 
       <br><br> 
       </div> 
       </div> 
       </div> 

當我給一個的startDate不工作「01-01-2016」和結束日期爲「31-01-2016」它是提取記錄如圖所示 enter image description here enter image description here 但是,當我給startDate爲「07-12-2015」和endDate爲「18-01-2016」它不取的紀錄顯示的圖像 enter image description here enter image description here

所以,這是怎麼回事,爲什麼它沒有比較幾年properly.I是SQL數據庫以及 enter image description here

+0

調試它,確定問題所在。字符串值是否正確?String startDate = request.getParameter(「startDate」);'? –

+0

也許你沒有這些日期間記錄2015年7月12日和18-01-2016 – Abdelhak

+0

看看開發人員工具後的數據,當你發送一個請求,並確保東西被髮送到了什麼。 –

回答

2

問題是,列「日期」是一個varchar,在數據庫中正確的形式是Date,而且「日期」是不是一列一個好名字,因爲它也是一個MySQL的功能。

由於保存的日期格式String不是一個可排序的形式,例如在你的日期排序爲字符串的:

  1. 2016年7月1日

  2. 2015年7月12日

  3. 18-01-2016

2016年7月1日是和18-01-2016之間的2015年7月12日

最好的解決辦法是轉換列的格式來糾正數據庫格式的日期。

速戰速決,如果這是不可能的

我們需要您的字符串列轉換爲日期和設置您的搜索字符串的正確格式MySQL的句法

//convert search string to mysql format (note you can define variables for faster code and you should consider handling exceptions) 
startDate = new SimpleDateFormat("yyyy-mm-dd").format(new SimpleDateFormat("dd/mm/yyyy").parse(startDate)); 
endDate = new SimpleDateFormat("yyyy-mm-dd").format(new SimpleDateFormat("dd/mm/yyyy").parse(endDate)); 

//syntax for query 
... AND STR_TO_DATE(`Date`, '%d/%m/%Y') BETWEEN '"+startDate+"' AND '"+endDate+"';" 

有關更多信息,請參閱

how to convert a string to date in mysql?

How do I query between two dates using MySQL?

您還應該考慮使用Prepared statement以避免sql注入問題。

Avoiding SQL Injection

+0

爲什麼給出「strDate」和「startDate」這兩個名稱?他們應該是相同的還是不同的? –

+0

同樣會編輯答案... –

+0

其實我並沒有應用「startDate」和「endDate」的代碼,所以有些日期不匹配。後來當我讀你仔細回答,然後我明白,這些行可以改變場景,它確實。再次感謝 –

相關問題