Delete Contents From Table Using JDBC
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).
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.
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 clas(needed for jdbc ) and connect with database as follows
- Now, add the sql query of step 3.1 inside prepareStatement and execute it as follows
- 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!!
The customer whose id was 1, has been deleted.