【发布时间】:2014-01-15 05:26:44
【问题描述】:
我有一个 JList,它显示了我添加到数据库中的所有项目。当我想删除多个项目时,比如两个项目,只有其中一个被删除。这是我的代码: 这个方法来自我的 DAL 类:
public void removeStudent(Student student)throws SQLException{
PreparedStatement studentStatement = getConnected().prepareStatement(
"delete from student where spnr = ? and sname = ? and sadress = ? " +
" and stel = ? ");
studentStatement.setString(1, student.getIdNumber());
studentStatement.setString(2, student.getName());
studentStatement.setString(3, student.getAddress());
studentStatement.setString(4, student.getTel());
studentStatement.executeUpdate();
}
类控制器:
public void removeStudent(Student student) throws SQLException{
dal.removeStudent(student);
}
和类视图。单击按钮删除时在 actionListener 中使用。
private void removeStudent(){
try{
controller.getDal().getStudentData().getName();
controller.getDal().getStudentData().getAddress();
controller.getDal().getStudentData().getIdNumber();
controller.getDal().getStudentData().getTel();
controller.removeStudent(controller.getDal().getStudentData());
}catch(Exception e){
e.printStackTrace();
}
}
还有 ActionEvent
if(infLst.getSelectedIndex() > -1){
int choice = JOptionPane.showConfirmDialog(null,
"Delete?" + "The student will be permamently deleted",
null,
JOptionPane.YES_NO_OPTION);
if(choice == JOptionPane.YES_OPTION){
//Removes from the JList
((DefaultListModel)infLst.getModel()).removeElementAt(index);
//And database
removeStudent();
}else{
return;
}
}else{
JOptionPane.showMessageDialog(null, "Please make sure you have selected an item from the list");
}
}}});
请考虑我是数据库编程领域的新手。提前致谢。
【问题讨论】:
标签: java mysql data-access-layer