Home » Java programming language

How to delete a particular record using JDBC in Java?

In this article, we are going to learn how to delete a particular record from MYSQL table using JDBC through java program?
Submitted by Manu Jemini, on October 14, 2017


  1. How to create a table using JDBC in Java?
  2. How to insert records through JDBC in Java?
  3. How to display all records using JDBC in Java?
  4. How to display particular record by a field using JDBC in Java?

Note: To delete a particular record from MYSQL table, you should know at least one field of that record.

Now, we need to create an object of Connection class and connect to the database.

Then, we need to create an object of Statement class and then prepare a MySQL query for displaying data which we want to delete. After this, we display data in console and put up a question to delete above data from MYSQL database.

Then, we prepare a query to delete the data and execute it by using executeUpdate() method, which is a method of Statement class.

Database details:

  • Hostname: localhost
  • Port number: 3306
  • Username: root
  • Password: 123
  • Database name: demo
  • Table name: employees
  • Field: empid (employee id)

Java program to delete a particular record using JDBC

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DeleteByID {
	public static void main(String[] args) {

			//serverhost = localhost, port=3306, username=root, password=123
			Connection cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","123");

			Statement smt=cn.createStatement();

			DataInputStream KB=new DataInputStream(;

			//input a particular employee id of which we want to delete record
			System.out.print("Enter Employee ID:");
			String eid=KB.readLine();

			//query to take data of a particular record from table employee
			String q="Select * from employees where empid='"+eid+"'";

			//to execute query
			ResultSet rs=smt.executeQuery(q);

				//to show the data
				System.out.println("Employee id:"+rs.getString(1));
				System.out.println("Employee Name:"+rs.getString(2));
				System.out.println("Employee DOB:"+rs.getString(3));
				System.out.println("Employee City:"+rs.getString(4));
				System.out.println("Employee Salary:"+rs.getString(5));
				System.out.println("Sure To Delete Above Record Yes/No?");
				String ch=KB.readLine();
					//query to delete data of a particular record from table employee
					q="delete from employees where empid='"+eid+"'";
					//to execute query
					System.out.println("Record Deleted...");
				System.out.println("Record Not Found...");
		catch(Exception e){

Output (In console)

Enter Employee ID: 100
Employee id: 100
Employee Name: Aman
Employee DOB: 10/10/1990
Employee City: Delhi
Employee Salary: 35000
Sure To Delete Above Record Yes/No? Yes
Record Deleted...


Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.