简体   繁体   中英

Delete Button function to delete selected row from javafx tableView and SQLite table row

I am a newbie working on a Javafx project for the first time and can really use some help and direction. I have read and tried several variations for the correct syntax, methods and suggestions. Some I have used to get the button functions to work, all but one. I am using Netbeans 8.1, I have Java 1.8.0_102 installed, sqlite-jdbc-3.8.11.2 for connection.

I have a TableView with textfields for input (txtUsername, txtPassword, txtWebsite, txtComments) and four buttons Logout, Save, Clear and Delete. Logout, Save and Clear button function perfectly, my problem is with the delete function. When a row is selected and the delete button is pressed it delete's the selected row from the tableview ok, but not from the database table and that is where I am stuck. Any help would be greatly appreciated and welcomed.

List of project files: application.css, background.jpeg, login.fxml, loginController.java, LoginModel.java, Main.java, SqliteConnectioo.java, user.fxml, UserController.java The deleteData function within the UserController.java file:

public void deleteData(ActionEvent event) {        
    int selectedIndex = tableView.getSelectionModel().getSelectedIndex();
    if (selectedIndex >= 0) {
        tableView.getItems().remove(selectedIndex);

    } else {
        // Row Not selected.
        Alert alert = new Alert(AlertType.WARNING);
        alert.setTitle("No Selection");
        alert.setHeaderText("No Username Selected");
        alert.setContentText("Please select a Username in the table.");
        alert.showAndWait();            

    }

}

If you need to see the complete file or any of the other file just let me know and I will post them, thank you in advance for any assistance anyone can give me.

You will need to have a DAO class and I think you might already have one, since your questions hints at having a 'save/add to DB' functionality. IF you dont have this functionality, then try that first, you might learn something and then follow this question. If you have, then look below for the delete functionality.

And please don't ask to know about the add functionality as well, because then it would just solidify the fact that you are asking for an assignment to be done. I am answering this assuming you have tried PreparedStatement instead of Statement.

You will have to add the following method and variable to the DAO class and call this method with identifier from the UserController class.

private final String deleteQuery = "DELETE FROM <TABLE_NAME> WHERE <UNIQUE_COLUMN> = '"; //If you require multiple columns to get a unique row, add them in the where clause.

public void deleteRow(String uniqueIdentifier) {
      Statement stmt = con.createStatement(); //con is the connection object, it should be at SqliteConnectioo class
      stmt.executeUpdate(deleteQuery + uniqueIdentifier + "'");
      con.commit(); //If autocommit is off
}

Also, your TableView's template/datatype class can be retrieved using the following,

table.getSelectionModel().getSelectedItem()

From this, you will be able to get the unique indentifier(s). Your call to this DAO delete method will be as follows.

...
if (selectedIndex >= 0) {
        TableViewDataType tvDt = tableView.getSelectionModel().getSelectedItem() //TableViewDataType is the class which you have used to create the TableView, you can find this at the TableView's declaration.
        dao.deleteRow(tvDt.getTxtUsername());//assuming TxtUsername is the unique identifier
        tableView.getItems().remove(selectedIndex);
    } else {
...

If this is not what you asked for, then please clarify by adding more code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM