Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax jsp-如何将值发送到数据库表以进行处理?_Ajax_Jsp - Fatal编程技术网

Ajax jsp-如何将值发送到数据库表以进行处理?

Ajax jsp-如何将值发送到数据库表以进行处理?,ajax,jsp,Ajax,Jsp,我是AJAX新手,我有一个JSP页面,如下所示: <script > function update(param) { var currentbal = document.getElementsByName('NewUserName' + param)[0].value; console.log(currentbal); $.ajax({ type: "POST", ur

我是AJAX新手,我有一个JSP页面,如下所示:

<script >
    function update(param) {


        var currentbal = document.getElementsByName('NewUserName' + param)[0].value;


        console.log(currentbal);

        $.ajax({
            type: "POST",
            url: "update.jsp",
            data: { name: currentbal},
            success:function( msg ) {
                alert( "Data Updated: " + msg );
            },error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
            }
        });

    }




</script>

新的username列允许我输入任何文本字符串,当我点击updateuser按钮时,它应该会更新数据库中的表

数据库结构中的“我的表”有一个唯一的列
AUSERNAME
,它存储用户名,如屏幕截图所示。单击更新按钮后,它将传递新的用户名值并更新表中的该字段。这就是我想要达到的目标

我知道我必须在JSP页面中进行AJAX调用,如下所示:

<script >
    function update(param) {


        var currentbal = document.getElementsByName('NewUserName' + param)[0].value;


        console.log(currentbal);

        $.ajax({
            type: "POST",
            url: "update.jsp",
            data: { name: currentbal},
            success:function( msg ) {
                alert( "Data Updated: " + msg );
            },error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
            }
        });

    }




</script>

函数更新(参数){
var currentbal=document.getElementsByName('NewUserName'+param)[0]。值;
控制台日志(currentbal);
$.ajax({
类型:“POST”,
url:“update.jsp”,
数据:{name:currentbal},
成功:功能(msg){
警报(“数据更新:+msg”);
},错误:函数(xhr、ajaxOptions、thrownError){
警报(xhr.状态);
警报(thrownError);
}
});
}

至于我的
update.jsp
页面,我不太确定如何处理它并对表进行更改。我认为我需要在其中创建一个函数,但不确定如何将值传递给它并使用sql语句更新它。

在jsp页面中,您可以执行以下操作:

<%@page import= "java.sql.*" %>
<%@page import= "java.io.*" %>

<% 
    //getting data send from ajax request 

   String id= request.getParameter("id");
String user = request.getParameter("name"); //name for example
  //data base code
   try{
                    Class.forName("com.mysql.jdbc.Driver");
                }
                catch (ClassNotFoundException e) {
            e.printStackTrace();
                }

                Connection con = null;
                ResultSet resultSet = null;
                Statement stmt = null;
               int row=0;
 try {
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/abc","root","swati");
String sql = "UPDATE `tablename` SET `user`=? WHERE `id`=?";      //updating table
            PreparedStatement statement = con.prepareStatement(sql);
            statement.setString(1,  name); 
            statement.setString(2,  id); 
                        row = statement.executeUpdate(); 
            if (row > 0) {
               String txt="updated";
            // forwards to the message page
           }
          }
          else {
            String txt="not updated";
             }
          }
         catch (Exception ex) {
           ex.printStackTrace();
        } 

        }
      response.setContentType("text/plain");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(txt);

        %>  

0) {
String txt=“已更新”;
//转发到消息页面
}
}
否则{
String txt=“未更新”;
}
}
捕获(例外情况除外){
例如printStackTrace();
} 
}
response.setContentType(“文本/普通”);
响应。setCharacterEncoding(“UTF-8”);
response.getWriter().write(txt);
%>  

希望这对你有帮助

发布您的jsp代码还有一个问题,我有一个问题。是的,您尝试过吗?是的,但我需要一种方法来检索id,因为它是用java
rs.getString编写的。请查看我的新帖子: