JAVAWEB实现增删查改(图书信息管理)之修改功能实现
首先通过点击index.jsp页面的修改按钮,获取该行的id:↓

其次,跳转到updateBooks.jsp页面进行修改信息,页面代码如下:↓
<%@ page import="BookSystem.Other.Books" %><%--
Created by IntelliJ IDEA.
User: NFS
Date: 2019-7-12
Time: 14:31
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改书籍</title>
</head>
<body> <%
Books books = (Books) request.getAttribute("update"); if (books != null) {
%> <div>
<form method="post" action="update">
<label>
<span>编号:</span>
<input name="book_id" value="<%=books.getId()%>" readonly>
</label>
<label>
<span>书名:</span>
<input name="book_name" value="<%=books.getName()%>">
</label>
<label>
<span>作者:</span>
<input name="author" value="<%=books.getAuthor()%>">
</label>
<label>
<span>库存:</span>
<input name="number" value="<%=books.getNumber()%>">
</label>
<label>
<span>价格:</span>
<input name="price" value="<%=books.getPrice()%>">
</label>
<label>
<span>出版社:</span>
<input name="pub" value="<%=books.getPub()%>">
</label>
<input type="submit" value="提交修改信息">
</form>
</div> <%
} else {
%>
<div>此 ID 没有找到相关的数据,所以不能进行修改。</div>
<%
}
%> <footer>
<a href="<%=request.getContextPath()%>/books/lst">返回首页</a>
</footer>
</body>
</html>
updateBooks.jsp 所对应的servlet :updateBooks.java, 代码如下:↓
package BookSystem.CRUD; import BookSystem.Other.Books;
import BookSystem.Other.DButil; 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 java.io.IOException;
import java.sql.*; @WebServlet("/books/update")
public class UpdateBooks extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//创建Books对象
Books books = new Books();
//获取对应的id
int id = Integer.parseInt(req.getParameter("id"));
Connection connection=null;
Statement st = null;
ResultSet rs = null;
connection=new DButil().getConnection();
try {
st = connection.createStatement();
rs = st.executeQuery("select book_id,book_name ,author,number,price,pub from BookInfo where book_id = "+id); if(rs.next()) {
books =new Books( rs.getInt(1), rs.getString(2), rs.getString(3),rs.getInt(4),rs.getFloat(5), rs.getString(6)); }
}catch (SQLException e){
e.printStackTrace();
}finally {
DButil.close(connection,st,rs); }
req.setAttribute("update",books);
req.getRequestDispatcher("/Book/updateBooks.jsp").forward(req,resp); } @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码格式
req.setCharacterEncoding("UTF-8");
//获取数据
int id=Integer.parseInt(req.getParameter("book_id"));
String name=req.getParameter("book_name");
String author=req.getParameter("author");
Integer number=Integer.parseInt(req.getParameter("number"));
Float price=Float.parseFloat(req.getParameter("price"));
String pub=req.getParameter("pub");
Connection connection=null;
PreparedStatement prsmt=null; try {
//修改数据
connection=new DButil().getConnection();
String sql="update BookInfo set book_name =?, author=?, number=?, price=?, pub=? " +
"where book_id=?";
prsmt=connection.prepareStatement(sql);
prsmt.setString(1,name);
prsmt.setString(2,author);
prsmt.setInt(3,number);
prsmt.setFloat(4,price);
prsmt.setString(5,pub);
prsmt.setInt(6,id);
prsmt.executeUpdate();
}catch (SQLException e){
e.printStackTrace();
}finally {
try {
connection.close();
prsmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
String method = req.getMethod(); resp.sendRedirect(req.getContextPath() + "/books/lst"); // 重定向,肯定是 GET 方法
}
}
注:该整个CRUD不展示效果图,整体CSS应当有属于自己的
————————————————————————————————————————————————————————————
JAVAWEB实现增删查改(图书信息管理)之修改功能实现的更多相关文章
- JAVAWEB实现增删查改(图书信息管理)之添加功能实现
addBooks.jsp页面代码:↓ <%-- Created by IntelliJ IDEA. User: NFS Date: 2019-7-12 Time: 14:30 To change ...
- JavaWeb实现增删查改(图书信息管理)之删除功能实现
—————————————————————————————————————————————————————————— 删除按钮对应的servlet -->DeleteBooks.java ↓ ...
- JavaWeb实现增删查改(图书信息管理)——之查询
关于此次CRUD所需要的jar包,本人把文件放在了百度网盘,需要的自行去下载: 链接:https://pan.baidu.com/s/1Pqe88u6aPaeVjjOq1YFQ-w 提取码:pim ...
- JDBC课程4--使用PreparedStatement进行增删查改--封装进JDBCTools的功能中;模拟SQL注入 ; sql的date()传入参数值格式!
主要内容: /*SQL 的date()需要传入参数值: preparedStatement().setDate(new java.util.Date().getTime()); 熟悉了使用Prepar ...
- Django笔记&教程 5-1 基础增删查改
Django 自学笔记兼学习教程第5章第1节--基础增删查改 点击查看教程总目录 第四章介绍了模型类models.Model和创建模型,相当于介绍了数据库表和如何创建数据库表. 这一章将介绍如何使用模 ...
- java实现简单的数据库的增删查改,并布局交互界面
一.系统简介 1.1.简介 本系统提供了学生信息管理中常见的基本功能,主要包括管理员.管理员的主要功能有对学生信息进行增加.删除.修改.查找等操作,对信息进行管理,对信息进行修改.查找等操作 ...
- 在Eclipse上实现简单的JDBC增删查改操作
在Javaweb的学习里,学到了如何完成简单的增删查改操作,在这里撰写一篇文章以便自己整理回忆. 首先要建立一些包和导入一些文件.建一些类.具体框架如图 编写Product类 public clas ...
- day08 外键字段的增删查改
day08 外键字段的增删查改 今日内容概要 外键字段的增删查改 正反向查询的概念 基于对象的跨表查询(子查询) 基于双下划线的跨表查询(连表操作) 聚合查询与分组查询 F查询和Q查询 前提准备 cl ...
- 6.在MVC中使用泛型仓储模式和依赖注入实现增删查改
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...
随机推荐
- 11、 Hadoop 2.x各个服务组件如何配置在那台服务器运行并测试
HDFS模块 NameNode:是由哪个文件中的哪个配置属性指定的呢? core-site.xml文件中: <property> <name>fs.defaultFS</ ...
- LeetCode 1140. Stone Game II
原题链接在这里:https://leetcode.com/problems/stone-game-ii/ 题目: Alex and Lee continue their games with pile ...
- 如何把上传图片时候的文件对象转换为图片的url !
getObjectURL(file) { var url = null; if (window.createObjectURL != undefined) { url = window.createO ...
- Python 05 Geany的基本使用1
问题01:代码中包含中文编译时提示错误 原文:https://blog.csdn.net/weixin_43345286/article/details/82951698 解决:文档 - 设置文件编码 ...
- ESP8266低功耗解决的其中一个问题(芯片发热,影响旁边的温湿度芯片)
这个项目的这个问题困扰了自己好长时间了,ESP8266芯片发热,导致了旁边的温湿度传感器采集不了空气中的温度....采集的温度是芯片发热的温度 一直采集出来的是30多度......尽管空气温度10几度 ...
- 洛谷P2730 [IOI]魔板 Magic Squares
题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都有一种颜色.这8种颜 ...
- 计蒜客 38228. Max answer-线段树维护单调栈(The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer 南昌邀请赛网络赛) 2019ICPC南昌邀请赛网络赛
Max answer Alice has a magic array. She suggests that the value of a interval is equal to the sum of ...
- 微信小程序组件化开发框架WePY
wepy-CLI 安装 npm install -g wepy-cli wepy init standard my-project https://github.com/Tencent/wepy 特性 ...
- 廖雪峰Python笔记
△命令行模式和Python交互模式 在Windows开始菜单选择“命令提示符”,就进入到命令行模式,它的提示符类似C:\>:在命令行模式下敲命令python,就看到类似如下的一堆文本输出,然后就 ...
- linux(deepin) 下隐藏firefox标题栏
1. 右上角菜单 -> 定制 -> 左下角 "标题栏" 取消打钩 2. 如果上面无法解决,在firefox的启动前插入一个环境变量,具体修改 /usr/share/ap ...