First, by clicking on the index.jsp page of the Modify button, get id of the line: ↓
Then, jump to updateBooks.jsp modification information pages, the page code is as follows: ↓
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 = "submit modification information" > 48 </ form > 49 </ div > 50 51 is <% 52 is } the else { 53 is %> 54 is < div > This ID No data is found, it can not be modified . </ Div > 55 <% 56 is } 57 is %> 58 59 60 < footer > 61 is < A the href = "<% = request.getContextPath ()%> / Books / LST" >> 62 </footer> 63 </body> 64 </html>
updateBooks.jsp corresponding servlet: updateBooks.java, code is as follows: ↓
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"); // redirection, the GET method is certainly 86 } 87 }
Note: This does not show the entire CRUD renderings, overall should have their own CSS
————————————————————————————————————————————————————————————