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

Web的增删改查

准备环境

1. 添加web

  • 点击项目
  • 右键——>选择**添加框架**
  • 选择**web应用程序**

2.创建lib目录

  • 在web应用程序的**WEB-INF目录下**创建lib目录
  • 添加jar包(5个)
  • 解压:右键——>选择**添加库**

3.创建Dao层

  • 在src目录下创建包com.zmq
  • 在该包下创建dao层
  • 添加工具类BaseDao
  • 创建相应的类,继承BaseDao

4. 添加属性文件

  • 在src目录下添加属性文件

5. 创建实体层entity

  • 在com.zmq包下创建entity包
  • 在该包下创建相应的实体类

6. 添加配置

选择tomcat 服务器——>选择本地——>启动服务器

1.登录

UserDao——封装方法

 //登录public User selectByNameAndPwd(String name,String password){User user=null;try {getCon();String sql="select * from user where u_name=?&&password=?";ps=con.prepareStatement(sql);ps.setObject(1,name);ps.setObject(2,password);rs=ps.executeQuery();while(rs.next()){user=new User();user.setId(rs.getInt("id"));user.setName(rs.getString("u_name"));user.setPassword(rs.getString("password"));}} catch (Exception e) {throw new RuntimeException(e);} finally {closeAll();}return user;}

TextUserDao——单元测试

    @Testpublic void testSelectByNAP(){userDao.selectByNameAndPwd("张四","123456");}

login.jsp——登录页面

<body><%//获取error的值String error = request.getParameter("error");//判断,如果error为1,表示输入的密码或用户名有误if("1".equals(error)){out.print("<font color='red'>用户名或密码有误</font>");}%><form action="loginDo.jsp" method="post">账号:<input type="text" name="name"/><br>密码:<input type="password" name="password"/><br><input type="submit" value="登录"/><input type="button" onclick="register()" value="注册"/></form></body>

loginDo.jsp——登录处理页面

<body>
<%//设置编码request.setCharacterEncoding("utf-8");//接收数据String name = request.getParameter("name");String password = request.getParameter("password");//调用UserDao中的方法UserDao userDao=new UserDao();User user = userDao.selectByNameAndPwd(name, password);//依据返回值user是否为空判断输入密码,用户名是否正确if(user!=null){response.sendRedirect("success.jsp");}else {response.sendRedirect("login.jsp?error=1");}
%>
</body>

2. 注册

UserDao——封装方法

//注册public int add(String name,String password){String sql="insert into user values(null,?,?)";return edit(sql,name,password);}

TestUserDao——单元测试

 UserDao userDao=new UserDao();@Testpublic void testAdd(){userDao.add("张数","123456");}

login.jsp——登录页面

<body><%//获取error的值String error = request.getParameter("error");//判断,如果error为1,表示输入的密码或用户名有误if("1".equals(error)){out.print("<font color='red'>用户名或密码有误</font>");}%><form action="success.jsp" method="post">账号:<input type="text" name="name"/><br>密码:<input type="password" name="password"/><br><input type="submit" value="登录"/><input type="button" onclick="register()" value="注册"/></form></body>
<script>function register(){location.href="register.jsp";}
</script>

register.jsp——注册页面

<body>
<form action="registerDo.jsp" method="post">账号:<input type="text" name="name1"/><br>密码:<input type="text" name="password1"/><br><input type="submit" value="注册"/>
</form>

registerDo.jsp——注册处理页面

<body>
<%//设置编码request.setCharacterEncoding("utf-8");//接收数据String name1 = request.getParameter("name1");String password1 = request.getParameter("password1");//调用UserDao中的方法UserDao userDao=new UserDao();int add = userDao.add(name1, password1);if(add>0){response.sendRedirect("login.jsp");}else {response.sendRedirect("register.jsp");}
%>
</body>

3. 查询全部并显示

BooksDao——封装方法

//查询所有图书信息public List<Books> selectAll(){List<Books> list=new ArrayList<>();try {getCon();String sql="select * from books";ps=con.prepareStatement(sql);rs= ps.executeQuery();while (rs.next()){Books books=new Books();books.setId(rs.getInt("book_id"));books.setType(rs.getString("type_id"));books.setName(rs.getString("book_name"));books.setAuthor(rs.getString("author"));books.setPublisher(rs.getString("publisher"));books.setPrice(rs.getInt("price"));list.add(books);}} catch (Exception e) {throw new RuntimeException(e);} finally {closeAll();}return list;}

TestBooksDao——单元测试:输出书本名字

 BooksDao booksDao=new BooksDao();@Testpublic void testSelectAll(){List<Books> list = booksDao.selectAll();for (Books b: list) {System.out.println(b.getName());}}

success.jsp——登录成功后的显示页面

<head><title>Title</title><style>div{color: black;text-align: center;}a{text-decoration: none;}</style>
</head>
<body>
<%BooksDao booksDao=new BooksDao();List<Books> list = booksDao.selectAll();
%>
<div>登录成功页面</div>
<button>添加</button><br>
<table border="1px" align="center" width="70%" cellspacing="0"><tr><th>编&emsp;号</th><th>类&emsp;型</th><th>名&emsp;称</th><th>作&emsp;者</th><th>出版社</th><th>价&emsp;格</th><th>操&emsp;作</th></tr><%for (Books b:list) {%><tr><td><%=b.getId()%></td><td><%=b.getType()%></td><td><%=b.getName()%></td><td><%=b.getAuthor()%></td><td><%=b.getPublisher()%></td><td><%=b.getPrice()%></td><td><a href="#">编辑</a><a href="#">删除</a></td></tr><%}%>
</table></body>

4. 增加

BooksDao——封装方法

//增加public int add(String type,String name,String author,String publisher,Integer price){String sql="insert into books values(null,?,?,?,?,?)";return edit(sql,type,name,author,publisher,price);}

TestBooksDao——单元测试

 @Testpublic void testAdd(){booksDao.add("CMP","活着","余华","清华大学出版社",23);}

success.jsp——登录成功后的显示页面

<button onclick="add()">添加</button><br>
<script>function add(){location.href="add.jsp";}
</script>

add.jsp添加页面

<body>
<form action="addDo.jsp" method="post">类&emsp;型:<input type="text" name="types"/><br>名&emsp;称:<input type="text" name="names"/><br>作&emsp;者:<input type="text" name="authors"/><br>出版社:<input type="text" name="publishers"/><br>价&emsp;格:<input type="number" name="prices"/><br><input type="submit" value="确认添加"/>
</form></body>

addDo.jsp添加处理页面

<body>
<%//设置编码格式request.setCharacterEncoding("utf-8");//接收数据String types = request.getParameter("types");String names = request.getParameter("names");String authors = request.getParameter("authors");String publishers = request.getParameter("publishers");String prices = request.getParameter("prices");//调用BooksDao中的方法BooksDao booksDao=new BooksDao();int add = booksDao.add(types, names, authors, publishers, Integer.parseInt(prices));//判断if(add>0){response.sendRedirect("success.jsp");}else {response.sendRedirect("add.jsp");}%>
</body>

5. 删除

BooksDao——封装方法

//删除public int delete(Integer id){String sql="delete from books where book_id=?";return edit(sql,id);}

TestBooksDao——单元测试

 @Testpublic void testDel(){booksDao.delete(4);}

success.jsp——登录成功后的显示页面

 <a href="delDo.jsp?id=<%=b.getId()%>">删除</a>

deleteDo.jsp删除操作页面

<body>
<%//接收数据String id = request.getParameter("id");//调用BooksDao中的方法BooksDao booksDao=new BooksDao();int delete = booksDao.delete(Integer.parseInt(id));if(delete>0){out.print("<script>alert('删除成功');location.href='success.jsp'</script>");}else{out.print("<script>alert('删除失败');location.href='success.jsp'</script>");}
%>
</body>

6. 修改

6.1数据回显

BooksDao——封装方法

//根据id查询public Books selectById(Integer id){Books books=null;try {getCon();String sql="select * from books where book_id=?";ps=con.prepareStatement(sql);ps.setObject(1,id);rs=ps.executeQuery();while (rs.next()){books=new Books();books.setId(rs.getInt("book_id"));books.setType(rs.getString("type_id"));books.setName(rs.getString("book_name"));books.setAuthor(rs.getString("author"));books.setPublisher(rs.getString("publisher"));books.setPrice(rs.getInt("price"));}} catch (Exception e) {throw new RuntimeException(e);} finally {closeAll();}return books;}

TestBooksDao——单元测试

@Testpublic void testSelectById(){booksDao.selectById(1);}

success.jsp——登录成功后的显示页面

<a href="update.jsp?id=<%=b.getId()%>">编辑</a>

update.jsp修改页面,根据id查询并数据回显

<body><%//接收数据String id = request.getParameter("id");//调用BooksDao中的方法BooksDao booksDao=new BooksDao();Books books = booksDao.selectById(Integer.parseInt(id));%>
<form action="updateDo.jsp" method="post"><input type="hidden" name="id" value="<%=id%>"><br>类&emsp;型:<input type="text" name="type" value="<%=books.getType()%>"/><br>名&emsp;称:<input type="text" name="name" value="<%=books.getName()%>"/><br>作&emsp;者:<input type="text" name="author" value="<%=books.getAuthor()%>"/><br>出版社:<input type="text" name="publisher" value="<%=books.getPublisher()%>"/><br>价&emsp;格:<input type="number" name="price" value="<%=books.getPrice()%>"/><br><input type="submit" value="确认修改"/>
</form>
</body>

6.2确认修改

BooksDao——封装方法

//修改public int update(String type,String name,String author,String publisher,Integer price,Integer id){String sql="update books set type_id=?,book_name=?,author=?,publisher=?,price=? where book_id=?";return edit(sql,type,name,author,publisher,price,id);}

TestBooksDao——单元测试

@Testpublic void testUpdate(){booksDao.update("CMP","活着","余华","天津大学出版社",19,309108);}

updateDo.jsp修改操作页面:修改数据信息并同步到数据库

<body>
<%//设置编码request.setCharacterEncoding("utf-8");//接收数据String id = request.getParameter("id");String type = request.getParameter("type");String name = request.getParameter("name");String author= request.getParameter("author");String publisher= request.getParameter("publisher");String price= request.getParameter("price");//调用BooksDao的方法BooksDao booksDao=new BooksDao();booksDao.update(type,name,author,publisher,Integer.parseInt(price),Integer.parseInt(id));response.sendRedirect("success.jsp");
%>
</body>

7. 测试功能是否正确

测试写的功能有两种方法:

  • 使用主函数
  • 使用junit单元测试

步骤:

  • 加入单元测试的依赖——junit的jar包
  • 创建一个测试类——包含相应的测试方法
 //注意:该方法没有参数,也没有返回值。StudentDao studentDao=new StudentDao();@Testpublic void testFindAll(){List<Student> list = studentDao.findAll();for(Student s:list){System.out.println(s.getName()+"--->"+s.getAge());}}

注意:测试类中的方法没有参数,也没有返回值,要加@Test注解,测试时:选中方法名——>右键选择Run

8.注意

1. 关于时间类型

数据库与Java类型的一一对照:

data——data:表示日期

time——time:表示时间

timestamp——timestamp:表示日期时间毫秒

  • 获取数据:getDate、getTime、getTimestamp
  • 数据库获取现在的时间:now()

2. 关于强转

String类型转换为Integer类型:Integer. parseInt(变量)

3.关于性别

当数据库中使用1、2表示男女时,在前端页面显示可以使用**三目运算符**

s.getGender=="1"?"男":"女";

4.关于超链接传参

?key=value

<a href="delDo.jsp?id=<%=s.getId()%>删除</a>"

5. 关于删除成功后弹出提示信息

if(delete>0){out.print("<script>alert('删除成功');location.href='success.jsp'</script>");}else{out.print("<script>alert('删除失败');location.href='success.jsp'</script>");}

使用script和alert

6.关于修改时的id

因为修改时id不可变,且需要传递id值,所以需要对id进行处理,两种方法

  • 使用隐藏域hidden将id在前端页面隐藏
  • 读取出id并设置为只读模式readonly

7. 单选框、复选框的默认

单选框:checked

复选框:selected

相关文章:

Web的增删改查

准备环境 1. 添加web 点击项目右键——>选择**添加框架**选择**web应用程序** 2.创建lib目录 在web应用程序的**WEB-INF目录下**创建lib目录添加jar包(5个)解压&#xff1a;右键——>选择**添加库** 3.创建Dao层 在src目录下创建包com.zmq在该包下创建dao层添加工具…...

Java 前后端时间格式转换

在 Web 开发里&#xff0c;时间格式处理既常见又关键。由于前端和后端对时间的表示、处理方式存在差异&#xff0c;熟练掌握时间格式的转换方法就显得尤为重要。这篇文章会深入探讨 Java 前后端时间格式转换的相关知识&#xff0c;特别是 Java 时间转换的多种方式&#xff0c;其…...

【用deepseek和chatgpt做算法竞赛】——还得DeepSeek来 -Minimum Cost Trees_5

往期 【用deepseek和chatgpt做算法竞赛】——华为算法精英实战营第十九期-Minimum Cost Trees_0&#xff1a;介绍了题目和背景【用deepseek和chatgpt做算法竞赛】——华为算法精英实战营第十九期-Minimum Cost Trees_1&#xff1a;题目输入的格式说明&#xff0c;选择了邻接表…...

C++ 互斥锁的使用

mutex std::mutex 是C标准库中用于线程同步的互斥锁机制&#xff0c;主要用于保护共享资源&#xff0c;避免多个线程同时访问导致的竞态条件。 它提供了以下功能&#xff1a; 加锁&#xff08;lock&#xff09;&#xff1a;阻塞当前线程&#xff0c;直到获取锁。 解锁&#…...

【Elasticsearch】Retrieve inner hits获取嵌套查询的具体的嵌套文档来源,以及父子文档的来源

Retrieve inner hits 是 Elasticsearch 中的一个功能&#xff0c;用于在嵌套查询或父子查询中&#xff0c;返回导致主文档匹配的具体嵌套对象或子/父文档的详细信息&#xff0c;帮助用户更直观地理解查询结果的来源。 在 Elasticsearch 中&#xff0c;Retrieve inner hits是一…...

C语言中的typedef关键字详解

C语言中的typedef关键字详解 在C语言编程中&#xff0c;typedef 关键字是一个非常实用的特性&#xff0c;它可以帮助我们创建新的类型名&#xff0c;从而简化代码&#xff0c;提高可读性。本文将详细解析typedef的使用方法、场景以及注意事项。 1. typedef简介 typedef 是Ty…...

怎麼利用靜態ISP住宅代理在指紋流覽器中管理社媒帳號?

靜態ISP住宅代理是一種基於真實住宅IP的代理服務。這類代理IP通常由互聯網服務提供商&#xff08;ISP&#xff09;分配&#xff0c;具有非常高的真實性&#xff0c;與普通數據中心代理相比&#xff0c;更不容易被平臺檢測到為“虛假IP”或“代理IP”&#xff0c;靜態ISP住宅代理…...

【多语言生态篇一】【DeepSeek×Java:Spring Boot微服务集成全栈指南 】

(手把手带你从零实现AI能力调用,万字长文预警,建议收藏实操) 一、环境准备:别输在起跑线上 1.1 硬件软件全家桶 JDK版本:必须 ≥17(Spring Boot 3.2+强制要求,低版本直接报错)IDE推荐:IntelliJ IDEA终极版(社区版缺Spring AI插件支持)构建工具:Maven 3.9+ / Grad…...

IOS UITextField 无法隐藏键盘问题

设置UITextField 键盘按钮返回键为“完成”&#xff0c;即return key 设置done .m代码设置代理 //设置代理协议 UITextFieldDelegate&#xff0c; self.mobileTextField.delegate self; ///点击完成键隐藏键盘 - (BOOL)textFieldShouldReturn:(UITextField *)textField{//取…...

einops测试

文章目录 1. einops2. code3. pytorch 1. einops einops 主要是通过爱因斯坦标记法来处理张量矩阵的库&#xff0c;让矩阵处理上非常简单。 conda : conda install conda-forge::einopspython: 2. code import torch import torch.nn as nn import torch.nn.functional as…...

25轻化工程研究生复试面试问题汇总 轻化工程专业知识问题很全! 轻化工程复试全流程攻略 轻化工程考研复试真题汇总

轻化工程复试心里没谱&#xff1f;学姐带你玩转面试准备&#xff01; 是不是总觉得老师会问些刁钻问题&#xff1f;别焦虑&#xff01;其实轻化工程复试套路就那些&#xff0c;看完这篇攻略直接掌握复试通关密码&#xff01;文中有重点面试题可直接背~ 目录 一、这些行为赶紧避…...

小米路由器 AX3000T 降级后无法正常使用,解决办法

问题描述 买了个 AX3000T 路由器&#xff0c;想安装 OpenWRT 或者 安装 Clash 使用&#xff0c;看教程说是需要降级到 v1.0.47 版本。 结果刷机之后路由器无法打开了&#xff0c;一直黄灯亮&#xff0c;中间灭一下&#xff0c;又是黄灯长亮&#xff0c;没有 WIFI 没有连接。以…...

qt5实现表盘的旋转效果,通过提升QLabel类

因为工作需要&#xff0c;需要实现温度的表盘展示效果 实现思路&#xff1a; 通过提示声QLabel控价类&#xff0c;实现报盘的旋转和展示效果 1. 编写一个QLabel的类MyQLabel,实现两个方法 1. void paintEvent(QPaintEvent *event); //重绘函数 2. void valueChanged(int va…...

【HeadFirst系列之HeadFirst设计模式】第7天之命令模式:封装请求,轻松实现解耦!

命令模式&#xff1a;封装请求&#xff0c;轻松实现解耦&#xff01; 大家好&#xff01;今天我们来聊聊设计模式中的命令模式&#xff08;Command Pattern&#xff09;。如果你曾经需要将请求封装成对象&#xff0c;或者希望实现请求的撤销、重做等功能&#xff0c;那么命令模…...

HTTPS(下)

主要讲加密算法RSA&#xff0c;ECDHE TLS的握手涉及四次通信&#xff0c;根据不同的密钥交换算法&#xff0c;TLS 握手流程也会不一样的&#xff0c;现在常用的密钥交换算法有两种&#xff1a;RSA 算法和 ECDHE 算法 正常情况下&#xff0c;需要先TCP三次握手后进行TLS四次握手…...

vue2 和 vue3 中 computer 计算属性的用法

Vue 2 中的 computed 在 Vue 2 中&#xff0c;计算属性是响应式的&#xff0c;并且基于 getter 进行缓存&#xff0c;只有依赖的响应式数据发生变化时才会重新计算。 基本用法 <template><div><p>原始消息&#xff1a;{{ message }}</p><p>反…...

【STM32学习】标准库实现STM32 ADC采集1路、2路、多路

目录 ADC采集 ADC配置步骤 STM32F103C8T6的ADC 输入通道 ​编辑 1路ADC&#xff08;A4 ADC 通道4&#xff09; 1路ADC源码代码链接&#xff1a; 2路ADC&#xff08;A4 ADC 通道4、A5 ADC 通道5&#xff09;基于DMA实现 多路ADC实现采集 ADC采集 ADC配置步骤 使能GPIO…...

【STM32】外部时钟|红外反射光电开关

1.外部时钟 单片机如何对外部触发进行计数&#xff1f;先看一下内部时钟&#xff0c;内部时钟是接在APB1和APB2时钟线上的&#xff0c;APB1,APB2来自stm32单片机内部的脉冲信号&#xff0c;也叫内部时钟。我们用来定时。同样我们可以把外部的信号接入单片机&#xff0c;来对其…...

【语音科学计算器】当前汇率

JSON_MARKER_HORN{“base”:“USD”,“rates”:{“EUR”:0.9758,“JPY”:157.68,“GBP”:0.8190,“CNY”:7.3327,“HKD”:7.7872,“AUD”:1.6260,“CAD”:1.4422,“CHF”:0.9157,“SGD”:1.3714,“KRW”:1473.05,“NZD”:1.7992,“THB”:34.54,“MYR”:4.4930,“PHP”:57.32,“…...

PHP post 数据丢失问题

max_input_vars是PHP配置选项之一&#xff0c;用于设置一个请求中允许的最大输入变量数。它指定了在处理POST请求或者通过URL传递的参数时&#xff0c;PHP脚本能够接收和处理的最大变量数量。 max_input_vars的默认值是1000&#xff0c;意味着一个请求中最多可以包含1000个输入…...

树莓派超全系列教程文档--(62)使用rpicam-app通过网络流式传输视频

使用rpicam-app通过网络流式传输视频 使用 rpicam-app 通过网络流式传输视频UDPTCPRTSPlibavGStreamerRTPlibcamerasrc GStreamer 元素 文章来源&#xff1a; http://raspberry.dns8844.cn/documentation 原文网址 使用 rpicam-app 通过网络流式传输视频 本节介绍来自 rpica…...

汽车生产虚拟实训中的技能提升与生产优化​

在制造业蓬勃发展的大背景下&#xff0c;虚拟教学实训宛如一颗璀璨的新星&#xff0c;正发挥着不可或缺且日益凸显的关键作用&#xff0c;源源不断地为企业的稳健前行与创新发展注入磅礴强大的动力。就以汽车制造企业这一极具代表性的行业主体为例&#xff0c;汽车生产线上各类…...

基础测试工具使用经验

背景 vtune&#xff0c;perf, nsight system等基础测试工具&#xff0c;都是用过的&#xff0c;但是没有记录&#xff0c;都逐渐忘了。所以写这篇博客总结记录一下&#xff0c;只要以后发现新的用法&#xff0c;就记得来编辑补充一下 perf 比较基础的用法&#xff1a; 先改这…...

ffmpeg(四):滤镜命令

FFmpeg 的滤镜命令是用于音视频处理中的强大工具&#xff0c;可以完成剪裁、缩放、加水印、调色、合成、旋转、模糊、叠加字幕等复杂的操作。其核心语法格式一般如下&#xff1a; ffmpeg -i input.mp4 -vf "滤镜参数" output.mp4或者带音频滤镜&#xff1a; ffmpeg…...

令牌桶 滑动窗口->限流 分布式信号量->限并发的原理 lua脚本分析介绍

文章目录 前言限流限制并发的实际理解限流令牌桶代码实现结果分析令牌桶lua的模拟实现原理总结&#xff1a; 滑动窗口代码实现结果分析lua脚本原理解析 限并发分布式信号量代码实现结果分析lua脚本实现原理 双注解去实现限流 并发结果分析&#xff1a; 实际业务去理解体会统一注…...

鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个生活电费的缴纳和查询小程序

一、项目初始化与配置 1. 创建项目 ohpm init harmony/utility-payment-app 2. 配置权限 // module.json5 {"requestPermissions": [{"name": "ohos.permission.INTERNET"},{"name": "ohos.permission.GET_NETWORK_INFO"…...

ardupilot 开发环境eclipse 中import 缺少C++

目录 文章目录 目录摘要1.修复过程摘要 本节主要解决ardupilot 开发环境eclipse 中import 缺少C++,无法导入ardupilot代码,会引起查看不方便的问题。如下图所示 1.修复过程 0.安装ubuntu 软件中自带的eclipse 1.打开eclipse—Help—install new software 2.在 Work with中…...

【碎碎念】宝可梦 Mesh GO : 基于MESH网络的口袋妖怪 宝可梦GO游戏自组网系统

目录 游戏说明《宝可梦 Mesh GO》 —— 局域宝可梦探索Pokmon GO 类游戏核心理念应用场景Mesh 特性 宝可梦玩法融合设计游戏构想要素1. 地图探索&#xff08;基于物理空间 广播范围&#xff09;2. 野生宝可梦生成与广播3. 对战系统4. 道具与通信5. 延伸玩法 安全性设计 技术选…...

JavaScript基础-API 和 Web API

在学习JavaScript的过程中&#xff0c;理解API&#xff08;应用程序接口&#xff09;和Web API的概念及其应用是非常重要的。这些工具极大地扩展了JavaScript的功能&#xff0c;使得开发者能够创建出功能丰富、交互性强的Web应用程序。本文将深入探讨JavaScript中的API与Web AP…...

若依登录用户名和密码加密

/*** 获取公钥&#xff1a;前端用来密码加密* return*/GetMapping("/getPublicKey")public RSAUtil.RSAKeyPair getPublicKey() {return RSAUtil.rsaKeyPair();}新建RSAUti.Java package com.ruoyi.common.utils;import org.apache.commons.codec.binary.Base64; im…...