我尝试做学校管理应用程序,我已经完成了“添加学生”操作和“删除学生”操作,但是无法弄清楚如何进行“更新操作”,这是我的addStudent和updateStudent
@FXML
private void addStudent(javafx.event.ActionEvent event){
String sqlInsert = "INSERT INTO students (id,fname,lname,email,DOB)
VALUES (?,?,?,?,?)";
try{
Connection conn = dbConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(sqlInsert);
stmt.setString(1, this.id.getText());
stmt.setString(2, this.firstname.getText());
stmt.setString(3, this.lastname.getText());
stmt.setString(4, this.email.getText());
stmt.setString(5, this.dob.getEditor().getText());
stmt.execute();
conn.close();
}catch (SQLException e){
e.getMessage();
}
}
@FXML
private void updateStudent(javafx.event.ActionEvent event){
String sqlUpdate = "UPDATE students SET fname=?, lname=?, email=?, DOB=? WHERE id=?"; /*setid=?...*/ /*VALUES(?,?,?,?,?)*/
try{
Connection conn = dbConnection.getConnection();
PreparedStatement stat = conn.prepareStatement(sqlUpdate);
stat.setString(1,this.id.getText());
stat.setString(2,this.firstname.getText());
stat.setString(3,this.lastname.getText());
stat.setString(4,this.email.getText());
stat.setString(5,this.dob.getEditor().getText());
stat.execute();
conn.close();
}catch(SQLException e){
e.getMessage();
}
}