Deleting Record from a table using java

Delete.html

<html>
    <head>
              <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
    </head>
    <body bgcolor="pink">
     <center>
        
        <font color="blue" size="4">
        <h2> Sookshmas Delete module </h2>
        </font>
        <br><br>
        <form action="delete" method="POST">
        Student id:<input type="text" name="id"/>
        <input type="submit" value="Delete" />
          </form>
    </center>
      
   
    </body>
</html>

Servlet: delete.java

package handler;

import DAO.user;
import java.io.IOException;
import java.io.PrintWriter;
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 javax.servlet.http.HttpSession;

@WebServlet(name = "delete", urlPatterns = {"/delete"})
public class delete extends HttpServlet {

    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
            HttpSession session = request.getSession();
        try (PrintWriter out = response.getWriter()) 
        {
          int id=Integer.parseInt(request.getParameter("id"));
           user u=new user(session);
           String status=u.delete(id);
           
           if(status.equals("success"))
{

request.getRequestDispatcher("success.jsp").forward(request,response);
}
if(status.equals("failure"))
{

request.getRequestDispatcher("failure.jsp").forward(request,response);
}
}

        
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

Classname: user.java

package DAO;

import java.sql.*;
import javax.servlet.http.HttpSession;

public class user {

    Connection con;
    Statement st;
    ResultSet rs = null;
    String status = "";
    HttpSession se;
    student s;

    public user(HttpSession session) {
        try {

            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sookshmas", "root", "root");
             se = session;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
public String delete(int id) {
        int count = 0;
        String status="";
        try {
            st = con.createStatement();
            count = st.executeUpdate("delete from sookshmas where slno='" + id + "'");
            if (count > 0) {
                status = "success";
            } else {
                status = "failure";
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return status;
    }
}

Success.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body bgcolor="lightgreen">
    <center><br><br>

        <h1> Record Deleted Successfully </h1>   
    </body>
</html>

Failure.jsp


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body bgcolor='lightgreen'>

    <center><br><br>
        <h1>Record not Deleted </h1>   


    </body>
</html>

Posted on by