我对 Java 和 JavaFX 相当陌生,我正在尝试编写一个从 MYSQL 数据库中提取用户数据的程序。我的连接有效,但我无法将信息提取到 TableView 中。现在我已经重做了很多次,现在我不知道我在哪里,特别是因为我没有出错。
有人可以告诉我如何使它正常工作。
这是用户类
public class Users {
public Integer userID;
public String userName;
public String password;
public Users(Integer userID, String userName, String password) {
this.userID = userID;
this.userName = userName;
this.password = password;
}
private static final ObservableList<Users> allUsersList = FXCollections.observableArrayList();
/** This method adds Users to the all Users observable list.
* @param Users */
public static void addUsers(Users users) {
allUsersList.add(users);
}
/** This method gets Users from the all Users observable list.
* @return all Users*/
public static ObservableList<Users> getAllUsers() {
return allUsersList;
}
/** This method modifies Users on the Users observable list
* @param newUsers.*/
public void modifyUsers(Users newUsers) {
}
/** This method deletes Users from the Users observable list
* @param selectedUser.*/
public void deleteUsers(Users selectedUser) {
}
public int getUserID() {
return userID;
}
public void setUserID(int userID) {
this.userID = userID;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setUser(int userID, String userName, String password) {
}
}
这是控制器类
public class UsersScreenController implements Initializable {
@FXML
public TableView<Users> userTable;
@FXML
private TextField userID;
@FXML
private TextField userName;
@FXML
private TextField password;
@FXML
private TableColumn<Users, Integer> usernameIDCol;
@FXML
private TableColumn<Users, String> userNameCol;
@FXML
private TableColumn<Users, String> passwordCol;
@FXML
private Button Dashboard;
@FXML
private Button editUser;
@FXML
private Button newUserBtn;
@FXML
private Button saveUserBtn;
@FXML
private Button resetUserBtn;
public UsersScreenController() {
}
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
userTable.setItems(Users.getAllUsers());
usernameIDCol.setCellValueFactory(new PropertyValueFactory<>("User_ID"));
userNameCol.setCellValueFactory(new PropertyValueFactory<>("User_Name"));
passwordCol.setCellValueFactory(new PropertyValueFactory<>("Password"));
}
// Generates unique id for each new user as added
AtomicInteger count = new AtomicInteger(4);
/** This method adds a new user to the Users table
* @param actionEvent
*/
@FXML
public void newUser(ActionEvent actionEvent) throws IOException {
int userIDS = count.incrementAndGet();
String userNameS = userNameCol.getText();
String passwordS = passwordCol.getText();
// Users.addUser(new User(int userID, String userName, String password));
Parent root = FXMLLoader.load(getClass().getResource("/View/UsersScreen.fxml"));
Stage stage = (Stage)((Node)actionEvent.getSource()).getScene().getWindow();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**Sets modified users to show in user table
* @param users
**/
@FXML
public void setUser(ActionEvent event){
String userIDS=userID.getText();
String userNameS=userName.getText();
String passwordS=password.getText();
userTable.setItems(Users.getAllUsers());
usernameIDCol.setCellValueFactory(new PropertyValueFactory<>("User_ID"));
userNameCol.setCellValueFactory(new PropertyValueFactory<>("User_Name"));
passwordCol.setCellValueFactory(new PropertyValueFactory<>("Password"));
}
public void editUser(ActionEvent actionEvent) throws IOException {
TableViewSelectionModel<Users> selectionModel = userTable.getSelectionModel();
ObservableList<Users> selectedItems = selectionModel.getSelectedItems();
Users users = null;
users = userTable.getSelectionModel().getSelectedItem();
if (users == null) {
Alert errorAlert = new Alert(AlertType.ERROR);
errorAlert.setTitle("Missing Selection Error");
errorAlert.setContentText("Missed item selection. Can you select the user you want to modify?");
errorAlert.show();
return;
}
int userIDS = users.getUserID();
String userNameS = users.getUserName();
String passwordS = users.getPassword();
FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/UsersScreen.fxml"));
Parent root = loader.load();
Users user = loader.getController();
Stage stage = (Stage)((Node)actionEvent.getSource()).getScene().getWindow();
stage.setScene(new Scene(root));
stage.show();
}
@FXML
private void deleteUser(ActionEvent actionEvent) {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Delete User");
alert.setContentText("Are you sure you wish to delete user?");
if(alert.showAndWait().get() == ButtonType.OK) {
userTable.getItems().remove(userTable.getSelectionModel().getSelectedItem());
}
}
public void saveUser(ActionEvent actionEvent) throws IOException {
TableViewSelectionModel<Users> selectionModel = userTable.getSelectionModel();
ObservableList<Users> selectedItems = selectionModel.getSelectedItems();
Users users = null;
users = userTable.getSelectionModel().getSelectedItem();
if (users == null) {
Alert errorAlert = new Alert(AlertType.ERROR);
errorAlert.setTitle("Missing Selection Error");
errorAlert.setContentText("Missed item selection. Can you select the user you want to modify?");
errorAlert.show();
return;
}
int userIDS = users.getUserID();
String userNameS = users.getUserName();
String passwordS = users.getPassword();
FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/UsersScreen.fxml"));
Parent root = loader.load();
Users user = loader.getController();
Stage stage = (Stage)((Node)actionEvent.getSource()).getScene().getWindow();
stage.setScene(new Scene(root));
stage.show();
}
/** This method brings user back to main screen when clicking cancel button.
* @param actionEvent
*/
@FXML
private void resetUser(ActionEvent event) throws IOException {
}
/** This method brings user back to Dashboard screen when clicking Dashboard button.
* @param actionEvent
*/
@FXML
private void goToDashBoard(ActionEvent event) throws IOException {
Stage primaryStage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("/View/Dashboard.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("SCHEDULER");
primaryStage.show();
}
我认为这可能是导致问题的原因,我称之为准备好的声明。
public class DBUsers {
private static ObservableList<Users> getAllUsers(){
ObservableList<Users> userData = FXCollections.observableArrayList();
try{
String sql = "SELECT * from users";
PreparedStatement ps = DBConnection.getConn().prepareStatement(sql);
ResultSet rs = ps.executeQuery(sql);
while (rs.next()) {
int userID = rs.getInt("User_ID");
String userName = rs.getString("User_Name");
String password = rs.getString("Password");
UsersScreenController.userTable.getColumns().addAll(users);
}
}
catch (SQLException e) {
e.printStackTrace();
}
return userData;
}
}
最后是主
public class Main extends Application {
/**
*
* @param primaryStage
* @throws Exception
*/
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/View/LoginForm.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("SCHEDULER");
primaryStage.show();
}
/**
* runs args then calls closeConn() to close database connections
* @param args
* @throws java.lang.Exception
*/
public static void main(String[] args) throws Exception {
Locale.setDefault(new Locale("fr", "FR"));
System.out.println(Locale.getDefault());
DBConnection.openConnection();
launch(args);
DBConnection.closeConnection();
}
}