JDBC PreparedStatement Delete example Using Mysql database

Create a table and insert few records in test database in mysql before executing this program, 

Create query

    1. create table student(id int NOT NULL AUTO_INCREMENT, name varchar(100), password varchar(100), PRIMARY KEY(id));

Insert Query

  1. insert into student(name, password) values('candidjava','123345');

Also make sure to add Mysql jar file in classpath

Mysql jar can be downloaded from



package com.candidjava.jdbc;

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

public class BasicDelete {

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
			Class.forName("com.mysql.jdbc.Driver"); // loads mysql driver
			Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); // create new connection with test database
			String query="delete from student where name=?";
			PreparedStatement ps=con.prepareStatement(query);  // generates sql query
			ps.setString(1, "mathanlal");
			ps.executeUpdate(); // execute it on test database
			System.out.println("Record successfuly deleted");

Record Successfully deleted

Related Post