2014-03-05 19 views
0

我有一個日曆功能,我需要從我的MySQL數據庫調用一個函數。系統功能如下:您可以選擇「從」日期 - 「到」日期。它看起來像這樣:從javascript/AJAX調用MySQL查詢

http://postimg.org/image/5uurc5ycb/

的JavaScript/AJAX代碼工作,而且成功地連接到我的數據庫。所以我想要做的就是打電話查詢:

SELECT *, (Day_hours + (Day_minutes/100)) as Allday_hours FROM Workdata 

所以它會返回列Allday_hours。有誰知道我該怎麼做?順祝商祺的Mads

<form> 
     <input id="start1" /> 
     <input id="start2" /> 
    </form> 

    <script> 

    $(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'); 
         } 
        }); 
      } 
     }); 
    }); 

</script> 

我的數據庫連接是通過servlet去,看起來像這樣:

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); 
      } 
     } 
    } 
} 
+0

JavaScript無法直接與MySQL連接!?!? – Strawberry

+0

我編輯了我的問題。我忘了寫我的連接通過一個servlet: -/ – McDuck4

+0

我想我不確定你在哪裏掛斷了。通過查看你的servlet,看起來你已經知道如何對數據庫進行查詢了。什麼阻止你簡單地運行你想要的數據庫查詢?換句話說,當你嘗試這個時,你遇到的具體問題是什麼? –

回答

0

我沒有使用Servlet多少經驗,但基本上,你可以指定哪些行動應該阿賈克斯這樣進行:

$.ajax({ 
    url: "../dataExchange", 
    type: "post", 
    data: {date: Date, action: "doTheSelect"}, 
    dataType: 'json', 
    success: function(data){ 
     alert("success"); 
     $("#result").html('submitted successfully'); 

     // do something with variable - data(returned json object) 
    }, 
    error:function(){ 
     alert("failure"); 
     $("#result").html('there is error while submit'); 
    } 
}); 

而且在你的servlet如果指定的條件是這樣的(僞):

if(posted_data.action == "doTheSelect") { 
    // here goes the SQL query 

    return "json encoded result of query"; 
} else { 
    // do some other stuff like specifying another condition based on another value of action variable 
} 

這樣只執行提到的查詢。最後你可以在ajax.success函數中處理收到的數據。