-
Notifications
You must be signed in to change notification settings - Fork 0
/
Read.java
37 lines (30 loc) · 1.14 KB
/
Read.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
package task1;
import java.sql.*;
public class Read {
Read(String url, String username, String password){
try {
Connection conn = DriverManager.getConnection(url, username, password);
// Create a prepared statement for the update query
String selectQuery = "SELECT * FROM Students";
PreparedStatement selectStmt = conn.prepareStatement(selectQuery);
// Set the value for the WHERE clause in the select query
// selectStmt.setInt(1, 123); // student_id
// Execute the select query and get the result set
ResultSet rs = selectStmt.executeQuery();
// Iterate through the result set and print the results
while (rs.next()) {
int studentId = rs.getInt("student_id");
String firstName = rs.getString("first_name");
String lastName = rs.getString("last_name");
int grade = rs.getInt("grade");
System.out.println("Student: " + studentId + " " + firstName + " " + lastName + " " + grade);
}
// Close the result set, prepared statement, and connection
rs.close();
selectStmt.close();
conn.close();
}catch(Exception e) {
System.out.println(e);
}
}
}