删除图书信息
deletebook.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; 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> <% } %> <h align="center">删除图书信息</h> <div class="content"> <div class="main"> <a href="guanliyuan.jsp">返回主页</a> <br><br> <form name="form" action="Servlet?method=getbyname" method="post" onsubmit="return check(form)"> <a>书名:</a> <input type="text" id="bookname" name="bookname" /> <br><br> <input type="submit" value="删除" /> </form> </div> </div> </body> </html>
deletebook2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h>信息删除</h> <div class="content"> <div class="main"> <a href="guanliyuan.jsp">返回主页</a> <table> <tr> <td>编号</td> <td>${book.number }</td> <tr> <tr> <td>书名</td> <td>${book.bookname}</td> <tr> <tr> <td>作者名</td> <td>${book.writername }</td> <tr> <tr> <td>出版社名</td> <td>${book.publishname }</td> <tr> <tr> <td>可借阅数量</td> <td>${book.shuliang}</td> <tr> </table> <form action="Servlet?method=deleteBook" method="post" onsubmit="return check();"> <input type="hidden" id="bookname" name="bookname" value="${book.bookname}"> <input type="submit" name="action1" value="确定删除"> </form> </div> <script type="text/javascript"> function check() { if (confirm("是否确认删除该信息?")){ return true; }else{ return false; } } </script> </body> </html>
servlet.java
private void getbyname(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("deletebook.jsp").forward(req, resp); } else { req.setAttribute("book", book); req.getRequestDispatcher("deletebook2.jsp").forward(req,resp); } }
Dao.java
//删除图书 public boolean deleteBook(String bookname) { String sql="delete from book where bookname='" + bookname + "'"; Connection conn = DBUtil.getConn(); Statement state = null; int a = 0; boolean f = false; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; }