Deleting Data using JDBC

by | Apr 15, 2021 | Java

Home » Java » Deleting Data using JDBC

Introduction

JDBC represents Java Database Connectivity. This goes about as a connection interfacing Java programs with different free data sets. The JDBC utilizes a few apparatuses to play out the different capacities as referenced underneath:

  • Setting up a solid association between your Java code and an outside information base.
  • Creation of SQL or MySQL articulations.
  • When inquiries in SQL or MySQL emerge, the JDBC utilizes different APIs to settle/resolve them.
  • Reading data from the different data sets, adjusting them as well as erasing them.

As a major idea, the JDBC represents a total arrangement of interfaces that permit clients to associate with data sets and get information from the equivalent for the execution of different projects. With the assistance of the JDBC, different executables can be composed as follows:

  • Java Applications
  • Java Applets
  • Java Servlets
  • Java ServerPages (JSPs)
  • Enterprise JavaBeans (EJBs)

Prerequisites

The capacity to read, modify, and delete information from data sets is an extremely basic ability of any program. Without free availability of information, any genuine program will ultimately end because of information starvation. The example given in the articles tells the best way to delete information from databases. The ‘username’ and ‘secret key’ referenced in the example codes can be supplanted by genuine qualifications. You ought to likewise ensure the data set you are utilizing (SQL, MySQL, or some other data set) is cutting-edge and appropriately working.

Creating a Database

In order to delete records from a database, a database needs to be created first.

  • Importing the packages: Requires that you incorporate the packages containing the JDBC classes required for data set programming. Regularly, utilizing import java.sql.* should be enough.
  • Register the JDBC driver: Requires that you instate a driver so you can open a correspondences channel with the data set.
  • Open a connection: Requires utilizing the DriverManager.getConnection() technique to make a Connection object, which addresses an actual association with a data set server.
  • Execute a Query: Requires utilizing an object of type Statement for building and presenting a SQL statement to erase records from a table. This Query utilizes the WHERE provision to erase conditional records.
  • Clean up the environment: Requires unequivocally shutting all data set assets as opposed to depending on the JVM’s trash assortment.

Sample Code

//This code will demonstrate how to read data using JDBC
import java.sql.*;
public class Demo {
static final String JDBC_DRIVER = “//name of the driver”;
static final String DB_URL = “//URL of the Database”;
static final String USER = “//your username”;
static final String PASS = “//Password”;
public static void main (String args [])
{
Connection conn = null;
Statement stmt = null;
try {
Class.forName (“//Name of the Driver”);
System.out.println (“Establishing connection to the Database”);
conn = DriverManager.getConnection (DB_URL, USER, PASS);
System.out.println(“Connection made successfully with DB”);
System.out.println(“Creating Statement”);
stmt = conn.createStatement ();
String sql = “DELETE from Registration” + “WHERE id = 101”;
stmt.executeUpdate(sql);
sql = “SELECT id, first, last, age FROM Registration”;
Result rs = stmt.executeQuery(sql);
while (rs.next())
{
int id = rs.getInt(“id”);
int age = rs.getInt(“age”);
String first = rs.getString(“first”);
String last = rs.getString (“last”);
System.out.println(“ID: “ + id);
System.out.println(“Age: “ + age);
System.out.println(“First: “ + first);
System.out.println(“Last: “ + last);
}
rs.close();
}catch(SQL Exception se){
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try{
if (stmt! = null)
conn.close();
} catch (SQLException se){
}
try {
if (stmt! =null)
conn.close();
}
catch(SQLException se){
}
try{
if (conn!= null)
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
}
System.out.println(“Exit”);
}
}

 

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author