Datepicker始终返回相同的值

时间:2014-03-09 14:16:03

标签: javascript mysql calendar

感谢这里的一些人,我让我的日历工作。我的问题有点难以解释,所以我做了一个简短的屏幕录制我的问题是什么:

https://www.youtube.com/watch?v=maXwOJRWf28

正如您可能在视频中看到的那样,有一个

Day_hours       Day_minutes
   3                 45
   4                 15
   5                 45 
   2                 15

我想在特定时间段内一起计算小时和分钟。我的意思是,我会得到一个数字,表示该时间段内有多少小时和分钟。它适用于计算。视频中的Fx我选择日期2014-03-03 - 2014-03-04并获得15.2小时的结果。如果我然后选择日期2014-03-24 - 2014-03-28我仍然得到结果15.2小时,即使这些日期没有数据。所以不知道它,我认为我的javascript dosent得到我选择的日期,servlet只是遍历数据库中的所有列并取这些的SUM?有人可以看到它是我的javascript我需要更多的代码,还是在我的servlet中?

最诚挚的问候 MADS

Javascript代码:

    <form>
        <input id="startDate" />
        <input id="endDate" />
    </form>
    <div id="startresult"></div>
    <div id="endresult"></div>
    <script>

    $(function(){
        $("#startDate").datepicker({
            dateFormat: 'yy-mm-dd',
            onSelect: function(dateText,inst){
                $('.selected-date').html(dateText);

                $.ajax({
                      url: "../getHoursSQL",
                      type: "post",
                      data: JSON,
                      success: function(data){
                          alert("success");
                          $("#startresult").html(data);

                      },
                      error:function(){
                          alert("failure");
                          $("#startresult").html('there is error while submit');
                      }  
                    });
            }
        });
    });

    $(function(){
            $("#endDate").datepicker({
                dateFormat: 'yy-mm-dd',
                onSelect: function(dateText,inst){
                    $('.selected-date').html(dateText);

                    $.ajax({
                          url: "../getHoursSQL",
                          type: "post",
                          data: JSON,
                          success: function(data){
                              alert("success");
                              $("#endresult").html(data);
                          },
                          error:function(){
                              alert("failure");
                              $("#result").html('there is error while submit');
                          }  
                        });
                }
            });
        });

</script>

Servlet代码:

package WorkPackage;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet("/getHoursSQL")
public class getHoursSQL extends HttpServlet{

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) 
        throws ServletException, IOException{

        String connectionURL = "jdbc:mysql://localhost/NekiWork";
        Connection connection=null;

        try {

            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(connectionURL, "root", ""); 
            String sql = "SELECT *, (Day_hours + (Day_minutes / 100)) AS Allday_hours FROM Workdata";
            PreparedStatement pst = connection.prepareStatement(sql);

            ResultSet rs = pst.executeQuery(sql);

            float Allday_hours_sum = 0;
                while (rs.next()){                                      
                    Allday_hours_sum += Float.parseFloat (rs.getString("Allday_hours")); 

                }   
                res.setContentType("text/html;charset=UTF-8");          
                res.getWriter().print(Allday_hours_sum);    

            pst.close();

        }
        catch(ClassNotFoundException e){

            System.out.println("Couldn't load database driver: " + e.getMessage());
        }
        catch(SQLException e){
            System.out.println("SQLException caught: " + e.getMessage());
        }
        catch (Exception e){
            System.out.println(e);
        }
        finally {

            try {
                if (connection != null) connection.close();
            }
            catch (SQLException ignored){
                System.out.println(ignored);
            }
        }
    }
}

0 个答案:

没有答案