Unreported exception SQLException; must be caught or declared to be thrown error

落爺英雄遲暮 提交于 2019-12-24 10:56:16

问题


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:


回答1:


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.



来源:https://stackoverflow.com/questions/15144715/unreported-exception-sqlexception-must-be-caught-or-declared-to-be-thrown-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!