-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoJdbc.java
More file actions
50 lines (43 loc) · 1.39 KB
/
Copy pathDemoJdbc.java
File metadata and controls
50 lines (43 loc) · 1.39 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
/**
* Example: Delete a Record using Statement
*
* Database Schema:
* CREATE DATABASE java_jdbc;
* USE java_jdbc;
* CREATE TABLE student (
* id INT PRIMARY KEY AUTO_INCREMENT,
* name VARCHAR(100),
* age INT,
* course VARCHAR(100)
* );
*
* Expected Output:
* Connection established
* 1 row(s) deleted successfully.
* Connection closed
*/
public class DemoJdbc {
public static void main(String[] args) throws Exception {
// 1. Define connection details
String url = "jdbc:postgresql://localhost:5432/java_jdbc";
String uname = "postgres";
String pass = "YOUR_PASSWORD"; // Replace with your actual password
// The SQL query to delete data
String sql = "DELETE FROM student WHERE id = 101";
// 2. Establish connection
Connection con = DriverManager.getConnection(url, uname, pass);
System.out.println("Connection established");
// 3. Create Statement
Statement st = con.createStatement();
// 4. Execute SQL
int rowsAffected = st.executeUpdate(sql);
System.out.println(rowsAffected + " row(s) deleted successfully.");
// 5. Close resources
st.close();
con.close();
System.out.println("Connection closed");
}
}