JavaWeb——JDBC 操作数据库
五、删除数据
- 在前面几篇博客中,已经实现在 JSP 页面查询、修改和增加数据,现在实现从 JSP 页面删除数据。
- 首先新建一个 Servlet 用于删除数据库的操作,代码如下:
//@Date:2019/12/3 // Author:御承扬 //E-mail:2923616405@qq.com package com.lyq.ServletSet; 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.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; @WebServlet(name = "DeleteServlet", urlPatterns = "/DeleteServlet") public class DeleteServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int id = Integer.parseInt(request.getParameter("id")); try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8"; String username = "root"; String password = "*********"; Connection conn = null; try { conn = DriverManager.getConnection(url, username, password); if(conn != null){ String sql = "delete from tb_books where id=?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1,id); ps.executeUpdate(); ps.close(); conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } response.sendRedirect("FindServlet"); } }
- 接着建立可视化的 JSP 页面,代码如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="com.lyq.bean.Book"%> <%@ page import="java.util.List" %> <html> <head> <title>修改图书信息</title> <style type="text/css"> body { background: #d7c7e9; align-items: center; text-align: center; } </style> <script type="text/javascript"> function check(form) { with (form) { if (bookCount.value === "") { alert("图书数量不能为空"); return false; } return true; } } </script> </head> <body> <table align="center" width="700" border="1" height="180" bordercolor="white" cellpadding="1" cellspacing="1"> <tr bgcolor="white"> <td align="center" colspan="7"> <h2>所有图书信息</h2> </td> </tr> <tr align="center" bgcolor="#e1ffc1"> <td><b>ID</b></td> <td><b>图书名称</b></td> <td><b>价格</b></td> <td><b>数量</b></td> <td><b>作者</b></td> <td><b>修改数量</b></td> <td><b>删 除</b></td> </tr> <% List<Book> list = (List<Book>) request.getAttribute("list"); if(list == null ||list.size() < 1){ out.print("没有数据"); }else{ for(Book book:list){ %> <tr align="center"> <td><%=book.getId()%> </td> <td><%=book.getName()%> </td> <td><%=book.getPrice()%> </td> <td><%=book.getBookCount()%> </td> <td><%=book.getAuthor()%> </td> <td> <form action="${pageContext.request.contextPath}/UpdateServlet" method="post" onsubmit="return check(this)"> <input type="hidden" name="id" value="<%=book.getId()%>"> <label> <input type="text" name="bookCount" size="3"> </label> <input type="submit" value="修改"> </form> </td> <td> <a href="${pageContext.request.contextPath}/DeleteServlet?id=<%=book.getId()%>">删除</a> </td> </tr> <% } } %> <tr bgcolor="white"> <td align="center" colspan="3"> <input type="button" value="添加图书信息" onclick="window.location.href='http://localhost:8080/JavaWebProject1_war_exploded/JDBCOption/book.jsp'"> </td> <td align="center" colspan="3"> <input type="button" value="图书信息查询" onclick="window.location.href='http://localhost:8080/JavaWebProject1_war_exploded/JDBCOption/book_list.jsp'"> </td> </tr> </table> </body> </html>
- 渲染效果如下:
- 在第五行点击删除,刷新页面后如下: