我试图找出如何使用删除按钮来从sqlite数据库删除数据。我设法从tableview中删除一行。
我设法成功删除数据的唯一方法是指定查询要删除哪一行。所以问题是我做错了什么?
以下是我遇到问题的代码部分:
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<PacientiData> ptable;
@FXML
private TableColumn<PacientiData, String> idcolumn;
@FXML
private TableColumn<PacientiData, String> firstnamecolumn;
@FXML
private TableColumn<PacientiData, String> lastnamecolumn;
@FXML
private TableColumn<PacientiData, String> emailcolumn;
@FXML
private TableColumn<PacientiData, String> dobcolumn;
private dbConnection dc;
private ObservableList<PacientiData> 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<PacientiData, String>("ID"));
this.firstnamecolumn.setCellValueFactory(new PropertyValueFactory<PacientiData, String>("firstName"));
this.lastnamecolumn.setCellValueFactory(new PropertyValueFactory<PacientiData, String>("lastName"));
this.emailcolumn.setCellValueFactory(new PropertyValueFactory<PacientiData, String>("email"));
this.dobcolumn.setCellValueFactory(new PropertyValueFactory<PacientiData, String>("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();
}
}
}
答案 0 :(得分:2)
您的问题是什么
此代码存在问题,因为永远不会指定要删除的id的值:
Statement statement = null;
statement = conn.createStatement();
String sqldelete =" DELETE FROM pacienti WHERE id = ? ";
statement.executeUpdate(sqldelete);
如果您没有告诉它要删除什么,它怎么可能知道删除什么?
我可能无法验证其他问题,因为我无法在您的数据库上试用您的完整程序,但至少以上是一个问题。
如何解决
您应该使用PreparedStatement并设置ID:
PreparedStatement statement = conn.prepareStatement(
"DELETE FROM pacienti WHERE id = ?"
);
statement.setInt(1, id);
statement.executeUpdate();
id
的值应该是什么,从您的示例中不清楚。我的猜测是你会使用类似下面的东西,假设你的模型类中存在适当的id值和存取方法:
Student selectedStudent = studenttable.getSelectionModel().getSelectedItem();
int id = selectedStudent.getId();
关于提议的解决方案的几点说明
我使用int
因为我不知道您的模型如何表达ID,但如果可能是大量学生或long
,您可以使用String
如果ID是UUID。
如果没有选择任何内容(因为没有要删除的内容),您不希望删除运行,那么在初始化时,您可以将删除按钮的禁用属性绑定到所选项目列表。
deleteButton.disableProperty().bind(
Bindings.isEmpty(studenttable.getSelectionModel().getSelectedItems())
);
我提供的代码用于SingleSelectionModel,其中一次只选择一个项目进行删除。如果使用MultipleSelectionModel,则需要使用getSelectedItems()而不是getSelectedItem(),并确保删除所有选定的项目,而不是仅删除一个。