当前位置: 首页 > news >正文

java ajax增删改查_使用AJAX实现数据的增删改查

主页:index.html

编号:

姓名:

性别:男:女:

年龄:15

16

17

18

19

20

21

22

23

24

25

身高:

体重:

编号:

编号

姓名

性别

年龄

身高

体重

编号:

编号:

姓名:

性别:男:女:

年龄:15

16

17

18

19

20

21

22

23

24

25

身高:

体重:

增加的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\":\"成功插入数据库\"}";

}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\":\"成功插入数据库\"}";

}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> listmap = new ArrayList<>();

listmap = MysqlUtil.show(sql, params);

for(int i =0 ; i

页面如下:

1a6252848a21c5ea1f8296aba0c521ea.png

对应的数据库:

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

相关文章:

  • java去掉结尾的空格_如何从Java中显示的字符串末尾删除空格?
  • java最大回文字符串长度_Leet Code 5 最长回文子串 - Java
  • java泡沫_Java初认识--函数和数组
  • java虚拟机内存溢出的三个原因_JVM发生内存溢出的原因分析及解决方案
  • mysql更新多个字段php_PHP:如果语句无意中导致多个MySQL列更新?
  • properties java jar_propertiesutil jar包
  • python段落注释的语法格式是_Python 基础语法
  • python读取xml配置_python解析xml配置文件
  • java 接口数据类型_Java中的基本数据类型与引用数据类型
  • java 红包接口开发_java调用微信现金红包接口的心得与体会总结
  • java项目中学到了什么_我们能从Java的HelloWorld中学到什么?
  • js java md5加密_MD5加密 (java、js)
  • junit mysql_使用Junit单元测试及操作MySQL数据库时出现错误及解决方法
  • java最简单的算术程序_java – ANTLR4访问者模式简单的算术示例
  • java版我的世界有溺尸_我的世界溺尸怎么找
  • angular组件开发
  • conda常用的命令
  • js学习笔记
  • Linux链接文件
  • PAT A1092
  • python_bomb----数据类型总结
  • SQLServer之创建数据库快照
  • 构建二叉树进行数值数组的去重及优化
  • 关于List、List?、ListObject的区别
  • 力扣(LeetCode)357
  • 实现菜单下拉伸展折叠效果demo
  • 想使用 MongoDB ,你应该了解这8个方面!
  • 协程
  • HanLP分词命名实体提取详解
  • ​HTTP与HTTPS:网络通信的安全卫士
  • ​ssh-keyscan命令--Linux命令应用大词典729个命令解读
  • #### go map 底层结构 ####
  • ###项目技术发展史
  • #基础#使用Jupyter进行Notebook的转换 .ipynb文件导出为.md文件
  • #微信小程序:微信小程序常见的配置传值
  • #我与Java虚拟机的故事#连载04:一本让自己没面子的书
  • $con= MySQL有关填空题_2015年计算机二级考试《MySQL》提高练习题(10)
  • (初研) Sentence-embedding fine-tune notebook
  • (多级缓存)缓存同步
  • (算法二)滑动窗口
  • (转)使用VMware vSphere标准交换机设置网络连接
  • (转)原始图像数据和PDF中的图像数据
  • (转)总结使用Unity 3D优化游戏运行性能的经验
  • .class文件转换.java_从一个class文件深入理解Java字节码结构
  • .net Application的目录
  • .net core webapi 大文件上传到wwwroot文件夹
  • .NET Core 通过 Ef Core 操作 Mysql
  • .net framework4与其client profile版本的区别
  • .NET 中选择合适的文件打开模式(CreateNew, Create, Open, OpenOrCreate, Truncate, Append)
  • .Net6使用WebSocket与前端进行通信
  • .NET设计模式(2):单件模式(Singleton Pattern)
  • .Net下的签名与混淆
  • .Net转Java自学之路—SpringMVC框架篇六(异常处理)
  • .stream().map与.stream().flatMap的使用
  • @ConditionalOnProperty注解使用说明