亚洲女同成aV人片在线观看|亚洲www啪成人一区二区麻豆|亚洲国产中日韩精品综合|亚洲国产成人精品一级片|亚洲无码在线视频免费

您的當前位置: 首頁(yè) > 百度SEO工具

AJAX實(shí)現數據的增刪改查操作詳解java后臺

發(fā)布時(shí)間:2026-05-04 15:14:53 瀏覽:6 次

這篇文章主要介紹了AJAX實(shí)現數據的實(shí)現數據刪改增刪改查操作,結合實(shí)例形式詳細分析了ajax結合java后臺實(shí)現數據庫增刪改查相關(guān)操作技巧,需要的朋友可(ke)以參考下

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

主頁(yè):index.html??(O_O)

<!(′?`*)DOCTYPE html>
<html>
&(╯°□°)╯︵ ┻━┻lt;head>
<meta cha??rset="UTF-8">
<title></title>
&lヾ(^-^)ノt;scri??pt src='http://libs.baidu.com/jquery/2.1.4/jquery.min.js??'>&l??t;/script>
</head>
<bo(′?`)dy>
編號:<input type="text" value="" id='pno'/><??br>
姓名:<input type='text' value='' id='name'/&?gt;&l??t;br>
性別:男:<input type="radio" name="sex" value="男">女:<input type="radio"??? name='sex' value="女"><br&gヾ(′▽?zhuān)??t;
年齡:<select id??="age">
<option value="15">15</option>
<option value='16'>16</option>
<opti(′▽?zhuān)?on value='17'>17ヽ(′▽?zhuān)?/</option>
<option value="18(′▽?zhuān)?">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>
<op??tion value='25'>25</option>
</select><br>
身高:<inpu??t 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'/>
<inpu??t type='button' valu(′?ω?`)e='查詢(xún)' id='btn_2' onclick="que??ry()"/>
<table id='queryResult'&g(??ヮ?)?*:???t;
<tr>
<(╥_╥)td>編號</td>
<td>姓名</td>
<td>性別</td>
<td>年齡</td>
<td>身高</td>
<td>體重</td>
</??tr>
<tr>
<td></td>
<td></tヾ(^-^)ノd>
<td></td>
<td>??</td>
<td></td>
<td></td>
</tr>
</table>


<br>
<br>
<br>
編號:<input type="text" value="" id="pno_del"/>
<input type="button" valu(??ヮ?)?*:???e="刪除" id="btn_3" onclick='del()'/>

<br>
<br>
<br>
編號:<input type='text' value='' id='pno_up'/><br(′_ゝ`)>
姓名:<input type='text' value='' id='n?ame_up'/&??gt;<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>
<(°o°)option value='16'>16</option>
<option value='17'>17</o┐(′?`)┌ptio??n>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20&l??t;/option>
<option value='21'>21</option>
<option value='22'>22</option>
<option value=??"23">2???3</option>
<ヾ(?■_■)ノ;option value="24">24</option>
<option value="25"??>25</option>??
</select><br>
身高:<input type='text??' value='' id="(′ω`)he(O_O)ight_up"/><br>
體重:<input type="text" va(′?_?`)lue="" id='weight_up'/><br&??gt;
<input type='button' value='更新'ヽ(′▽?zhuān)?ノ id='btn_4' onclick="update()"/>

<(′ω`);/body>

<script ty??p(′?_?`)e="text/javasc(′ω`)ript">
/*
var x = $("#queryResult").html();

for(′ω`)(va(′?ω?`)r i=0; i < 20 ; i++) {
x += '<tr&(′▽?zhuān)?)gt;<td></td><td></td><td></td><td></td><td></td><td></t??d></t??r>';
}
$("#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, errorT(′▽?zhuān)?hrown) {

alert(typeof(errorThrown));
}

});
}


function qu(°ロ°) !ery() {

var pno = $("#pno_query").val();
var str = ["編號","姓名","性別","年齡","身高","體重"];
$.ajax({
type : "post",
url : "HelloQuery",
data : {
"pno": pno
},
cache : true,
async : true,
success: function (data ,textSta┐(′?`)┌tus, jqXHR??){
//data = $.parseJSON(data);
var j = 0;
var x = 1;
//for(var i=1; i <20; i++) {
for(var p in data){ //遍歷json對象的每個(gè)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",
ur??l : "HelloDelete",
data : {
"pn(′ω`)o": 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(ty???peof(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();
va??r weight = $("#weight_up")?.val();

var data={

"pno":pn??o,
"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));??
}

});
}



</scrip??t>
</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.Htt(′;ω;`)pSe??rvletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.MysqlUtil;

/**
* Servlet implementation class Hello
*/
@WebServlet("ヽ(′▽?zhuān)?ノ/Hello")
public class Hello extends Ht??tpServlet {
private static final long serial(′?ω?`)VersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
publi??c Hello() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(Ht??tpServletRe??quest request, HttpServletResponse response)
*/
protected void doGet(HttpServletReques(′;д;`)t request, Http??ServletResponse response) throws ServletException, IOExceptiヽ(′▽?zhuān)?ノon {
// TODO Auto-generated method stub
r??esponse.getWriter().append("Served at: ").append(request.getContextPath());
}

/**
* @see HttpServlet#doPost(H??ttpServletR??eques??t request, HttpServletResponse response)
*/
protected void doPost(HttpServletヾ(?■_■)ノRequest requ( ?ヮ?)est, HttpServletResponse response) throws Serv??letException, IOException {
response.setCharacterEncoding("??utf-8");
response.setContentType("application/json; charset=utf-8");

String pno = requ??est.ge??tParameter("??pno");
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String age = requ(?_?;)est.getParameter("age");
String hei(′?`*)ght = request.get?Parameter("height");
String weight = reque??st.getParameter("weight");

St(′▽?zhuān)?)ring sqlInsert='INSERT INTO Per??son (Pno,Pname,Psex,Page,Pheight,Pweight) VALUES('';
sqlInsert += pno +"','";
sqlInsert += name +"','";
sqlInse??rt += sex +"',";
sqlInsert += age +",";
sqlInsert += height +",";??
sqlI??nsert += 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 coヽ(′▽?zhuān)?/m.web;

import java.(′?_?`)io.IOException;
import javax.servlet.ServletException;
im?port javax.servlet.annotation.WebServ(???)let;
import javaxヽ(′▽?zhuān)?ノ.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servl?et.http.HttpServletResponse;

import co(′?ω?`)m.mysql.Mys(°□°)qlUtil;

/**
* Servlet implementation class HelloDelete
*/
@WebServlet("/?HelloDelete")
public class HelloDelete extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public HelloD(╥_╥)elete() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServle(′?`)tRequest requ(°o°)est, HttpServletResp??onse response)
*/
protected voi???d(°□°) doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcep??tion, IOException {
// TO(′?`)DO Auto-generate??d method stub
response.getWriter().append("Served at:?? ").append(request.getContextPath());
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(Http(′?ω?`)ServletRequest request, HttpServle???tResponse response) throws ServletException, IOException {
response.setCha??racterEncoding("utf-8");
response.setContentTy??pe("application/json; charset=utf-8");

String pno = reques(′ω`)t.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\":\"刪除失敗\"}";
}
re??sponse.getWriter().write(r(′ω`*)ep);
}

}

更新的Servlet:HelloUpdate.java

pa??ckage(???) com.web;

import java.io.IOException;
import javaヽ(′▽?zhuān)?ノx.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax??.servlet.ヾ(^-^)ノhttp.HttpServlet;
import javax.ser(/ω\)vlet.http.HttpServletRequest;
import javax.servlet.http.Http(′?_?`)ServletRespoヽ(′ー`)ノnse;

import com.mysql.MysqlUtil;

/**
* Servle(′▽?zhuān)?t implementation class Hel(′?`)loUpdate
*/
@WebServlet("/Hel??loUpdate")
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 voi??d doGet(HttpServletRequ(′▽?zhuān)?)est request, HttpServ(′▽?zhuān)?)letResponse response) throws ServletException??, IOException {
// TODO Auto-generated method(°ロ°) ! stub
response.getWriter().append("Served at: ").append(request.getContextPath((′▽?zhuān)?)));
}

/**
* @see HttpServl??et#???doPost(HttpServletRequest request, HttpServle(???)tResponse?? response)
*/
p(′▽?zhuān)?)rotected void do??Post(HttpServletRequest request??, Http(°o°)ServletResponse response) throws?? ServletException, IOException {
respons(╬?益?)e.setCharacterEncoding("utf-8");
response.setContentType("application/json; charset=utf-8");

St?ring pno = request.getParameter("pno");
Strin??g name = request.getPar??ameter("name");
String sex = request.getParameter("sex");
String age = request.getParameter("age");
String height = request.getParameter("heigh(?_?;)t");
String weight = request.gヽ(′?`)ノetParameter("weight");

String sqlupdate = "update Person set ";
// sqlupdate += "Pno='"+ pno +"',";
sqlupdate += "Pname='"+ name +"',";
sqlupdate += "Psex='"+ sex +"',";
sqlupdate += "Page='+ age +',";
sqlupdate += "Pheight="+ height +",";
sqlupdate += "Pweigh??t="+(′;д;`) weiヾ(′?`)?ght;
sqlupdate += " where(╯°□°)╯︵ ┻━┻ Pno='"+p(????)no+"'";
Sys(′_ゝ`)tem.out.??pri(O_O)ntln(sqlupd(′▽?zhuān)?ate);
int message = My??sqlUtil.update(sqlupdate);
String rep = "";
if(message == 1) {
rep='{ \'code\":\"200\",\"message\":\"成功插入數據庫\"}";
}else {
rep = "{ \"code\":\"999\",\"message\":\"插入失敗了(′?_?`)\"}";
}
resp??onse.getWriter().write(rep);

}

}

查詢(xún)的Servlet:HelloQuery.java

package com.web;

import java.io.IOException(?????);
import java.ut(???)il.ArrayList;
import java.util.Arrays;
impor??t java.util.List;
iヽ(′ー`)ノmport java.util.??Map;

import javax.servlet.ServletException;
import javax.servlet.annotati??on.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.MysqlUt??il;

/**
* Servlet implementation class HelloQuery
*/
@WebServlet("/Hello(╯°□°)╯Query")??
public class HelloQuery extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet(′_ゝ`)#HttpServlet()ヾ(′ω`)?
*/
public HelloQue??ry() {
super();
// TODO Auto-generateヽ(′ー`)ノd con(°o°)structor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
pro( ?ヮ?)tected void doGet(??HttpServletRequest request, HttpServletResponse res??ponse) thヾ(^-^)ノrows ServletException??, IOException {
// TODO Auto-generated methヽ(′▽?zhuān)?ノod stub
response.getWriter().appen(′?_?`)d("Served at: ").append(request.getContextPa???th());
}

/**
* @see HttpServlet#doPost(HttpServ(′?_?`)letRequest request, HttpServletResponse response)
*??/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOE(/ω\)xception {
response.setC(′▽?zhuān)?)haracterEncoding(( ?ヮ?)"utf-8");
respon(′?`)se.setContent??Type("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 dat(′ω`*)a = "{ ";

S(′?_?`)tring[] 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<li(╯°□°)╯stmap.get(i).siz(′?_?`)e();j++) {
data += "\ヾ(′?`)?""+str[j]+"\":"+"\""+listmap.get(i).get(params[j])+"\",";
}
}
data = data.substring(0, data.length??()-1);
data += "}";


System.out.println(data);
response.getWriヽ(′?`)ノter().write(data);
}



}

頁(yè)面如下:

對應的數據庫:

git克隆地址:https://github.com/dreamiboy/JDBCU(′?ω?`)til.git

更多關(guān)于ajax相關(guān)內容感興趣的讀者可查看本站專(zhuān)題:《jquery中Ajax用法總結》、《JavaScript中ajax操作技巧總結》、實(shí)現(′?`*)數據刪改《PHP+ajax技巧與應用小結》及《asp.net ajax技巧總結專(zhuān)題》

希望本文所述對大家ajax程序設計有所幫助。查操

作詳

來(lái)源:腳本之??家

作詳

鏈接:https://www.jb51.net/art(?_?;)i??cle/187854.htm

作詳

搜索

亚洲女同成aV人片在线观看|亚洲www啪成人一区二区麻豆|亚洲国产中日韩精品综合|亚洲国产成人精品一级片|亚洲无码在线视频免费 巴彦县| 浏阳市| 壶关县| 宁夏| 长兴县| 台东市| 北流市| 吉林省| 腾冲县| 江都市| 永靖县| 台州市| 达州市| 客服| 江西省| 万盛区| 本溪| 高平市| 常熟市| 周口市| 长寿区| 泸西县| 江源县| 阳信县| 汝城县| 泸溪县| 吴川市| 十堰市| 普宁市| 成都市| 安陆市| 孝昌县| 安远县| 土默特左旗| 荥经县| 孟州市| 宁波市| 北辰区| 石阡县| 铜鼓县| 湾仔区| http://444 http://444 http://444 http://444 http://444 http://444