title: Call MySQL query from javascript/AJAX
body: <p>I have a calendar function, where I need to call a function from my MySQL database. The system functions like this: you can choose a "from" date - "to" date. It looks like this:</p>
  
  <p><a href="http://postimg.org/image/5uurc5ycb/" rel="nofollow">http://postimg.org/image/5uurc5ycb/</a></p>
  
  <p>The javascript/AJAX code works and it is successfully connected to my database. So what I want to do is to call the query:</p>
  
  <pre><code>SELECT *, (Day_hours + (Day_minutes / 100)) as Allday_hours FROM Workdata
  </code></pre>
  
  <p>so it will return the column Allday_hours. Does anyone knows how I can do that? Best Regards Mads</p>
  
  <pre><code>&lt;form&gt;
          &lt;input id="start1" /&gt;
          &lt;input id="start2" /&gt;
      &lt;/form&gt;
  
      &lt;script&gt;
  
      $(function(){
          $("#start1").datepicker({
              dateFormat: 'yy-mm-dd',
              onSelect: function(dateText,inst){
                  alert(dateText);
  
                  $.ajax({
                        url: "../dataExchange",
                        type: "post",
                        data: Date,
                        success: function(){
                            alert("success");
                             $("#result").html('submitted successfully');
                        },
                        error:function(){
                            alert("failure");
                            $("#result").html('there is error while submit');
                        }   
                      });
              }
          });
      });
  
  
      $(function(){
          $("#start2").datepicker({
              dateFormat: 'yy-mm-dd',
              onSelect: function(dateText,inst){
                  alert(dateText);
  
                  $.ajax({
                        url: "../dataExchange",
                        type: "post",
                        data: Date,
                        success: function(){
                            alert("success");
                             $("#result").html('submitted successfully');
                        },
                        error:function(){
                            alert("failure");
                            $("#result").html('there is error while submit');
                        }   
                      });
              }
          });
      });
  
  &lt;/script&gt;
  </code></pre>
  
  <p>My connection to the database is going through a servlet, that looks like this:</p>
  
  <pre><code>package WorkPackage;
  
  import java.io.*;
  import java.sql.*;
  import javax.servlet.*;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.*;
  
  @WebServlet("/dataExchange")
  public class dataExchange extends HttpServlet{
  
      private static final long serialVersionUID = 1L;
  
      @Override
      protected void doGet(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
          doPost(request, response);
      }
  
      public void init(ServletConfig config) throws ServletException{
          super.init(config);
      }
  
      public void doPost(HttpServletRequest req, HttpServletResponse res) 
          throws ServletException, IOException{
  
          String connectionURL = "jdbc:mysql://localhost/NekiWork";
          Connection connection=null;
  
          res.setContentType("text/html");
          PrintWriter out = res.getWriter();
  
          String Date = req.getParameter("Date");
          String Name = req.getParameter("Name");
          String Address = req.getParameter("Address");
          String Day_hours = req.getParameter("Day_hours");
          String Day_minutes = req.getParameter("Day_minutes");
          String Km_to_address = req.getParameter("Km_to_address");
          String Time_to_address = req.getParameter("Time_to_address");
  
          try {
  
              Class.forName("com.mysql.jdbc.Driver");
              connection = DriverManager.getConnection(connectionURL, "root", ""); 
              String sql = "INSERT INTO Workdata (Date, Name, Address, Day_hours, Day_minutes, Km_to_address, Time_to_address) VALUES (?,?,?,?,?,?,?)"; 
              PreparedStatement pst = connection.prepareStatement(sql);
              pst.setString(1, Date);
              pst.setString(2, Name);
              pst.setString(3, Address);
              pst.setString(4, Day_hours);
              pst.setString(5, Day_minutes);
              pst.setString(6, Km_to_address);
              pst.setString(7,  Time_to_address);
  
              pst.executeUpdate();
              pst.close();
          }
          catch(ClassNotFoundException e){
  
              out.println("Couldn't load database driver: " + e.getMessage());
          }
          catch(SQLException e){
              out.println("SQLException caught: " + e.getMessage());
          }
          catch (Exception e){
              out.println(e);
          }
          finally {
  
          try {
              if (connection != null) connection.close();
          }
              catch (SQLException ignored){
                  out.println(ignored);
              }
          }
      }
  }
  </code></pre>