Here is a section of code from a MODEL class of the MVC Framework:

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){} 
    }

}

I am struggling to get the model of the table and set it to table in VIEW class where my only error seems to be java.sql exception not being caught or thrown. I don't want to be doing this in VIEW class as it is a bad practice. ideally should be done in MODEL. Here is the proportion of the code from VIEW I'm struggling to deal with;

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

    }

How do I adjust my code and set up the table on initialisation appropriately without handing errors on interface, i.e. VIEW Class

Recommended Answers

All 2 Replies

Maybe have it pass the exception

public EventView throws Exception {
    Foo.bar();
}

I wouldn't know specifically what the issue you have is, but if you want the exception to be handled elsewhere throw it upwards.

The view knows nothing about SQL, and so has no idea what to do with an SQL exception. On the other hand, the model knows nothing about how to talkto the user. So normally you would do something like:
1. In the model, catch the exception and write as much technical debugging info as possible to System.err so an SQL expert can try to debug it. Then create new exception that the view can understand, eg new Exception("There has been a technical error, please contact database support for assistance", and throw that to the view.
In the view catch that exception, display the user error message, and either give up or re-try the operation.

See also http://docs.oracle.com/javase/tutorial/essential/exceptions/chained.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.