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>编 号</th><th>类 型</th><th>名 称</th><th>作 者</th><th>出版社</th><th>价 格</th><th>操 作</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">类 型:<input type="text" name="types"/><br>名 称:<input type="text" name="names"/><br>作 者:<input type="text" name="authors"/><br>出版社:<input type="text" name="publishers"/><br>价 格:<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>类 型:<input type="text" name="type" value="<%=books.getType()%>"/><br>名 称:<input type="text" name="name" value="<%=books.getName()%>"/><br>作 者:<input type="text" name="author" value="<%=books.getAuthor()%>"/><br>出版社:<input type="text" name="publisher" value="<%=books.getPublisher()%>"/><br>价 格:<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个)解压:右键——>选择**添加库** 3.创建Dao层 在src目录下创建包com.zmq在该包下创建dao层添加工具…...
Java 前后端时间格式转换
在 Web 开发里,时间格式处理既常见又关键。由于前端和后端对时间的表示、处理方式存在差异,熟练掌握时间格式的转换方法就显得尤为重要。这篇文章会深入探讨 Java 前后端时间格式转换的相关知识,特别是 Java 时间转换的多种方式,其…...
【用deepseek和chatgpt做算法竞赛】——还得DeepSeek来 -Minimum Cost Trees_5
往期 【用deepseek和chatgpt做算法竞赛】——华为算法精英实战营第十九期-Minimum Cost Trees_0:介绍了题目和背景【用deepseek和chatgpt做算法竞赛】——华为算法精英实战营第十九期-Minimum Cost Trees_1:题目输入的格式说明,选择了邻接表…...
C++ 互斥锁的使用
mutex std::mutex 是C标准库中用于线程同步的互斥锁机制,主要用于保护共享资源,避免多个线程同时访问导致的竞态条件。 它提供了以下功能: 加锁(lock):阻塞当前线程,直到获取锁。 解锁&#…...
【Elasticsearch】Retrieve inner hits获取嵌套查询的具体的嵌套文档来源,以及父子文档的来源
Retrieve inner hits 是 Elasticsearch 中的一个功能,用于在嵌套查询或父子查询中,返回导致主文档匹配的具体嵌套对象或子/父文档的详细信息,帮助用户更直观地理解查询结果的来源。 在 Elasticsearch 中,Retrieve inner hits是一…...
C语言中的typedef关键字详解
C语言中的typedef关键字详解 在C语言编程中,typedef 关键字是一个非常实用的特性,它可以帮助我们创建新的类型名,从而简化代码,提高可读性。本文将详细解析typedef的使用方法、场景以及注意事项。 1. typedef简介 typedef 是Ty…...
怎麼利用靜態ISP住宅代理在指紋流覽器中管理社媒帳號?
靜態ISP住宅代理是一種基於真實住宅IP的代理服務。這類代理IP通常由互聯網服務提供商(ISP)分配,具有非常高的真實性,與普通數據中心代理相比,更不容易被平臺檢測到為“虛假IP”或“代理IP”,靜態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 键盘按钮返回键为“完成”,即return key 设置done .m代码设置代理 //设置代理协议 UITextFieldDelegate, self.mobileTextField.delegate self; ///点击完成键隐藏键盘 - (BOOL)textFieldShouldReturn:(UITextField *)textField{//取…...
einops测试
文章目录 1. einops2. code3. pytorch 1. einops einops 主要是通过爱因斯坦标记法来处理张量矩阵的库,让矩阵处理上非常简单。 conda : conda install conda-forge::einopspython: 2. code import torch import torch.nn as nn import torch.nn.functional as…...
25轻化工程研究生复试面试问题汇总 轻化工程专业知识问题很全! 轻化工程复试全流程攻略 轻化工程考研复试真题汇总
轻化工程复试心里没谱?学姐带你玩转面试准备! 是不是总觉得老师会问些刁钻问题?别焦虑!其实轻化工程复试套路就那些,看完这篇攻略直接掌握复试通关密码!文中有重点面试题可直接背~ 目录 一、这些行为赶紧避…...
小米路由器 AX3000T 降级后无法正常使用,解决办法
问题描述 买了个 AX3000T 路由器,想安装 OpenWRT 或者 安装 Clash 使用,看教程说是需要降级到 v1.0.47 版本。 结果刷机之后路由器无法打开了,一直黄灯亮,中间灭一下,又是黄灯长亮,没有 WIFI 没有连接。以…...
qt5实现表盘的旋转效果,通过提升QLabel类
因为工作需要,需要实现温度的表盘展示效果 实现思路: 通过提示声QLabel控价类,实现报盘的旋转和展示效果 1. 编写一个QLabel的类MyQLabel,实现两个方法 1. void paintEvent(QPaintEvent *event); //重绘函数 2. void valueChanged(int va…...
【HeadFirst系列之HeadFirst设计模式】第7天之命令模式:封装请求,轻松实现解耦!
命令模式:封装请求,轻松实现解耦! 大家好!今天我们来聊聊设计模式中的命令模式(Command Pattern)。如果你曾经需要将请求封装成对象,或者希望实现请求的撤销、重做等功能,那么命令模…...
HTTPS(下)
主要讲加密算法RSA,ECDHE TLS的握手涉及四次通信,根据不同的密钥交换算法,TLS 握手流程也会不一样的,现在常用的密钥交换算法有两种:RSA 算法和 ECDHE 算法 正常情况下,需要先TCP三次握手后进行TLS四次握手…...
vue2 和 vue3 中 computer 计算属性的用法
Vue 2 中的 computed 在 Vue 2 中,计算属性是响应式的,并且基于 getter 进行缓存,只有依赖的响应式数据发生变化时才会重新计算。 基本用法 <template><div><p>原始消息:{{ message }}</p><p>反…...
【STM32学习】标准库实现STM32 ADC采集1路、2路、多路
目录 ADC采集 ADC配置步骤 STM32F103C8T6的ADC 输入通道 编辑 1路ADC(A4 ADC 通道4) 1路ADC源码代码链接: 2路ADC(A4 ADC 通道4、A5 ADC 通道5)基于DMA实现 多路ADC实现采集 ADC采集 ADC配置步骤 使能GPIO…...
【STM32】外部时钟|红外反射光电开关
1.外部时钟 单片机如何对外部触发进行计数?先看一下内部时钟,内部时钟是接在APB1和APB2时钟线上的,APB1,APB2来自stm32单片机内部的脉冲信号,也叫内部时钟。我们用来定时。同样我们可以把外部的信号接入单片机,来对其…...
【语音科学计算器】当前汇率
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配置选项之一,用于设置一个请求中允许的最大输入变量数。它指定了在处理POST请求或者通过URL传递的参数时,PHP脚本能够接收和处理的最大变量数量。 max_input_vars的默认值是1000,意味着一个请求中最多可以包含1000个输入…...
Ubuntu系统下交叉编译openssl
一、参考资料 OpenSSL&&libcurl库的交叉编译 - hesetone - 博客园 二、准备工作 1. 编译环境 宿主机:Ubuntu 20.04.6 LTSHost:ARM32位交叉编译器:arm-linux-gnueabihf-gcc-11.1.0 2. 设置交叉编译工具链 在交叉编译之前&#x…...
docker详细操作--未完待续
docker介绍 docker官网: Docker:加速容器应用程序开发 harbor官网:Harbor - Harbor 中文 使用docker加速器: Docker镜像极速下载服务 - 毫秒镜像 是什么 Docker 是一种开源的容器化平台,用于将应用程序及其依赖项(如库、运行时环…...
苍穹外卖--缓存菜品
1.问题说明 用户端小程序展示的菜品数据都是通过查询数据库获得,如果用户端访问量比较大,数据库访问压力随之增大 2.实现思路 通过Redis来缓存菜品数据,减少数据库查询操作。 缓存逻辑分析: ①每个分类下的菜品保持一份缓存数据…...
浅谈不同二分算法的查找情况
二分算法原理比较简单,但是实际的算法模板却有很多,这一切都源于二分查找问题中的复杂情况和二分算法的边界处理,以下是博主对一些二分算法查找的情况分析。 需要说明的是,以下二分算法都是基于有序序列为升序有序的情况…...
【开发技术】.Net使用FFmpeg视频特定帧上绘制内容
目录 一、目的 二、解决方案 2.1 什么是FFmpeg 2.2 FFmpeg主要功能 2.3 使用Xabe.FFmpeg调用FFmpeg功能 2.4 使用 FFmpeg 的 drawbox 滤镜来绘制 ROI 三、总结 一、目的 当前市场上有很多目标检测智能识别的相关算法,当前调用一个医疗行业的AI识别算法后返回…...
python执行测试用例,allure报乱码且未成功生成报告
allure执行测试用例时显示乱码:‘allure’ �����ڲ����ⲿ���Ҳ���ǿ�&am…...
C++使用 new 来创建动态数组
问题: 不能使用变量定义数组大小 原因: 这是因为数组在内存中是连续存储的,编译器需要在编译阶段就确定数组的大小,以便正确地分配内存空间。如果允许使用变量来定义数组的大小,那么编译器就无法在编译时确定数组的大…...
return this;返回的是谁
一个审批系统的示例来演示责任链模式的实现。假设公司需要处理不同金额的采购申请,不同级别的经理有不同的审批权限: // 抽象处理者:审批者 abstract class Approver {protected Approver successor; // 下一个处理者// 设置下一个处理者pub…...
处理vxe-table 表尾数据是单独一个接口,表格tableData数据更新后,需要点击两下,表尾才是正确的
修改bug思路: 分别把 tabledata 和 表尾相关数据 console.log() 发现 更新数据先后顺序不对 settimeout延迟查询表格接口 ——测试可行 升级↑:async await 等接口返回后再开始下一个接口查询 ________________________________________________________…...
华为OD机考-机房布局
import java.util.*;public class DemoTest5 {public static void main(String[] args) {Scanner in new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextLine()) { // 注意 while 处理多个 caseSystem.out.println(solve(in.nextLine()));}}priv…...
