Delete Contents From Table Using JDBC
Last Updated :
06 Mar, 2023
JDBC(Java Database Connectivity) is a standard API(application interface) between the java programming language and various databases like Oracle, SQL, PostgreSQL, etc. It connects the front end(for interacting with the users) with the backend(for storing data).
Approach:
1. CREATE DATABASE: Create a database using sqlyog and create some tables in it and fill data inside it in order to delete contents of a table. Here, for example, Name a database as “hotelman” and table names are "cuslogin" and "adminlogin". Taking "cuslogin" table as an example.


2. CREATE CONNECTION: Open Netbeans and create a new package. Inside the package, open a new java file and type the below code for JDBC connectivity and save the filename with connection.java.
Java
// Create JDBC Connection
import java.sql.*;
public class connection {
Connection con = null;
public static Connection connectDB()
{
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/hotelman",
"root", "1234");
// here,root is the username and 1234 is the
// password,you can set your own username and
// password.
return con;
}
catch (SQLException e) {
System.out.println(e);
}
}
}
3. DELETE CONTENTS IN A TABLE: Delete the customer details from "cuslogin" table whose id is 1.
- Initialise a string with the sql query as follows
String sql="delete from cuslogin where id=1"; - Initialise the below objects of Connection class, PreparedStatement class(needed for jdbc ) and connect with database as follows
Connection con=null;
PreparedStatement p=null;
con=connection.connectDB(); - Now, add the sql query of step 3.1 inside prepareStatement and execute it as follows
p =con.prepareStatement(sql);
p.execute(); - Open a new java file (here, its result.java) inside the same package and type the full code (shown below) for deleting the details of customer whose id is 1, from table "cuslogin".
NOTE: Both the files viz result.java and connection.java should be inside the same package, else the program won't give the desired output!!
Java
/*package whatever //do not write package name here */
import java.sql.*;
public class result {
public static void main(String[] args)
{
Connection con=null;
PreparedStatement p=null;
con=connection.connectDB();
try{
String sql="delete from cuslogin where id=1";
p =con.prepareStatement(sql);
p.execute();
}catch(SQLException e){
System.out.println(e);
}
}
}
Output:

The customer whose id was 1, has been deleted.
Similar Reads
How to Delete a Table from a Databricks Connection Databricks is a powerful data analytics platform built around Apache Spark. It simplifies big data processing, machine learning, and business intelligence tasks in a unified environment. One of the key tasks when working with data in Databricks is managing our tables by deleting no longer required t
3 min read
Spring Data JPA - Delete Records From MySQL Spring Boot simplifies database operations using Spring Data JPA, which provides built-in methods for CRUD (Create, Read, Update, Delete) operations. In modern application development, data manipulation is a critical task, and Java Persistence API (JPA) simplifies this process. Java persistence API
4 min read
How to Insert Records to a Table using JDBC Connection? Before inserting contents in a table we need to connect our java application to our database. Java has its own API which JDBC API which uses JDBC drivers for database connections. Before JDBC, ODBC API was used but it was written in C which means it was platform-dependent. JDBC API provides the appl
4 min read
SQL Server DELETE and DROP TABLE Commands In SQL Server, managing data involves not only inserting and updating records but also removing them when they're no longer needed. Two important commands for data removal are DELETE and DROP TABLE. These commands play crucial roles in maintaining database integrity and managing database structures.
4 min read
Java Program to Retrieve Contents of a Table Using JDBC connection It can be of two types namely structural and non-structural database. The structural database is the one which can be stored in row and columns. A nonstructural database can not be stored in form of rows and columns for which new concepts are introduced which would not be discussing here. Most of th
5 min read