-
Notifications
You must be signed in to change notification settings - Fork 0
/
Delete.java
32 lines (27 loc) · 969 Bytes
/
Delete.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package task1;
import java.sql.*;
import java.util.Scanner;
public class Delete {
Delete(String url, String username, String password){
// Establish a connection to the database
try {
Scanner sc = new Scanner(System.in);
Connection conn = DriverManager.getConnection(url, username, password);
// Create a prepared statement for the delete query
String deleteQuery = "DELETE FROM Students WHERE student_id = ?";
PreparedStatement deleteStmt = conn.prepareStatement(deleteQuery);
// Set the value for the WHERE clause in the delete query
System.out.println("Enter Students id: ");
int id = sc.nextInt();
deleteStmt.setInt(1, id); // student_id
// Execute the delete query
deleteStmt.executeUpdate();
Read r = new Read(url, username, password);
// Close the prepared statement and connection
deleteStmt.close();
conn.close();
}catch(Exception e) {
System.out.println(e);
}
}
}