DATABASE MANAGEMENT SYSTEMS LAB
EXP: 15 Write a Java program to connect to a database using
Date: JDBC and delete values into it
Aim: Write a Java program to connect to a database using JDBC and
delete values from it
SOLUTION:
1. Software Requirements:
Java Development Kit (JDK): Version 8 or later
Database: XAMPP Server
JDBC Driver: Corresponding JDBC driver for the selected database (e.g.,
MySQL Connector/J for MySQL) //mysql-connector-j-9.1.0.jar
Database Credentials: Database URL, username, and password
2. Functional Requirements:
Establish a connection to the database using JDBC
Execute a simple SQL query (e.g., retrieving data from a table)
Display query results in the console
Handle exceptions properly (e.g., SQLExceptions)
Ensure proper resource management (closing connections)
Source code
import java.sql.*;
import java.util.Scanner;
public class DeleteExample {
public static void main(String[] args) {
// Database connection details
String url = "jdbc:mysql://localhost:3306/college"; // Replace 'college'
with your database name
Page 1
String username = "root"; // Replace with your database username
String password = ""; // Replace with your database password
// SQL query to delete a row
String deleteQuery = "DELETE FROM student WHERE Stuid = ?";
try (Scanner scanner = new Scanner(System.in)) {
// Load the MySQL JDBC driver
Class.forName("com.mysql.jdbc.Driver");
System.out.println("MySQL JDBC Driver Loaded Successfully!");
// Establish the connection
Connection connection = DriverManager.getConnection(url, username,
password);
System.out.println("Connected to the database!");
// Display the records before deletion
System.out.println("Current records in the student table:");
displayRecords(connection);
// Ask the user for the ID of the row to delete
System.out.print("Enter the ID of the student to delete: ");
int studentId = scanner.nextInt();
// Prepare the SQL delete statement
PreparedStatement preparedStatement =
connection.prepareStatement(deleteQuery);
// Set the value for the parameter
preparedStatement.setInt(1, studentId);
// Execute the delete query
int rowsDeleted = preparedStatement.executeUpdate();
// Check if the deletion was successful
if (rowsDeleted > 0) {
System.out.println("The row was deleted successfully!");
} else {
System.out.println("No matching row found with the provided ID.");
}
// Display the records after deletion
System.out.println("Remaining records in the student table:");
displayRecords(connection);
Page 2
// Close the connection
connection.close();
} catch (Exception e) {
// Handle exceptions
e.printStackTrace();
}
}
private static void displayRecords(Connection connection) {
String query = "SELECT * FROM student";
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query)) {
while (resultSet.next()) {
int Stuid = resultSet.getInt("Stuid");
String Stuname = resultSet.getString("Stuname"); // Adjust column
name if different
System.out.println("Stuid: " + Stuid + ", Stuname: " + Stuname);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Complile and Execution
C:\Users\SRIKANTH\Desktop\jdbc>javac -cp .;mysql-connector-java-
9.1.0.jar DeleteExample.java
C:\Users\SRIKANTH\Desktop\jdbc>java -cp .;mysql-connector-j-9.1.0.jar
DeleteExample
Page 3
Page 4