-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUpdateController.java
More file actions
126 lines (100 loc) · 4.13 KB
/
UpdateController.java
File metadata and controls
126 lines (100 loc) · 4.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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package jobinterviewpreparationsystem;
import java.net.URL;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.SelectionMode;
import jobinterviewpreparationsystem.DB.DatabaseConnection;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TextField;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import jobinterviewpreparationsystem.DB.DatabaseHandler;
public class UpdateController implements Initializable {
@FXML
private ListView<String> userListView;
@FXML
private TextField newUsernameField;
@FXML
private TextField newPasswordField;
private DatabaseHandler databaseHandler = new DatabaseHandler();
private void loadJobSeekers() {
String query = "SELECT user_id, email FROM users_table WHERE user_type = 'JobSeeker'";
try (Connection conn = (Connection) DatabaseConnection.getInstance();
PreparedStatement stmt = conn.prepareStatement(query);
ResultSet rs = stmt.executeQuery()) {
userListView.getItems().clear();
while (rs.next()) {
int userId = rs.getInt("user_id");
String email = rs.getString("email");
userListView.getItems().add("ID: " + userId + ", Email: " + email); // Add to ListView
}
} catch (SQLException e) {
showAlert("Error", "Failed to load JobSeekers from database.");
e.printStackTrace();
}
}
@FXML
private void handleUpdateUserAction(ActionEvent event) {
String selectedItem = userListView.getSelectionModel().getSelectedItem();
if (selectedItem == null) {
showAlert("Error", "Please select a user to update.");
return;
}
String userIdString = selectedItem.split(",")[0].split(":")[1].trim(); // Get the userId part
int userId = Integer.parseInt(userIdString); // Convert to integer
String newUsername = newUsernameField.getText().trim();
String newPassword = newPasswordField.getText().trim();
if (newUsername.isEmpty() && newPassword.isEmpty()) {
showAlert("Error", "Please enter a new password.");
return;
}
if (!newPassword.isEmpty()) {
boolean updated = databaseHandler.updateJobSeekerPassword(userId, newPassword);
if (updated) {
showAlert("Success", "Password updated successfully.");
loadJobSeekers(); // Refresh the ListView
} else {
showAlert("Error", "Failed to update password. Please try again.");
}
}
newUsernameField.clear();
newPasswordField.clear();
}
private void showAlert(String title, String message) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
@FXML
private void handleGoBackButton(ActionEvent event) {
JobInterviewPreparationSystem.showManageScreen(); // Replace with your method to navigate back
}
@Override
public void initialize(URL location, ResourceBundle resources) {
// Initialize ListView and load job seekers from the database
userListView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
loadJobSeekers(); // Load job seekers into the ListView from the database
}
}