2020.11.29收获
修改图书信息
updatebook.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: red;">图书信息列表</h1> <a href="guanliyuan.jsp">返回主页</a> <table class="tb"> <tr> <td>id</td> <td>唯一编号</td> <td>书名</td> <td>作者名</td> <td>出版社名</td> <td>可借阅数量</td> <td align="center" colspan="2">操作</td> </tr> <c:forEach items="${book}" var="item"> <tr> <td>${item.id}</td> <td>${item.number}</td> <td>${item.bookname}</td> <td>${item.writername}</td> <td>${item.publishname}</td> <td>${item.shuliang}</td> <td><a href="Servlet?method=getBookById&id=${item.id}">修改</a></td> </tr> </c:forEach> </table> </div> </body> </html>
updatebook2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>菜单</title> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: red;">信息修改</h1> <a href="guanliyuan.jsp">返回主页</a> <form action="Servlet?method=updateBook" method="post" onsubmit="return check()"> <div class="a"> 唯一编号<input type="text" id="number" name="number" value="${book.number}"/> </div> <div class="a"> 书名<input type="text" id="bookname" name="bookname" value="${book.bookname}"/> </div> <div class="a"> 作者名<input type="text" id="writername" name="writername" value="${book.writername}"/> </div> <div class="a"> 出版社名称<input type="text" id="publishname" name="publishname" value="${book.publishname}"/> </div> <div class="a"> 可借阅数量<input type="text" id="shuliang" name="shuliang" value="${book.shuliang}"/> </div> <div class="a"> <button type="submit" class="b"><input type="hidden" id="id" name="id" value="${book.id}"/>修 改</button> </div> </form> </div> </body> </html>
servlet.java
private void getbyname2(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String bookname = req.getParameter("bookname"); Book book = dao.getbyname(bookname); if(book==null) { req.setAttribute("message", "未找到该书籍"); req.getRequestDispatcher("updatebook.jsp").forward(req, resp); } else { req.setAttribute("book", book); req.getRequestDispatcher("updatebook2.jsp").forward(req,resp); } }
Dao.java
public Book getBookById(int id) { String sql = "select * from book where id =?"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; Book book = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String number= rs.getString("number"); String bookname = rs.getString("bookname"); String writername = rs.getString("writername"); String publishname= rs.getString("publishname"); int shuliang= rs.getInt("shuliang"); book= new Book(id, number, bookname, writername, publishname, shuliang); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return book; }
这个写的有问题