title: Deleting data from sqlite database from JavaFX GUI
body: <p>I'm trying to figure out how to make a delete button to work that deletes data from a sqlite database.I managed to delete a row from the tableview.</p>
  
  <p>The only way I managed to successfully delete data is to specify the query which row to delete.So the question is what I'm doing wrong?</p>
  
  <p>Here is the part of the code that I'm having problem with:</p>
  
  <pre><code>package Medic;
  
  import dbUtil.dbConnection;
  import javafx.collections.FXCollections;
  import javafx.collections.ObservableList;
  import javafx.event.ActionEvent;
  import javafx.fxml.FXML;
  import javafx.fxml.Initializable;
  import javafx.scene.control.*;
  import javafx.scene.control.cell.PropertyValueFactory;
  import javafx.stage.Stage;
  
  
  import java.net.URL;
  import java.sql.*;
  import java.util.ResourceBundle;
  
  public class MedicController implements Initializable {
      @FXML
      private Button deleteButton;
      @FXML
      private Button closeButton;
      @FXML
      private TextField id;
      @FXML
      private TextField firstname;
      @FXML
      private TextField lastname;
      @FXML
      private TextField email;
      @FXML
      private DatePicker dob;
  
      @FXML
      private TableView&lt;PacientiData&gt; ptable;
  
      @FXML
      private TableColumn&lt;PacientiData, String&gt; idcolumn;
      @FXML
      private TableColumn&lt;PacientiData, String&gt; firstnamecolumn;
      @FXML
      private TableColumn&lt;PacientiData, String&gt; lastnamecolumn;
      @FXML
      private TableColumn&lt;PacientiData, String&gt; emailcolumn;
      @FXML
      private TableColumn&lt;PacientiData, String&gt; dobcolumn;
  
  
      private dbConnection dc;
      private ObservableList&lt;PacientiData&gt; data;
      private String sql = "SELECT * FROM pacienti";
  
  
      @Override
      public void initialize(URL location, ResourceBundle resources) {
  
          this.dc = new dbConnection();
      }
  
      @FXML
      private void loadStudentData(ActionEvent actionEvent) {
          try {
              Connection conn = dbConnection.getConnection();
  
              this.data = FXCollections.observableArrayList();
  
              ResultSet rs = conn.createStatement().executeQuery(sql);
  
              while (rs.next()) {
                  this.data.add(new PacientiData(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5)));
              }
          } catch (SQLException ex) {
              System.out.println("Error " + ex);
          }
          this.idcolumn.setCellValueFactory(new PropertyValueFactory&lt;PacientiData, String&gt;("ID"));
          this.firstnamecolumn.setCellValueFactory(new PropertyValueFactory&lt;PacientiData, String&gt;("firstName"));
          this.lastnamecolumn.setCellValueFactory(new PropertyValueFactory&lt;PacientiData, String&gt;("lastName"));
          this.emailcolumn.setCellValueFactory(new PropertyValueFactory&lt;PacientiData, String&gt;("email"));
          this.dobcolumn.setCellValueFactory(new PropertyValueFactory&lt;PacientiData, String&gt;("DOB"));
  
          this.ptable.setItems(null);
          this.ptable.setItems(this.data);
  
      }
  
      @FXML
      private void addStudent(ActionEvent actionEvent) {
          String sqlInsert = "INSERT INTO pacienti(id,fname,lname,email,DOB) VALUES(?,?,?,?,?)";
          try {
  
              Connection conn = dbConnection.getConnection();
              PreparedStatement statement = conn.prepareStatement(sqlInsert);
              statement.setString(1, this.id.getText());
              statement.setString(2, this.firstname.getText());
              statement.setString(3, this.lastname.getText());
              statement.setString(4, this.email.getText());
              statement.setString(5, this.dob.getEditor().getText());
              statement.execute();
              conn.close();
  
  
          } catch (SQLException e) {
              e.printStackTrace();
          }
      }
  
      @FXML
      private void clearFields(ActionEvent actionEvent) {
          this.id.setText("");
          this.firstname.setText("");
          this.lastname.setText("");
          this.email.setText("");
          this.dob.setValue(null);
      }
  
  
      @FXML
      public void handleCloseButtonAction(ActionEvent event) {
          Stage stage = (Stage) closeButton.getScene().getWindow();
          stage.close();
      }
  
  
      @FXML
  
      public void deleteButton(ActionEvent event) {
          Connection conn = null;
          Statement statement = null;
  
          try {
              Class.forName("org.sqlite.JDBC");
              conn = DriverManager.getConnection("jdbc:sqlite:database.sqlite");
              conn.setAutoCommit(false);
              System.out.println("Opened database succesfully");
              int selectedIndex = ptable.getSelectionModel().getSelectedIndex();
              ptable.getItems().remove(selectedIndex);
              statement = conn.createStatement();
              String sqldelete =" DELETE FROM pacienti WHERE id = ? ";
              statement.executeUpdate(sqldelete);
              conn.commit();
  
              ResultSet rs = statement.executeQuery("SELECT * FROM pacienti ");
  
              while (rs.next() ) {
                  int id = rs.getInt("id");
                  String fname = rs.getString("fname");
                  String lname = rs.getString("lname");
                  String email = rs.getString("email");
                  String DOB = rs.getString("DOB");
              }
  
              rs.close();
              statement.close();
              conn.close();
          }
          catch (Exception e ) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/pHOtd.png" rel="nofollow noreferrer">enter image description here</a></p>