JavaWeb实现修改功能

    首先通过点击index.jsp页面的修改按钮,获取该行的id:↓

 

      其次,跳转到updateBooks.jsp页面进行修改信息,页面代码如下:↓

 1 <%@ page import="BookSystem.Other.Books" %><%--
 2   Created by IntelliJ IDEA.
 3   User: NFS
 4   Date: 2019-7-12
 5   Time: 14:31
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <html>
10 <head>
11     <title>修改书籍</title>
12 </head>
13 <body>
14 
15 <%
16     Books books = (Books) request.getAttribute("update");
17 
18     if (books != null) {
19 %>
20 
21 <div>
22     <form method="post" action="update">
23         <label>
24             <span>编号:</span>
25             <input name="book_id" value="<%=books.getId()%>" readonly>
26         </label>
27         <label>
28             <span>书名:</span>
29             <input name="book_name" value="<%=books.getName()%>">
30         </label>
31         <label>
32             <span>作者:</span>
33             <input name="author" value="<%=books.getAuthor()%>">
34         </label>
35         <label>
36             <span>库存:</span>
37             <input name="number" value="<%=books.getNumber()%>">
38         </label>
39         <label>
40             <span>价格:</span>
41             <input name="price" value="<%=books.getPrice()%>">
42         </label>
43         <label>
44             <span>出版社:</span>
45             <input name="pub" value="<%=books.getPub()%>">
46         </label>
47         <input type="submit" value="提交修改信息">
48     </form>
49 </div>
50 
51 <%
52 } else {
53 %>
54 <div>此 ID 没有找到相关的数据,所以不能进行修改。</div>
55 <%
56     }
57 %>
58 
59 
60 <footer>
61     <a href="<%=request.getContextPath()%>/books/lst">返回首页</a>
62 </footer>
63 </body>
64 </html>

 

 

      updateBooks.jsp 所对应的servlet :updateBooks.java, 代码如下:↓

 1 package BookSystem.CRUD;
 2 
 3 import BookSystem.Other.Books;
 4 import BookSystem.Other.DButil;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.annotation.WebServlet;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 import java.io.IOException;
12 import java.sql.*;
13 
14 @WebServlet("/books/update")
15 public class UpdateBooks extends HttpServlet {
16     @Override
17     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
18         //创建Books对象
19         Books books = new Books();
20         //获取对应的id
21         int id = Integer.parseInt(req.getParameter("id"));
22         Connection  connection=null;
23         Statement st = null;
24         ResultSet rs = null;
25         connection=new DButil().getConnection();
26         try {
27             st = connection.createStatement();
28             rs = st.executeQuery("select book_id,book_name ,author,number,price,pub from BookInfo where book_id = "+id);
29             
30             if(rs.next()) {
31                 books =new Books( rs.getInt(1), rs.getString(2), rs.getString(3),rs.getInt(4),rs.getFloat(5),  rs.getString(6));
32 
33             }
34         }catch (SQLException e){
35             e.printStackTrace();
36         }finally {
37             DButil.close(connection,st,rs);
38 
39 
40         }
41         req.setAttribute("update",books);
42         req.getRequestDispatcher("/Book/updateBooks.jsp").forward(req,resp);
43 
44     }
45 
46     @Override
47     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
48        //设置编码格式
49         req.setCharacterEncoding("UTF-8");
50         //获取数据
51         int id=Integer.parseInt(req.getParameter("book_id"));
52         String name=req.getParameter("book_name");
53         String author=req.getParameter("author");
54         Integer number=Integer.parseInt(req.getParameter("number"));
55         Float price=Float.parseFloat(req.getParameter("price"));
56         String pub=req.getParameter("pub");
57         Connection connection=null;
58         PreparedStatement prsmt=null;
59 
60         try {
61             //修改数据
62             connection=new DButil().getConnection();
63             String sql="update BookInfo set book_name =?, author=?, number=?, price=?, pub=? " +
64                     "where book_id=?";
65             prsmt=connection.prepareStatement(sql);
66             prsmt.setString(1,name);
67             prsmt.setString(2,author);
68             prsmt.setInt(3,number);
69             prsmt.setFloat(4,price);
70             prsmt.setString(5,pub);
71             prsmt.setInt(6,id);
72             prsmt.executeUpdate();
73         }catch (SQLException e){
74             e.printStackTrace();
75         }finally {
76             try {
77                 connection.close();
78                 prsmt.close();
79             } catch (SQLException e) {
80                 e.printStackTrace();
81             }
82         }
83         String method = req.getMethod();
84         
85         resp.sendRedirect(req.getContextPath() + "/books/lst");   // 重定向,肯定是 GET 方法
86     }
87 }

 

      注:该整个CRUD不展示效果图,整体CSS应当有属于自己的

————————————————————————————————————————————————————————————

posted @ 2019-09-04 19:51  Winton-H  阅读(8934)  评论(0编辑  收藏  举报