更新MySql javascritpt中的数据

时间:2015-11-28 11:15:14

标签: javascript mysql database jsp

我需要在javascript中更新db MySql中的数据。 我有一个函数将参数传递给servlet但似乎不起作用。 这是我的功能

    function editTable(id,valore){
        var url = "../ModificaPersonaAttivitaServlet?";
        url += "type=perso_atti&value1=" + id+"&value2="+valore;
        xmlhttp4.onreadystatechange = handlerForEdit;
        xmlhttp4.open("GET", url);
        xmlhttp4.send("");

    }

这里是我的handlerForEdit

    function handlerForEdit(){

    if (xmlhttp4.readyState == 4 && xmlhttp4.status == 200) {
            var jsonObject = eval('(' + xmlhttp4.responseText + ')');

    }
    }

这是我的servlet

    package it.timesheet.servlet.ajax;

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import com.mysql.jdbc.Connection;
    import com.mysql.jdbc.PreparedStatement;
    import com.mysql.jdbc.Statement;

    import it.timesheet.pojo.PersonaAttività;
    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;

    /**
     * Servlet implementation class LoadPersonaAttivitàServlet
     */
    @WebServlet("/modificaPersonaAttivitaServlet")
    public class ModificaPersonaAttivitaServlet extends HttpServlet
    {
        private static final long serialVersionUID = 1L;

        private final String TYPE_OF_REQUEST = "type";

        private final String personaattività_TYPE = "perso_atti";

        private final String SELECT_VALUE_KEY1 = "value1";
        private final String SELECT_VALUE_KEY2 = "value2";

        /**
         * @see HttpServlet#HttpServlet()
         */
        public ModificaPersonaAttivitaServlet()
        {
            super();
        }

        /**
         * @see HttpServlet#doGet(HttpServletRequest request,         HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            String type = request.getParameter(TYPE_OF_REQUEST);
            response.setContentType("application/json");
            response.setHeader("Cache-Control", "no-cache");

            if (personaattività_TYPE.equals(type))
            {
                String id = request.getParameter(SELECT_VALUE_KEY1);

                String ore=request.getParameter(SELECT_VALUE_KEY2);
                Connection connection = null;
                try {
                    connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","wxkj32skapi");
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                try
                {
                    Statement pst2= (Statement) connection.createStatement();
                    String query="UPDATE personaattività SET NumeroOre="+ore+" WHERE ID_personaattività=" + id ;
                    pst2.executeUpdate(query);

                }
                catch (SQLException e)
                {

                    e.printStackTrace();
                }
                catch (IllegalArgumentException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        }

        /**
         * @see HttpServlet#doPost(HttpServletRequest request,         HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request,         HttpServletResponse response) throws ServletException, IOException
        {
            // TODO Auto-generated method stub
        }

    }
你能帮帮我吗?非常感谢你

1 个答案:

答案 0 :(得分:0)

首先,您需要调试问题所在。在浏览器控制台中查看请求状态。它会告诉你很好的见解。

同样将调试器放在servlet方法的入口点,它会告诉你客户端没有问题,但在服务器端。可能是一些运行时异常。