servlet mysql的删除和更新

servlet mysql的删除和更新 不能用红字的方法:


public static ResultSet getResultSet(Statement stmt, String sql) {
            ResultSet res = null;
            try {
                res = stmt.executeQuery(sql);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return res;
        }


只能用蓝字里面的方法:

public static  void getResultSets(Statement stmt, String sql) {

                try {
                     stmt.executeUpdate(sql);
                } catch (SQLException e) {
                    e.printStackTrace();
                }

            }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,在JSP页面中,您需要创建一个包含删除按钮的表格,并为每个条目设置唯一的ID。例如: ``` <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Email</th> <th>Delete</th> </tr> </thead> <tbody> <% // Create a database connection Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password"); // Retrieve data from the database and display it in the table Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM users"); while (rs.next()) { String id = rs.getString("id"); String name = rs.getString("name"); String email = rs.getString("email"); %> <tr> <td><%= id %></td> <td><%= name %></td> <td><%= email %></td> <td><a href="DeleteServlet?id=<%= id %>">Delete</a></td> </tr> <% } // Close the database connection rs.close(); stmt.close(); conn.close(); %> </tbody> </table> ``` 在上面的代码中,我们从数据库中检索用户数据,并为每个用户创建一个删除链接。链接指向一个名为DeleteServletservlet,并将用户的ID作为参数传递。 现在,我们需要创建DeleteServlet,它将处理删除请求并从数据库中删除相应的记录。例如: ``` import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class DeleteServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Get the user ID from the request parameter String id = request.getParameter("id"); try { // Create a database connection Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password"); // Delete the user record from the database PreparedStatement stmt = conn.prepareStatement("DELETE FROM users WHERE id = ?"); stmt.setString(1, id); stmt.executeUpdate(); // Close the database connection stmt.close(); conn.close(); // Redirect the user back to the original page response.sendRedirect("users.jsp"); } catch (SQLException e) { e.printStackTrace(); } } } ``` 在上面的代码中,我们首先从请求参数中获取用户ID,然后使用PreparedStatement从数据库中删除相应的记录。最后,我们将用户重定向回原始页面。 最后,您需要确保在web.xml文件中正确配置DeleteServlet。例如: ``` <servlet> <servlet-name>DeleteServlet</servlet-name> <servlet-class>DeleteServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DeleteServlet</servlet-name> <url-pattern>/DeleteServlet</url-pattern> </servlet-mapping> ``` 现在,当用户单击表格中的删除链接时,将调用DeleteServlet并从数据库中删除相应的记录。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值