0

I have put together student record system for a class I am taking that utilizes SQL. Users can simply add, delete, and display student records. In the UI, there are buttons for all of the functions and below the buttons, all of the input student records are displayed. Everything is functioning as intended, however I am running into a small snag that I'm not sure how to address. I'm pulling my hair out because I'm sure it's something very simple.

When there are NO student records in the database and you click either the delete or display buttons, the user is presented with an error message that says "You must first select a student record to delete or display"....functioning as intended.

When there ARE student records in the database and the user selects one of the student records and clicks either the delete or display buttons the record is either deleted or displayed and they function as intended.

However, here is the problem I am actually running into. If there ARE student records displayed and the user DOES NOT select one of them and they hit the delete or display buttons, when it should show the same error message as above, it does nothing at all.

I actually know what the problem is. I set an if..else statement that checks if there are records present at all ( if records > 0 ) and so of course if there are any student records present in the database and the user has not selected one, it doesn't know what to do. I really want to learn this on my own and I'm just looking for some direction.

So the question is: is there a function in Java to see if a row in the database has been selected by the user or not?

Here is a copy of the coding for the "Delete" button:

{
  if (this.studentsTable.getRowCount() > 0)

  {
      int studentID = Integer.parseInt(this.studentsTable.getValueAt(this.studentsTable.getSelectedRow(), 0).toString());
      if (JOptionPane.showConfirmDialog(this, "Are you sure you want to delete the selected student?") == 0)

      {
          Graduate student = new Graduate(studentID, "", "");
          student.delete();
          ((DefaultTableModel)this.studentsTable.getModel()).removeRow(this.studentsTable.getSelectedRow());
      }
  }

  else

  {
      JOptionPane.showMessageDialog(this, "You must first select a student record to delete.");
  }

}

4

0 に答える 0