JDBC Video tutorial - How to delete Data in a MySQL Database?

This video tutorial is for beginners in database programming using JDBC and teaches them to delete the existing data in a MySQL database table.

JDBC Video tutorial - How to delete Data in a MySQL Database?

JDBC Beginners tutorial for deleting the data into MySQL database - RoseIndia.Net

The process of deleting the existing record in the table is an import process in developing the database driven application. Sometimes some records are not required in the database and there is a requirement to delete the record. In such cases delete operation is required and which can be performed from JDBC program by executing the delete sql statement from Java program.

The Java Database Connectivity API (JDBC) provides rich set of classes, interfaces for performing all types of database operations from the database. JDBC API allows the developers to write program, which connects to the MySQL database using pure type 4 JDBC driver.

In this video tutorial I am teaching you to write program in Java for deleting the data in a MySQL database table. We have used the delete statement to delete the data into the table. JDBC allows the programmers to run any query against the connected database easily and it also helps to capture the exceptions that might occur at the runtime.

Steps for updating the data from Java program:

Step 1: Connect to the MySQL database
Following code connects to the MySQL database and returns the JDBC Connection object to the Java program.

con = DriverManager.getConnection(url + db, "root", "root");

Step 2: Write the delete query - Following is the sql query which is used to delete the data.

String sql = "delete from employee where id=1 ";

Step 3: Execute the sql - The Statement object is used to execute the sql statement. Here is the code that executes the query:

st.executeUpdate(sql);

Step 4: Close the statement and connection object
Following code is used to close the Statement and Connection object:

st.close();
con.close();

Here is the video of the Tutorial of deleting the data from MySQL table:

Here is the full code of the Java program:

package net.roseindia;
package net.roseindia;
import java.sql.*;

public class DeleteRecordExample {

public static void main(String[] args) {

	System.out.println("Delete Example!");
	Connection con = null;
	String url = "jdbc:mysql://localhost:3306/";
	String db = "jdbcexamples";
	String driver = "com.mysql.jdbc.Driver";
	try {
	Class.forName(driver);
	con = DriverManager.getConnection(url + db, "root", "root");
	try {
		Statement st = con.createStatement();
		
		 String sql = "delete from employee  where id=1 ";
		
		 st.executeUpdate(sql);
		 
		 st.close();

		System.out.println("Data Deleted");
	} catch (SQLException s) {
		System.out.println("SQL statement is not executed!");
	}

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

}

To run the program right click in the java code window and then select Run As-> Java Application

After successful execution of the program record will be deleted from the MySQL table.

Briefly following are the steps to delete data in database:

  1. Connect to MySQL Database using JDBC
  2. Create the Statement Object
  3. Construct the sql
  4. Run the sql using the Statement object against database process results if any.

Download the source code of the JDBC delete example project in Eclipse project format.

So, in this tutorial on RoseIndia.net you have learned the example program for updating the data into MySQL database using the JDBC driver.

Check all JDBC Tutorials at http://www.roseindia.net/jdbc/index.shtml