如何在两个日期之间获取数据?

时间:2012-10-22 07:46:41

标签: mysql jsp

我在jsp文件中遇到日期选择器问题。

当我点击Allmeters.jsp文件中的超链接时,它将打开meteridinfo.jsp。在这个文件中,我有一个名为meterid的标签。我从allmeters.jsp得到这个meterid值,还有两个名为fromdate和todate的字段。通过使用这些日期我从mysql表获取数据,但问题是当我选择从2012-05-1的日期和todate为2012-05-11它不是retreiving数据,但当我改变todate为2012-05-31然后它显示来自mysql表的数据,这意味着当我选择一个月中的lastdate作为todate然后它才会到来。请帮助我。

AllMeters.jsp的编码如下:

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
        <%@page import="java.sql.*"%>
        <html>
        <head>
        <title>All Meter's</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <style>
        td{FONT: 13px Arial,sans-serif;}
        th{FONT:bold 13px Arial,sans-serif;color:white;background-color:#3366ff}
        input{FONT: 13px Arial,sans-serif;border: solid 1;}
        select{font: 13px Arial,sans-serif}
        #button{cursor:hand}
        A:hover {COLOR: red}</style>
        <script type="text/javascript">
        window.history.forward();
        function noback() { window.history.forward(); }
        </script>
        </head>
        <body>
        <table  border=1 style="" width="100%" align="center" cellpadding="5" cellspacing="0">
        <tr>
        <th>S No</th>
        <th>Meter ID</th>
        <th>Consumer ID</th>
        <th>Consumer Name</th>
        <th>Reading</th>
        <th> Date</th>
        <th>Time</th>
        <th>Status</th>
        <th>Sub Zone</th>
        <th>Zone</th></tr>
        <%
        try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/suwatermeter","root","sumith");
        Statement st=con.createStatement();
        int mid;
        int SNo=1;
        ResultSet rs=st.executeQuery("select * from Meter_List");
        while(rs.next()){
        %>   
<tr><td align="center" bgcolor="#ededed"><%=SNo%></td><td align="center"     bgcolor="#ededed"><a  href="meteridinfo.jsp?mid=<%=rs.getInt("meterid")%>"><%=rs.getInt("Meterid")%></a></td><td align="center" bgcolor="#ededed"><%=rs.getInt("Consumerid")%></td><td  bgcolor="#ededed"><%=rs.getString("Consumername")%></td><td  align="center" bgcolor="#ededed"><%=rs.getInt("LastReading")%></td><td align="center" bgcolor="#ededed"><%=rs.getDate("Date")%></td><td align="center" bgcolor="#ededed"><%=rs.getTime("Time")%></td><td align="center" bgcolor="#ededed"><%=rs.getString("Status")%></td><td align="center" bgcolor="#ededed"><%=rs.getString("Subzone")%></td><td align="center" bgcolor="#ededed"><%=rs.getString("zone")%></td></tr>
 <%
    SNo++;
    }
    rs.close();
    st.close();
    con.close();
    }
    catch(Exception e){}
    %>
    </tbody>
    <form action="" method="POST" name="frm_pages">
    <table style="width: 100%;" align="center" border="0">    
    </table>
    </body>
    </html>

    code of file meteridinfo.jsp

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <%@page import="java.sql.*"%>
    <%@page import="java.util.Date"%>
    <%@page import="java.text.SimpleDateFormat"%>
    <%
    Connection con;
    String sqlQuery="";
    Statement st;
    ResultSet rs=null;
    %>
    <html>
    <head>
    <title>Meterid Information
    </title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <link rel="stylesheet" type="text/css" href="style.css" />
    <style>
    td{FONT: 13px Arial,sans-serif;}
    th{FONT:bold 13px Arial,sans-serif;color:white;background-color:#3366ff}
    input{FONT: 13px Arial,sans-serif;border: solid 1;}
    select{font: 13px Arial,sans-serif}
    #button{cursor:hand}
    A:hover {COLOR: red}</style>
    <link rel="stylesheet" type="text/css" media="all" href="jsDatePick_ltr.min.css" />
    <script type="text/javascript" src="jquery.1.4.2.js"></script>
    <script type="text/javascript" src="jsDatePick.jquery.min.1.3.js"></script>
    <script type="text/javascript">
    function calendarSetup() {pick("inputField1");pick("inputField2");} //initialize calendar on each date input field 
    function pick(inputField){new JsDatePick({useMode:2,target:inputField,dateFormat:"%d-%m-%Y"});} //display calendar for a given date input field
    </script>
    </head>
    <body onload=calendarSetup()>
    <body onload=calendarSetup()>
    <form action="./meteridinfon.jsp" method="get" name="search">
    &nbsp;<table width="500px;"align="center" border="0" cellpadding="4" cellspacing="3" bgcolor="#cccccc"  bordercolor="#000000">
    <tbody><tr>
    <th colspan="3" align="middle"><strong>Meter ID Readings</strong></th></tr>
    <tr><td>
    <input type=hidden name="mid" value="<%=request.getParameter("mid")%>">
    <label name="name" value="<%=request.getParameter("mid")%>"><font size="4">Meter ID : <%=request.getParameter("mid")%></font></label></td>
    </tr>
    <tr><td class="options1"  colspan="3">
    <font size="4">From</font>
    <input type="text" size="20" maxlength="10" id="inputField1" name="fromDate">
    <font size="4">To</font>
    <input type="text" size="20" id="inputField2" name="toDate">
    </font>
    </td></tr>
    <tr><td style="height: 30px;" colspan="4" align="center">
    <input name="Search" value="Search"  style= "HEIGHT:30px" style="width: 90px;  border: 1px solid rgb(13, 31, 78);" type="submit">
    </td></tr>
    <%
    try
    {
    int mid=Integer.parseInt(request.getParameter("mid")); 
    out.println("mid ===> +"+mid);    
    String fromDate=request.getParameter("fromDate"); 
    String toDate=request.getParameter("toDate"); 
    out.println("fromDate ===> +"+fromDate);
    out.println("toDate ===> +"+toDate);
    String query="select LastReading,Date,Time from Meter_List where Date between str_to_date('"+fromDate+"', '%Y-%m-%d') and str_to_date('"+toDate+"', '%Y-%m-%d') and Meterid="+mid;    
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/suwatermeter","root","sumith");
    st=con.createStatement();
    if(fromDate !=null){        
    rs=st.executeQuery(query);
    }
    //ResultSet rs=st.executeQuery("select LastReading,Date,Time from Meter_List where Meterid="+meterid);
    while(rs.next()){
    %>
    <table width="65%" align="center">
    <tr bgcolor="lightblue">
    <td>Reading</td>
    <td bgcolor="lightblue"><%=rs.getInt("LastReading")%></td>
    </tr>
    <tr bgcolor="lightblue">
    <td>Installation Date</td>
    <td bgcolor="lightblue"><%=rs.getDate("Date")%></td>
    </tr>
    <tr bgcolor="lightblue">
    <td>Inastallation Time</td>
    <td bgcolor="lightblue"><%=rs.getTime("Time")%></td>
    </tr>    
    <%
     }
    rs.close();
    st.close();
    con.close();
    }
    catch(Exception e){
    e.printStackTrace();
    }
    %>
    </table>
    </tbody>
    </table>
    </div>
    </body>
    </html>

在mysql表中我的日期值如下:         2012-05-10,         2012-03-04,         2012-03-04,         2012-09-12,         2012-09-12,         2012-09-12,         2012-09-12

3 个答案:

答案 0 :(得分:0)

SELECT 
    * 
FROM my_table 
WHERE date(date_column1) >= DATE(NOW()) 
AND date(date_column2) <= DATE(NOW())

答案 1 :(得分:0)

你可以使用

select * from my_table where date_coumn between '2012-05-01' AND '2012-05-11'

答案 2 :(得分:0)

select LastReading,Date,Time from Meter_List
where str_to_date(date, '%Y-%m-%d') 
between str_to_date('"+fromDate+"', '%Y-%m-%d') and 
str_to_date('"+toDate+"', '%Y-%m-%d') and Meterid="+mid;