Share on Google+Share on Google+

JDBC Delete Statement Example

Advertisement
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

Advertisements

Advertisement

Posted on: October 13, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.