-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDBCExample.java
More file actions
145 lines (114 loc) · 5.24 KB
/
JDBCExample.java
File metadata and controls
145 lines (114 loc) · 5.24 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import java.sql.*;
public class JDBCExample {
// Database credentials
private static final String URL = "jdbc:mysql://localhost:3306/testjdb";
private static final String USER = "root";
private static final String PASSWORD = "Nutan@123";
public static void main(String[] args) {
try {
// Step 1: Load MySQL JDBC Driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Step 2: Establish connection
Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
System.out.println("Connected to the database successfully!");
// Step 3: Create Table
createTable(connection);
// Step 4: Insert Data
insertData(connection, 10, "John Doe", "nut@123.com");
insertData(connection, 11, "Jane Smith", "nuts@212.com");
//show data
showData(connection,10);
showData(connection,11);
// Step 5: Retrieve Data
retrieveData(connection);
// Step 6: Update Data
updateData(connection, 10, "nutan@321.com");
// Step 7: Delete Data
deleteData(connection, 10);
// search data
searchData(connection, 11);
// Step 8: Close Connection
connection.close();
System.out.println("Database connection closed.");
} catch (Exception e) {
e.printStackTrace();
}
}
// Create Table Method
private static void createTable(Connection connection) throws SQLException {
String createTableSQL = "CREATE TABLE IF NOT EXISTS users ("
+ "id INT PRIMARY KEY, "
+ "name VARCHAR(100), "
+ "email VARCHAR(100) UNIQUE)";
try (Statement stmt = connection.createStatement()) {
stmt.execute(createTableSQL);
System.out.println("Table 'users' created successfully!");
}
}
// Insert Data Method
private static void insertData(Connection connection, int id, String name, String email) throws SQLException {
String insertSQL = "INSERT INTO users (id, name, email) VALUES (?, ?, ?)";
try (PreparedStatement pstmt = connection.prepareStatement(insertSQL)) {
pstmt.setInt(1, id);
pstmt.setString(2, name);
pstmt.setString(3, email);
pstmt.executeUpdate();
System.out.println("Inserted: " + name);
}
}
// Retrieve Data Method
private static void retrieveData(Connection connection) throws SQLException {
String selectSQL = "SELECT * FROM users";
try (Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(selectSQL)) {
System.out.println("User Data:");
while (rs.next()) {
System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name") + ", Email: " + rs.getString("email"));
}
}
}
// Update Data Method
private static void updateData(Connection connection, int id, String newEmail) throws SQLException {
String updateSQL = "UPDATE users SET email = ? WHERE id = ?";
try (PreparedStatement pstmt = connection.prepareStatement(updateSQL)) {
pstmt.setString(1, newEmail);
pstmt.setInt(2, id);
pstmt.executeUpdate();
System.out.println("Updated email for ID: " + id);
}
}
// Delete Data Method
private static void deleteData(Connection connection, int id) throws SQLException {
String deleteSQL = "DELETE FROM users WHERE id = ?";
try (PreparedStatement pstmt = connection.prepareStatement(deleteSQL)) {
pstmt.setInt(1, id);
pstmt.executeUpdate();
System.out.println("Deleted user with ID: " + id);
}
}
private static void showData(Connection connection, int id) throws SQLException {
String query = "SELECT * FROM users WHERE id = ?";
try (PreparedStatement pstmt = connection.prepareStatement(query)) {
pstmt.setInt(1, id);
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
System.out.println("Show Data → ID: " + rs.getInt("id")
+ ", Name: " + rs.getString("name")
+ ", Email: " + rs.getString("email"));
} else {
System.out.println("No data found for ID: " + id);
}
}
}
}
private static void searchData(Connection connection, int id) throws SQLException {
String selectSQL = "SELECT * FROM users where id = 6";
try (Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(selectSQL)) {
System.out.println("User Data:");
while (rs.next()) {
System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name") + ", Email: " + rs.getString("email"));
}
}
}
}