当前位置: 首页 > 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个输入…...

[特殊字符] 智能合约中的数据是如何在区块链中保持一致的?

&#x1f9e0; 智能合约中的数据是如何在区块链中保持一致的&#xff1f; 为什么所有区块链节点都能得出相同结果&#xff1f;合约调用这么复杂&#xff0c;状态真能保持一致吗&#xff1f;本篇带你从底层视角理解“状态一致性”的真相。 一、智能合约的数据存储在哪里&#xf…...

C++_核心编程_多态案例二-制作饮品

#include <iostream> #include <string> using namespace std;/*制作饮品的大致流程为&#xff1a;煮水 - 冲泡 - 倒入杯中 - 加入辅料 利用多态技术实现本案例&#xff0c;提供抽象制作饮品基类&#xff0c;提供子类制作咖啡和茶叶*//*基类*/ class AbstractDr…...

Linux 文件类型,目录与路径,文件与目录管理

文件类型 后面的字符表示文件类型标志 普通文件&#xff1a;-&#xff08;纯文本文件&#xff0c;二进制文件&#xff0c;数据格式文件&#xff09; 如文本文件、图片、程序文件等。 目录文件&#xff1a;d&#xff08;directory&#xff09; 用来存放其他文件或子目录。 设备…...

3.3.1_1 检错编码(奇偶校验码)

从这节课开始&#xff0c;我们会探讨数据链路层的差错控制功能&#xff0c;差错控制功能的主要目标是要发现并且解决一个帧内部的位错误&#xff0c;我们需要使用特殊的编码技术去发现帧内部的位错误&#xff0c;当我们发现位错误之后&#xff0c;通常来说有两种解决方案。第一…...

centos 7 部署awstats 网站访问检测

一、基础环境准备&#xff08;两种安装方式都要做&#xff09; bash # 安装必要依赖 yum install -y httpd perl mod_perl perl-Time-HiRes perl-DateTime systemctl enable httpd # 设置 Apache 开机自启 systemctl start httpd # 启动 Apache二、安装 AWStats&#xff0…...

线程与协程

1. 线程与协程 1.1. “函数调用级别”的切换、上下文切换 1. 函数调用级别的切换 “函数调用级别的切换”是指&#xff1a;像函数调用/返回一样轻量地完成任务切换。 举例说明&#xff1a; 当你在程序中写一个函数调用&#xff1a; funcA() 然后 funcA 执行完后返回&…...

将对透视变换后的图像使用Otsu进行阈值化,来分离黑色和白色像素。这句话中的Otsu是什么意思?

Otsu 是一种自动阈值化方法&#xff0c;用于将图像分割为前景和背景。它通过最小化图像的类内方差或等价地最大化类间方差来选择最佳阈值。这种方法特别适用于图像的二值化处理&#xff0c;能够自动确定一个阈值&#xff0c;将图像中的像素分为黑色和白色两类。 Otsu 方法的原…...

智能分布式爬虫的数据处理流水线优化:基于深度强化学习的数据质量控制

在数字化浪潮席卷全球的今天&#xff0c;数据已成为企业和研究机构的核心资产。智能分布式爬虫作为高效的数据采集工具&#xff0c;在大规模数据获取中发挥着关键作用。然而&#xff0c;传统的数据处理流水线在面对复杂多变的网络环境和海量异构数据时&#xff0c;常出现数据质…...

Xen Server服务器释放磁盘空间

disk.sh #!/bin/bashcd /run/sr-mount/e54f0646-ae11-0457-b64f-eba4673b824c # 全部虚拟机物理磁盘文件存储 a$(ls -l | awk {print $NF} | cut -d. -f1) # 使用中的虚拟机物理磁盘文件 b$(xe vm-disk-list --multiple | grep uuid | awk {print $NF})printf "%s\n"…...

SQL慢可能是触发了ring buffer

简介 最近在进行 postgresql 性能排查的时候,发现 PG 在某一个时间并行执行的 SQL 变得特别慢。最后通过监控监观察到并行发起得时间 buffers_alloc 就急速上升,且低水位伴随在整个慢 SQL,一直是 buferIO 的等待事件,此时也没有其他会话的争抢。SQL 虽然不是高效 SQL ,但…...