JavaWeb——操作数据库向MySQL数据库删除数据

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>
    
  • 渲染效果如下:
    在这里插入图片描述
  • 在第五行点击删除,刷新页面后如下:
    在这里插入图片描述

上一篇

下一篇

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

御承扬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值