Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Java Programming » JDBC » Delete data in a table using JDBC and MYSQL
Suryateja Pericherla Categories: JDBC. No Comments on Delete data in a table using JDBC and MYSQL
0
(0)

In this example, we will try to delete the record of student with id 5. The Java code for deleting a record in a MYSQL table using JDBC is given below:

import java.sql.*;
public class DeleteData {
   static final String DB_URL = "jdbc:mysql://localhost/sampledb";
   static final String USER = "root";
   static final String PASS = "123456";
   static final String QUERY = "DELETE from students where id=5";

   public static void main(String[] args) {
      // Open a connection
      try
	  {
		  Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
		  Statement stmt = conn.createStatement();
		  int rs = stmt.executeUpdate(QUERY);
		  System.out.println("No. of rows updated = " + rs);
      } 
	  catch (SQLException e) {
         e.printStackTrace();
      } 
   }
}

 

Output for the above program is given below:

No. of rows updated = 1

 

Note: The above compiles successfully only when we add the JAR file to the CLASSPATH environment variable as shown below:

set CLASSPATH=.;C:\Soft\mysql-connector-j-9.1.0\mysql-connector-j-9.1.0.jar

 

The data in the table after the above update (delete operation) is given below:

Delete data in a table using JDBC and MYSQL
 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


We can from the above result that the record of student with id 5 has been deleted.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
Pinterest
Youtube
Instagram