JDBC Delete Statement Example


 

JDBC Delete Statement Example

In this tutorial you will learn how to delete the record of database using JDBC detele statement

In this tutorial you will learn how to delete the record of database using JDBC detele statement

JDBC Delete statement Example

A JDBC delete statement deletes the particular record of the table.

At first create table named student in MySql database and inset values into it as.

CREATE TABLE student (
RollNo int(9)  PRIMARY KEY NOT NULL,
Name tinytext NOT NULL,
Course varchar(25) NOT NULL,
Address text
 );

Insert Value Into student table

INSERT INTO student VALUES(1, 'Vinay', 'MCA', 'Mumbai') ;

JDBCDeleteExample.java

package roseindia.net;

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

public class JDBCDeleteExample {
	public static void main(String[] args) throws SQLException {
		Connection con = null; // connection reference variable for getting
		// connection
		Statement stmt = null; // Statement reference variable for query
		// Execution
		String conUrl = "jdbc:mysql://192.168.10.13:3306/";
		String driverName = "com.mysql.jdbc.Driver";
		String databaseName = "student";
		String usrName = "root";
		String usrPass = "root";
		try {
			// Loading Driver
			Class.forName(driverName);
		} catch (ClassNotFoundException e) {
			System.out.println(e.toString());
		}
		try {
			// Getting Connection
			con = DriverManager.getConnection(conUrl + databaseName, usrName,
					usrPass);
			// Creating Statement for query execution
			stmt = con.createStatement();
			// creating Query String
			String query = "DELETE FROM student WHERE RollNo=1";
			// Updating Table
			stmt.executeUpdate(query);
			System.out.println("Record deleted Successfully....");
		} catch (Exception e) {
			System.out.println(e.toString());
		} finally {
			// Closing Connection
			con.close();
			stmt.close();
		}
	}
}
When you run this application it will display message as shown below:

Record deleted Successfully....

Download this example code

Ads