Skip to content
Related Articles

Related Articles

Delete Contents From Table Using JDBC
  • Last Updated : 24 Nov, 2020

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


 

3. DELETE CONTENTS IN A TABLE: Delete the customer details from “cuslogin” table whose id is 1.

  1. Initialise a string with the sql query as follows
    String sql=”delete from cuslogin where id=1″;
  2. Initialise the below objects of Connection class, PreparedStatement clas(needed for jdbc ) and connect with database as follows  
    Connection con=null;
    PreparedStatement p=null;
    con=connection.connectDB();
  3. Now, add the sql query of step 3.1 inside prepareStatement and execute it as follows
    p =con.prepareStatement(sql);
    p.execute();
  4. 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

filter_none

edit
close

play_arrow

link
brightness_4
code

/*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);
             
        }
    }
      
}

chevron_right


Output:

The customer whose id was 1, has been deleted.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :