1

This allows me to grab data from my database, store it as a DefaultTableModel and pass that model onto my GUI - populating my jTable with database data.

This is the method within my Account class:

public static DefaultTableModel buildTableModel() throws SQLException {
    Vector<Vector<Object>> data = new Vector<Vector<Object>>();
    Vector columns = new Vector();
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        String stmt = "SELECT * FROM APP.DATAVAULT";
        ps = Main.getPreparedStatement(stmt);
        rs = ps.executeQuery();
        ResultSetMetaData metaData = rs.getMetaData();

        // names of columns
        Vector<String> columnNames = new Vector<String>();
        int columnCount = metaData.getColumnCount();
        for (int column = 1; column <= columnCount; column++) {
            columnNames.add(metaData.getColumnName(column));
        }
        // data of the table
        while (rs.next()) {
            Vector<Object> vector = new Vector<Object>();
            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                vector.add(rs.getObject(columnIndex));
            }
            data.add(vector);
        }

    } finally {
        try {
            ps.close();
        } catch (Exception e) {
        }
        try {
            rs.close();
        } catch (Exception e) {
        }
    }

    DefaultTableModel tableModel = new DefaultTableModel(data, columns);
    return tableModel;
}

Then, within my GUI I have the following code as fields

Account acc = new Account();
DefaultTableModel dTableModel = acc.buildTableModel();

Lastly, within my GUI constructor, I am setting my datavault jTable's model to the model I have got from the database

public SPPMainGUI() {
    initComponents();
    datavaultjTable.setModel(dTableModel);
}

I have tried putting a try-catch block around the line

DefaultTableModel dTableModel = acc.buildTableModel();

But the error still exists. What are my options here? I understand that my method which is being called throws an SQLException, but why when I catch an SQLException it doesn't want to play ball?

Edit: The error: enter image description here

John Vasiliou
  • 977
  • 7
  • 27
  • 48
  • Are you sure its the same error that persists? Or does the error change to `dTableModel cannot be resolved to a variable`? – Perception Feb 28 '13 at 20:29
  • It seems to always be the same - I have added an image of the error for further clarification. I feel that if I solve this I may get another error like what you have asked above because I'm kind of swinging this code and not sure if it will work. – John Vasiliou Feb 28 '13 at 20:31
  • 1
    Well, that particular code isn't currently handling the exception. Try this - `DefaultTableModel dTableModel = null; try { dTableModel = acc.buildTableModel(); } catch(SQLException sqle) { // handle error }`. – Perception Feb 28 '13 at 20:34
  • Still the same error, with another error above the catch block cannot find SQLException e and ; required. – John Vasiliou Feb 28 '13 at 20:37
  • Make sure to format the code so the comment `//handle error` isn't on the same line as the ending brace. Also, make sure to import java.sql.SQLException. – Perception Feb 28 '13 at 20:46
  • I have done both of those. import java.sql.SQLException; and I have removed the commented //handle error line and replaced it with a simple System.out.println(sqle); – John Vasiliou Feb 28 '13 at 20:48
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25315/discussion-between-perception-and-john-vasiliou) – Perception Feb 28 '13 at 21:02

1 Answers1

0

I seemed to have solved it by doing the following:

within my GUI, my fields are as follows:

Account acc = new Account();
DefaultTableModel dTableModel;

The constructor now shows as:

public SPPMainGUI() throws SQLException {
    this.dTableModel = Account.buildTableModel();
    initComponents();
    datavaultjTable.setModel(dTableModel);
}

My Main method within my GUI now looks like so:

public static void main(String args[]) {
/* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                new SPPMainGUI().setVisible(true);
            } catch (SQLException ex) {
                Logger.getLogger(SPPMainGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
}

I'm not sure if this is correct but there are no more errors. My jTable does not display data ... yet. I'm not sure why but I guess that's this problem solved and I'm onto the next one. Thanks to all that helped.

John Vasiliou
  • 977
  • 7
  • 27
  • 48
  • 1
    I depends. Error handling is a fickle thing. Are you likely to call `buildTableModel` more then once in the life cycle of your app? If so, then you might want to handle the error at a earlier stage. If this method is only called once and (as it would seem) is critical for the application, then where it is handled is probably okay. I would probably use a `JOptionPane` to show the user that something has gone wrong, rather then leaving them thinking "where did the application go?" – MadProgrammer Feb 28 '13 at 22:09