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.

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:


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


Delete data in a table using JDBC and MYSQL
 

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?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

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 *