-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectStudents.java
More file actions
46 lines (37 loc) · 1.13 KB
/
Copy pathSelectStudents.java
File metadata and controls
46 lines (37 loc) · 1.13 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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class SelectStudents {
public static void main(String[] args) {
try {
// 1. Connect
Connection conn = DriverManager.getConnection(
":url",
"root",
"pass"
);
// 2. Write SELECT query
String sql = "SELECT * FROM students";
// 3. Prepare statement
PreparedStatement stmt = conn.prepareStatement(sql);
// 4. Execute query
ResultSet rs = stmt.executeQuery();
// 5. Read data row by row
while (rs.next()) {
System.out.println(
rs.getInt("id") + " " +
rs.getString("name") + " " +
rs.getInt("age") + " " +
rs.getString("email")
);
}
// 6. Close
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}