1

Having done some research here and there, I've disgraced myself at solving a particular problem. I am not too familiar with JTable and was wondering if I could get some suggestions.

I am working with an Model-View-Controller concepts, and I have the following issue.

Here is a section of code from the MODEL class which is error free:

    public TableModel getTableData() throws SQLException {
    try {
        String sql = "SELECT date as 'Date',eventName as 'Name', time as 'Start Time' FROM Event";
        pst = conn.prepareStatement(sql); 
        rs = pst.executeQuery();
        TableModel model = (DbUtils.resultSetToTableModel(rs));
        return model;
        }
   finally {
        try {rs.close(); pst.close(); conn.close(); }
        catch(SQLException e){} 
    }

}

and here is a snippet from VIEW class:

 public EventView() {
    initComponents();
    this.model = new EventModel();
    tableEvent.setModel(model.getTableData());

}

My error lies in the tableEvent.setModel(model.getTableData()); section of the VIEW class where it says an SQL exception must be caught or thrown. From what I understand it is not a good practice to deal with databases in Interface(VIEW CLASS). should be handled by MODEL.

I have 2 options here:

  1. add throws clause to the EventView() constructor itself
  2. Surround statement with try-catch.

But I believe that would add the database imports and concepts to the Interface, which I dont think is a good? What do you guys think, does it matter? alternatively could I initialize my table some other how?

MooHa
  • 819
  • 3
  • 11
  • 23

1 Answers1

0

You should remove the throws SQLException clause from the signature of getTableData function. The SQLException is handled inside that method, so there is no need to throw it and the view doesn't need to know about it.

Bogdan
  • 934
  • 7
  • 13