by Snehashis Mukherjee

Hello readers how are you doing?  Welcome to the last part of the Employee Management System Using Java and Servlet and MySQL database. In the last 3 articles, I have explained to you how you can create a table and insert data into the database. How you read all the data present in the database and show in the HTML file and in the last article I have explained you can Edit data and update the data back to Database.

Now, in this article, I’m going to explain how you can delete the data from the database permanently. And the best part is when you delete an employee record the page will not reload. You will be on the same page only. To perform this operation I have already implemented delete buttons in the last article. But not written the logic to perform the task. In this article, I will complete the code and finish this CRUD operation using Servlet.

What is CRUD:

In a database we mainly perform Create, Read, Update and Delete operations. Their operations are basically called CRUD operations in short.

Geeksteps DB
  • CreateThis operation is used to insert data into the database.
  • Read – is Used to fetch the data from Database.
  • Update – This Operation is used to update data on the database or make changes to the existing Data.
  • Delete – Last operation is Delete, is used to delete data prom database.

Each of the operations has its own query to perform their respective tasks.

The syntax for Deleting data from Database:

Generally, the Delete keyword is used to perform delete operations from the database. And we can also use where condition to delete some specific details.


DELETE FROM table_name WHERE condition;


DELETE FROM geeksteps.register WHERE id =1;

I hope you understand the concept of Deleting Data From Database. You have the project ready and you understood the concept of Deleting data then let’s create the Servlet class

Add a Method in EmployeeMain.java

In the firsts article, I’ve created a controller class which contains all the MySQL DB connection related codes. Now open the EmployeeMain.java file and paste the below code before closing the final brackets.

public static int delete(int id){  
        int status=0;  
            Connection con=EmployeeMain.getConnection();  
            PreparedStatement ps=con.prepareStatement("delete from geeksteps.employee where id=?");  
        }catch(Exception e){e.printStackTrace();}  
        return status;  

Here I add delete() method which takes id as input which will be deleted. This method takes an id and performs delete operation of that id.

Creating Servlet:

Create a servlet class inside your package and save the file as DeleteServlet.java. This servlet class performs delete operation and shows the details without refreshing the page. Copy the below code and paste the code and save the file.

package com.geeksteps.employee;
import java.io.IOException;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
 * Servlet implementation class DeleteServlet
public class DeleteServlet extends GenericServlet {
private static final long serialVersionUID = 1L;
 * @see Servlet#service(ServletRequest request, ServletResponse response)
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

 String sid=request.getParameter("id");  
        int id=Integer.parseInt(sid);  
      //  printWriter.print("<script>window.alert('Record Deleted Successfully')</script>");  
            request.getRequestDispatcher("ViewEmployeesServlet").include(request, response);

After saving the file run the project and then you can see the delete button after every employee details. Just click on the delete icon to delete that employee.

You can navigate to add the employee page from view employee page by clicking the Add Employee button that is present in the top right corner.



So I hope you have understood how you can update data into the database using Java and Servlet. If you have any problem ask me in the comment section. The source code is provided below.
If you found this article helpful then don’t be cheap to share. See you at the next one.
Subscribe to GeekSteps

Happy Coding 😉

Related Posts


John January 5, 2020 - 6:43 PM

Nice explaination

Rakib February 6, 2020 - 10:15 AM

Wow!excellent. I have learnt a lot of things from this post. If anyone wants to learn more then go to https://sisayed360.blogspot.com/


Leave a Comment