Delete Contents From Table Using JDBC

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
// Create JDBC Connectionimport 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 followsString sql=”delete from cuslogin where id=1′′;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();Now, add the sql query of step 3.1 inside prepareStatement and execute it as followsp =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”.
Initialise a string with the sql query as followsString sql=”delete from cuslogin where id=1′′;
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();
Now, add the sql query of step 3.1 inside prepareStatement and execute it as followsp =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.
JDBC
Picked
Technical Scripter 2020
Java
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Java Programming Examples
Strings in Java
Differences between JDK, JRE and JVM
Abstraction in Java