电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

AJAX實現(xiàn)數(shù)據(jù)的增刪改查操作詳解【java后臺】

瀏覽:381日期:2022-06-11 17:03:10

本文實例講述了AJAX實現(xiàn)數(shù)據(jù)的增刪改查操作。分享給大家供大家參考,具體如下:

主頁: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"> /* var x = $("#queryResult").html();  for(var i=0; i < 20 ; i++) {  x += "<tr><td></td><td></td><td></td><td></td><td></td><td></td></tr>"; } $("#queryResult").html(x);*/ 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; /** * Servlet implementation class Hello */@WebServlet("/Hello")public class Hello extends HttpServlet { private static final long serialVersionUID = 1L;      /**   * @see HttpServlet#HttpServlet()   */  public Hello() {    super();    // TODO Auto-generated constructor stub  }  /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); }  /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ 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\":\"成功插入數(shù)據(jù)庫\"}"; }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; /** * Servlet implementation class HelloDelete */@WebServlet("/HelloDelete")public class HelloDelete extends HttpServlet { private static final long serialVersionUID = 1L;      /**   * @see HttpServlet#HttpServlet()   */  public HelloDelete() {    super();    // TODO Auto-generated constructor stub  }  /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); }  /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ 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; /** * Servlet implementation class HelloUpdate */@WebServlet("/HelloUpdate")public class HelloUpdate extends HttpServlet { private static final long serialVersionUID = 1L;      /**   * @see HttpServlet#HttpServlet()   */  public HelloUpdate() {    super();    // TODO Auto-generated constructor stub  }  /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); }  /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ 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\":\"成功插入數(shù)據(jù)庫\"}"; }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; /** * Servlet implementation class HelloQuery */@WebServlet("/HelloQuery")public class HelloQuery extends HttpServlet { private static final long serialVersionUID = 1L;      /**   * @see HttpServlet#HttpServlet()   */  public HelloQuery() {    super();    // TODO Auto-generated constructor stub  }  /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); }  /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ 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); }   }

頁面如下:

對應(yīng)的數(shù)據(jù)庫:

git克隆地址:https://github.com/dreamiboy/JDBCUtil.git

更多關(guān)于ajax相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jquery中Ajax用法總結(jié)》、《JavaScript中ajax操作技巧總結(jié)》、《PHP+ajax技巧與應(yīng)用小結(jié)》及《asp.net ajax技巧總結(jié)專題》

希望本文所述對大家ajax程序設(shè)計有所幫助。

標(biāo)簽: Ajax
相關(guān)文章:
主站蜘蛛池模板: 浙江华锤电器有限公司_地磅称重设备_防作弊地磅_浙江地磅售后维修_无人值守扫码过磅系统_浙江源头地磅厂家_浙江工厂直营地磅 | 冷热冲击试验箱_温度冲击试验箱价格_冷热冲击箱排名_林频厂家 | 电动高尔夫球车|电动观光车|电动巡逻车|电动越野车厂家-绿友机械集团股份有限公司 | 德州万泰装饰 - 万泰装饰装修设计软装家居馆 | 烟台游艇培训,威海游艇培训-烟台市邮轮游艇行业协会 | 加气混凝土砌块设备,轻质砖设备,蒸养砖设备,新型墙体设备-河南省杜甫机械制造有限公司 | PSI渗透压仪,TPS酸度计,美国CHAI PCR仪,渗透压仪厂家_价格,微生物快速检测仪-华泰和合(北京)商贸有限公司 | BOE画框屏-触摸一体机-触控查询一体机-触摸屏一体机价格-厂家直销-触发电子 | 广东成考网-广东成人高考网 | 咖啡加盟-咖啡店加盟-咖啡西餐厅加盟-塞纳左岸咖啡西餐厅官网 | 二氧化碳/活性炭投加系统,次氯酸钠发生器,紫外线消毒设备|广州新奥 | 防爆电机生产厂家,YBK3电动机,YBX3系列防爆电机,YBX4节防爆电机--河南省南洋防爆电机有限公司 | PTFE接头|聚四氟乙烯螺丝|阀门|薄膜|消解罐|聚四氟乙烯球-嘉兴市方圆氟塑制品有限公司 | 危废处理系统,水泥厂DCS集散控制系统,石灰窑设备自动化控制系统-淄博正展工控设备 | 炭黑吸油计_测试仪,单颗粒子硬度仪_ASTM标准炭黑自销-上海贺纳斯仪器仪表有限公司(HITEC中国办事处) | 有机废气处理-rto焚烧炉-催化燃烧设备-VOC冷凝回收装置-三梯环境 | 江苏全风,高压风机,全风环保风机,全风环形高压风机,防爆高压风机厂家-江苏全风环保科技有限公司(官网) | 沈阳庭院景观设计_私家花园_别墅庭院设计_阳台楼顶花园设计施工公司-【沈阳现代时园艺景观工程有限公司】 | 塑钢课桌椅、学生课桌椅、课桌椅厂家-学仕教育设备首页 | 无菌实验室规划装修设计-一体化实验室承包-北京洁净净化工程建设施工-北京航天科恩实验室装备工程技术有限公司 | 天津热油泵_管道泵_天津高温热油泵-天津市金丰泰机械泵业有限公司【官方网站】 | 上海办公室装修,办公楼装修设计,办公空间设计,企业展厅设计_写艺装饰公司 | 废水处理-废气处理-工业废水处理-工业废气处理工程-深圳丰绿环保废气处理公司 | 曙光腾达官网-天津脚手架租赁-木板架出租-移动门式脚手架租赁「免费搭设」 | 哈希余氯测定仪,分光光度计,ph在线监测仪,浊度测定仪,试剂-上海京灿精密机械有限公司 | nalgene洗瓶,nalgene量筒,nalgene窄口瓶,nalgene放水口大瓶,浙江省nalgene代理-杭州雷琪实验器材有限公司 | 芝麻黑-芝麻黑石材厂家-永峰石业 | 隧道烘箱_隧道烘箱生产厂家-上海冠顶专业生产烘道设备 | 山东PE给水管厂家,山东双壁波纹管,山东钢带增强波纹管,山东PE穿线管,山东PE农田灌溉管,山东MPP电力保护套管-山东德诺塑业有限公司 | 电动百叶窗,开窗器,电动遮阳百叶,电动开窗机生产厂家-徐州鑫友工控科技发展有限公司 | 百度爱采购运营研究社社群-店铺托管-爱采购代运营-良言多米网络公司 | 空气能采暖,热泵烘干机,空气源热水机组|设备|厂家,东莞高温热泵_正旭新能源 | 小型铜米机-干式铜米机-杂线全自动铜米机-河南鑫世昌机械制造有限公司 | 芜湖厨房设备_芜湖商用厨具_芜湖厨具设备-芜湖鑫环厨具有限公司 控显科技 - 工控一体机、工业显示器、工业平板电脑源头厂家 | 防水套管-柔性防水套管-刚性防水套管-上海执品管件有限公司 | 桁架楼承板-钢筋桁架楼承板-江苏众力达钢筋楼承板厂 | 换链神器官网-友情链接交换、购买交易于一体的站长平台 | 精密冲床,高速冲床等冲压设备生产商-常州晋志德压力机厂 | 在线悬浮物浓度计-多参数水质在线检测仪-上海沃懋仪表科技有限公司 | 铝板冲孔网,不锈钢冲孔网,圆孔冲孔网板,鳄鱼嘴-鱼眼防滑板,盾构走道板-江拓数控冲孔网厂-河北江拓丝网有限公司 | 无线遥控更衣吊篮_IC卡更衣吊篮_电动更衣吊篮配件_煤矿更衣吊篮-力得电子 |