-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDBC.java
More file actions
25 lines (22 loc) · 926 Bytes
/
JDBC.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
import java.sql.*;
public class JDBC {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/testdb";
String user = "root";
String password = "password";
try (Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement()) {
// Insert data
String insertQuery = "INSERT INTO students (id, name, age) VALUES (1, 'John', 20)";
stmt.executeUpdate(insertQuery);
// Retrieve data
String selectQuery = "SELECT * FROM students";
ResultSet rs = stmt.executeQuery(selectQuery);
while (rs.next()) {
System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name") + ", Age: " + rs.getInt("age"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}