- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDBC.java
More file actions
Latest commit
25 lines (22 loc) · 926 Bytes
/
Copy pathJDBC.java
File metadata and controls
25 lines (22 loc) · 926 Bytes
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
importjava.sql.*;
publicclassJDBC {
publicstaticvoidmain(String[] args) {
Stringurl = "jdbc:mysql://localhost:3306/testdb";
Stringuser = "root";
Stringpassword = "password";
try (Connectionconn = DriverManager.getConnection(url, user, password);
Statementstmt = conn.createStatement()) {
// Insert data
StringinsertQuery = "INSERT INTO students (id, name, age) VALUES (1, 'John', 20)";
stmt.executeUpdate(insertQuery);
// Retrieve data
StringselectQuery = "SELECT * FROM students";
ResultSetrs = stmt.executeQuery(selectQuery);
while (rs.next()) {
System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name") + ", Age: " + rs.getInt("age"));
}
} catch (SQLExceptione) {
e.printStackTrace();
}
}
}