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

浏览器访问 AWS ECS 上部署的 Docker 容器(监听 80 端口)

✅ 一、ECS 服务配置 Dockerfile 确保监听 80 端口 EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]或 EXPOSE 80 CMD ["python3", "-m", "http.server", "80"]任务定义&#xff08;Task Definition&…...

零门槛NAS搭建:WinNAS如何让普通电脑秒变私有云?

一、核心优势&#xff1a;专为Windows用户设计的极简NAS WinNAS由深圳耘想存储科技开发&#xff0c;是一款收费低廉但功能全面的Windows NAS工具&#xff0c;主打“无学习成本部署” 。与其他NAS软件相比&#xff0c;其优势在于&#xff1a; 无需硬件改造&#xff1a;将任意W…...

【Oracle APEX开发小技巧12】

有如下需求&#xff1a; 有一个问题反馈页面&#xff0c;要实现在apex页面展示能直观看到反馈时间超过7天未处理的数据&#xff0c;方便管理员及时处理反馈。 我的方法&#xff1a;直接将逻辑写在SQL中&#xff0c;这样可以直接在页面展示 完整代码&#xff1a; SELECTSF.FE…...

Vue2 第一节_Vue2上手_插值表达式{{}}_访问数据和修改数据_Vue开发者工具

文章目录 1.Vue2上手-如何创建一个Vue实例,进行初始化渲染2. 插值表达式{{}}3. 访问数据和修改数据4. vue响应式5. Vue开发者工具--方便调试 1.Vue2上手-如何创建一个Vue实例,进行初始化渲染 准备容器引包创建Vue实例 new Vue()指定配置项 ->渲染数据 准备一个容器,例如: …...

P3 QT项目----记事本(3.8)

3.8 记事本项目总结 项目源码 1.main.cpp #include "widget.h" #include <QApplication> int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); } 2.widget.cpp #include "widget.h" #include &q…...

Rust 异步编程

Rust 异步编程 引言 Rust 是一种系统编程语言,以其高性能、安全性以及零成本抽象而著称。在多核处理器成为主流的今天,异步编程成为了一种提高应用性能、优化资源利用的有效手段。本文将深入探讨 Rust 异步编程的核心概念、常用库以及最佳实践。 异步编程基础 什么是异步…...

大语言模型(LLM)中的KV缓存压缩与动态稀疏注意力机制设计

随着大语言模型&#xff08;LLM&#xff09;参数规模的增长&#xff0c;推理阶段的内存占用和计算复杂度成为核心挑战。传统注意力机制的计算复杂度随序列长度呈二次方增长&#xff0c;而KV缓存的内存消耗可能高达数十GB&#xff08;例如Llama2-7B处理100K token时需50GB内存&a…...

重启Eureka集群中的节点,对已经注册的服务有什么影响

先看答案&#xff0c;如果正确地操作&#xff0c;重启Eureka集群中的节点&#xff0c;对已经注册的服务影响非常小&#xff0c;甚至可以做到无感知。 但如果操作不当&#xff0c;可能会引发短暂的服务发现问题。 下面我们从Eureka的核心工作原理来详细分析这个问题。 Eureka的…...

NXP S32K146 T-Box 携手 SD NAND(贴片式TF卡):驱动汽车智能革新的黄金组合

在汽车智能化的汹涌浪潮中&#xff0c;车辆不再仅仅是传统的交通工具&#xff0c;而是逐步演变为高度智能的移动终端。这一转变的核心支撑&#xff0c;来自于车内关键技术的深度融合与协同创新。车载远程信息处理盒&#xff08;T-Box&#xff09;方案&#xff1a;NXP S32K146 与…...

Python 实现 Web 静态服务器(HTTP 协议)

目录 一、在本地启动 HTTP 服务器1. Windows 下安装 node.js1&#xff09;下载安装包2&#xff09;配置环境变量3&#xff09;安装镜像4&#xff09;node.js 的常用命令 2. 安装 http-server 服务3. 使用 http-server 开启服务1&#xff09;使用 http-server2&#xff09;详解 …...