-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJDBC15.java
75 lines (63 loc) · 2.19 KB
/
JDBC15.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
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
/*
Steps to Set Up MySQL:
1. **Create Database**:
```sql
CREATE DATABASE school;
```
2. **Create a Table**:
```sql
USE school;
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
age INT
);
```
3. **Insert Sample Records**:
```sql
INSERT INTO students (name, age) VALUES ('John Doe', 20);
INSERT INTO students (name, age) VALUES ('Jane Smith', 22);
```
*/
import java.sql.*;
public class DisplayRecords {
public static void main(String[] args) {
// Database credentials
String url = "jdbc:mysql://localhost:3306/school"; // Change to your database URL
String username = "root"; // Change to your MySQL username
String password = "password"; // Change to your MySQL password
// JDBC connection and query execution
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
// 1. Establish the connection
connection = DriverManager.getConnection(url, username, password);
// 2. Create a statement object
statement = connection.createStatement();
// 3. Execute SQL query to fetch all records from the students table
String query = "SELECT * FROM students";
resultSet = statement.executeQuery(query);
// 4. Process the result set
System.out.println("ID\tName\tAge");
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
// Display the records
System.out.println(id + "\t" + name + "\t" + age);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// Close the resources to prevent memory leaks
try {
if (resultSet != null) resultSet.close();
if (statement != null) statement.close();
if (connection != null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}