iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >AJAX如何实现数据的增删改查操作
  • 208
分享到

AJAX如何实现数据的增删改查操作

2023-06-08 06:06:26 208人浏览 泡泡鱼
摘要

这篇文章主要介绍了ajax如何实现数据的增删改查操作,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。主页:index.html<!DOCTYPE html>

这篇文章主要介绍了ajax如何实现数据的增删改查操作,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

主页:index.html

<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <script src="Http://libs.baidu.com/Jquery/2.1.4/jquery.min.js"></script> </head> <body> 编号:<input type="text" value="" id="pno"/><br> 姓名:<input type="text" value="" id="name"/><br> 性别:男:<input type="radio" name="sex" value="男">女:<input type="radio" name="sex" value="女"><br> 年龄:<select id="age">  <option value="15">15</option>  <option value="16">16</option>  <option value="17">17</option>  <option value="18">18</option>  <option value="19">19</option>  <option value="20">20</option>  <option value="21">21</option>  <option value="22">22</option>  <option value="23">23</option>  <option value="24">24</option>  <option value="25">25</option> </select><br> 身高:<input type="text" value="" id="height"/><br> 体重:<input type="text" value="" id="weight"/><br> <input type="button" value="插入" id="btn_1" onclick="submit()"/> <br> <br> <br>  编号:<input type="text" value="" id="pno_query"/> <input type="button" value="查询" id="btn_2" onclick="query()"/> <table id="queryResult">  <tr>  <td>编号</td>  <td>姓名</td>  <td>性别</td>  <td>年龄</td>  <td>身高</td>  <td>体重</td>  </tr>  <tr>  <td></td>  <td></td>  <td></td>  <td></td>  <td></td>  <td></td>  </tr> </table>   <br> <br> <br> 编号:<input type="text" value="" id="pno_del"/> <input type="button" value="删除" id="btn_3" onclick="del()"/>  <br> <br> <br> 编号:<input type="text" value="" id="pno_up"/><br> 姓名:<input type="text" value="" id="name_up"/><br> 性别:男:<input type="radio" name="sex_up" value="男">女:<input type="radio" name="sex_up" value="女"><br> 年龄:<select id="age_up">  <option value="15">15</option>  <option value="16">16</option>  <option value="17">17</option>  <option value="18">18</option>  <option value="19">19</option>  <option value="20">20</option>  <option value="21">21</option>  <option value="22">22</option>  <option value="23">23</option>  <option value="24">24</option>  <option value="25">25</option> </select><br> 身高:<input type="text" value="" id="height_up"/><br> 体重:<input type="text" value="" id="weight_up"/><br> <input type="button" value="更新" id="btn_4" onclick="update()"/>  </body>  <script type="text/javascript">  function submit() { var pno = $("#pno").val(); var name = $("#name").val(); var sex = $('input[name="sex"]:checked').val(); var age = $("#age").val(); var height = $("#height").val(); var weight = $("#weight").val();  var data={    "pno":pno,  "name":name,  "sex":sex,  "age":age,  "height":height,  "weight" : weight }   $.ajax({  type : "post",  url : "Hello",  data : data,  cache : true,  async : true,  success: function (data ,textStatus, jqXHR){     if(data.code == 200){      alert("插入成功了");     }else{      alert(data.message);     }   },     error:function (XMLHttpRequest, textStatus, errorThrown) {                alert(typeof(errorThrown));     }   }); }   function query() {  var pno = $("#pno_query").val();  var str = ["编号","姓名","性别","年龄","身高","体重"]; $.ajax({  type : "post",  url : "HelloQuery",  data : {  "pno": pno  },  cache : true,  async : true,  success: function (data ,textStatus, jqXHR){  //data = $.parseJSON(data);  var j = 0;  var x = 1;  //for(var i=1; i <20; i++) {   for(var p in data){//遍历json对象的每个key/value对,p为key   console.log(data[p]);   if(j == 6) {    j = 0;    x++;   }    $("#queryResult tr:eq("+x+") td:eq("+j+")").html(data[p]);    console.log(data[p]);    j++;   }  //}              },     error:function (XMLHttpRequest, textStatus, errorThrown) {                alert(typeof(errorThrown));     }   }); }  function del() { var pno = $("#pno_del").val();   $.ajax({  type : "post",  url : "HelloDelete",  data : {  "pno": pno  },  cache : true,  async : true,  success: function (data ,textStatus, jqXHR){  if(data.code == 200){      alert("删除成功了");     }else{      alert(data.message);     }   },     error:function (XMLHttpRequest, textStatus, errorThrown) {                alert(typeof(errorThrown));     }   }); }  function update() { var pno = $("#pno_up").val(); var name = $("#name_up").val(); var sex = $('input[name="sex_up"]:checked').val(); var age = $("#age_up").val(); var height = $("#height_up").val(); var weight = $("#weight_up").val();  var data={    "pno":pno,  "name":name,  "sex":sex,  "age":age,  "height":height,  "weight" : weight }   $.ajax({  type : "post",  url : "HelloUpdate",  data : data,  cache : true,  async : true,  success: function (data ,textStatus, jqXHR){     if(data.code == 200){      alert("更新成功了");     }else{      alert(data.message);     }   },     error:function (XMLHttpRequest, textStatus, errorThrown) {                alert(typeof(errorThrown));     }   }); }    </script></html>

增加的Serlvet:Hello.java

package com.WEB; import java.io.IOException;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.MysqlUtil; @WebServlet("/Hello")public class Hello extends HttpServlet { private static final long serialVersionUID = 1L;        public Hello() {    super();    // TODO Auto-generated constructor stub  }   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); }   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  response.setCharacterEncoding("utf-8"); response.setContentType("application/json; charset=utf-8");  String pno = request.getParameter("pno"); String name = request.getParameter("name"); String sex = request.getParameter("sex"); String age = request.getParameter("age"); String height = request.getParameter("height"); String weight = request.getParameter("weight");  String sqlInsert = "INSERT INTO Person (Pno,Pname,Psex,Page,Pheight,Pweight) VALUES('"; sqlInsert += pno +"','"; sqlInsert += name +"','"; sqlInsert += sex +"',"; sqlInsert += age +","; sqlInsert += height +","; sqlInsert += weight +")";  int message = mysqlUtil.add(sqlInsert); String rep = ""; if(message == 1) {  rep = "{\"code\":200,\"message\":\"成功插入数据库\"}"; }else {  rep = "{\"code\":\"999\",\"message\":\"插入失败了\"}"; } response.getWriter().write(rep);   } }

删除的Servlet:HelloDelete.java

package com.web; import java.io.IOException;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.MysqlUtil; @WebServlet("/HelloDelete")public class HelloDelete extends HttpServlet { private static final long serialVersionUID = 1L;        public HelloDelete() {    super();    // TODO Auto-generated constructor stub  }   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); }   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); response.setContentType("application/json; charset=utf-8");  String pno = request.getParameter("pno");   String sqlDel = "delete from Person where pno="+pno;   int message = MysqlUtil.del(sqlDel); String rep = ""; if(message == 1) {  rep = "{\"code\":\"200\",\"message\":\"成功删除\"}"; }else {  rep = "{\"code\":\"999\",\"message\":\"删除失败\"}"; } response.getWriter().write(rep); } }

更新的Servlet:HelloUpdate.java

package com.web; import java.io.IOException;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.MysqlUtil; @WebServlet("/HelloUpdate")public class HelloUpdate extends HttpServlet { private static final long serialVersionUID = 1L;        public HelloUpdate() {    super();    // TODO Auto-generated constructor stub  }   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); }   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); response.setContentType("application/json; charset=utf-8");  String pno = request.getParameter("pno"); String name = request.getParameter("name"); String sex = request.getParameter("sex"); String age = request.getParameter("age"); String height = request.getParameter("height"); String weight = request.getParameter("weight");  String sqlupdate = "update Person set ";// sqlupdate += "Pno='"+ pno +"',"; sqlupdate += "Pname='"+ name +"',"; sqlupdate += "Psex='"+ sex +"',"; sqlupdate += "Page="+ age +","; sqlupdate += "Pheight="+ height +","; sqlupdate += "Pweight="+ weight; sqlupdate += " where Pno='"+pno+"'"; System.out.println(sqlupdate); int message = MysqlUtil.update(sqlupdate); String rep = ""; if(message == 1) {  rep = "{\"code\":\"200\",\"message\":\"成功插入数据库\"}"; }else {  rep = "{\"code\":\"999\",\"message\":\"插入失败了\"}"; } response.getWriter().write(rep);  } }

查询的Servlet:HelloQuery.java

package com.web; import java.io.IOException;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.Map; 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.MysqlUtil; @WebServlet("/HelloQuery")public class HelloQuery extends HttpServlet { private static final long serialVersionUID = 1L;        public HelloQuery() {    super();    // TODO Auto-generated constructor stub  }   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); }   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); response.setContentType("application/json; charset=utf-8"); String pno = request.getParameter("pno"); String[] params = {"Pno","Pname","Psex","Page","Pheight","Pweight"}; String sql = "select * from Person where Pno="+pno; String data = "{";  String[] str = {"编号","姓名","性别","年龄","身高","体重"}; List<Map<String,String>> listmap = new ArrayList<>(); listmap = MysqlUtil.show(sql, params); for(int i =0 ; i<listmap.size();i++) {    for(int j=0 ; j<listmap.get(i).size();j++) {  data += "\""+str[j]+"\":"+"\""+listmap.get(i).get(params[j])+"\",";    } } data = data.substring(0, data.length()-1); data += "}";   System.out.println(data); response.getWriter().write(data); }   }

页面如下:

AJAX如何实现数据的增删改查操作

对应的数据库:

AJAX如何实现数据的增删改查操作

感谢你能够认真阅读完这篇文章,希望小编分享的“AJAX如何实现数据的增删改查操作”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网精选频道,更多相关知识等着你来学习!

--结束END--

本文标题: AJAX如何实现数据的增删改查操作

本文链接: https://www.lsjlt.com/news/252237.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C++ 生态系统中流行库和框架的贡献指南
    作为 c++++ 开发人员,通过遵循以下步骤即可为流行库和框架做出贡献:选择一个项目并熟悉其代码库。在 issue 跟踪器中寻找适合初学者的问题。创建一个新分支,实现修复并添加测试。提交...
    99+
    2024-05-15
    框架 c++ 流行库 git
  • C++ 生态系统中流行库和框架的社区支持情况
    c++++生态系统中流行库和框架的社区支持情况:boost:活跃的社区提供广泛的文档、教程和讨论区,确保持续的维护和更新。qt:庞大的社区提供丰富的文档、示例和论坛,积极参与开发和维护。...
    99+
    2024-05-15
    生态系统 社区支持 c++ overflow 标准库
  • c++中if elseif使用规则
    c++ 中 if-else if 语句的使用规则为:语法:if (条件1) { // 执行代码块 1} else if (条件 2) { // 执行代码块 2}// ...else ...
    99+
    2024-05-15
    c++
  • c++中的继承怎么写
    继承是一种允许类从现有类派生并访问其成员的强大机制。在 c++ 中,继承类型包括:单继承:一个子类从一个基类继承。多继承:一个子类从多个基类继承。层次继承:多个子类从同一个基类继承。多层...
    99+
    2024-05-15
    c++
  • c++中如何使用类和对象掌握目标
    在 c++ 中创建类和对象:使用 class 关键字定义类,包含数据成员和方法。使用对象名称和类名称创建对象。访问权限包括:公有、受保护和私有。数据成员是类的变量,每个对象拥有自己的副本...
    99+
    2024-05-15
    c++
  • c++中优先级是什么意思
    c++ 中的优先级规则:优先级高的操作符先执行,相同优先级的从左到右执行,括号可改变执行顺序。操作符优先级表包含从最高到最低的优先级列表,其中赋值运算符具有最低优先级。通过了解优先级,可...
    99+
    2024-05-15
    c++
  • c++中a+是什么意思
    c++ 中的 a+ 运算符表示自增运算符,用于将变量递增 1 并将结果存储在同一变量中。语法为 a++,用法包括循环和计数器。它可与后置递增运算符 ++a 交换使用,后者在表达式求值后递...
    99+
    2024-05-15
    c++
  • c++中a.b什么意思
    c++kquote>“a.b”表示对象“a”的成员“b”,用于访问对象成员,可用“对象名.成员名”的语法。它还可以用于访问嵌套成员,如“对象名.嵌套成员名.成员名”的语法。 c++...
    99+
    2024-05-15
    c++
  • C++ 并发编程库的优缺点
    c++++ 提供了多种并发编程库,满足不同场景下的需求。线程库 (std::thread) 易于使用但开销大;异步库 (std::async) 可异步执行任务,但 api 复杂;协程库 ...
    99+
    2024-05-15
    c++ 并发编程
  • 如何在 Golang 中备份数据库?
    在 golang 中备份数据库对于保护数据至关重要。可以使用标准库中的 database/sql 包,或第三方包如 github.com/go-sql-driver/mysql。具体步骤...
    99+
    2024-05-15
    golang 数据库备份 mysql git 标准库
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作