未报告的异常SQLException;必须被捕获或声明为抛出错误 [英] Unreported exception SQLException; must be caught or declared to be thrown error

查看:479
本文介绍了未报告的异常SQLException;必须被捕获或声明为抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这使我可以从数据库中获取数据,将其存储为DefaultTableModel并将该模型传递到GUI上-用数据库数据填充jTable.

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;
}

然后,在我的GUI中,我将以下代码作为字段

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

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

最后,在我的GUI构造函数中,我将datavault jTable的模型设置为从数据库中获取的模型

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

我已尝试在该行上放置try-catch块

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

DefaultTableModel dTableModel = acc.buildTableModel();

DefaultTableModel dTableModel = acc.buildTableModel();

但是错误仍然存​​在.我在这里有什么选择?我知道被调用的方法会引发SQLException,但是为什么当我捕获SQLException时却不想打球?

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?

错误:

推荐答案

我似乎已经通过执行以下操作解决了该问题:

I seemed to have solved it by doing the following:

在我的GUI中,我的字段如下:

within my GUI, my fields are as follows:

Account acc = new Account();
DefaultTableModel dTableModel;

构造函数现在显示为:

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

GUI中的Main方法现在看起来像这样:

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

我不确定这是否正确,但没有其他错误.我的jTable还没有显示数据.我不确定为什么,但是我想这就是解决的问题,我将继续讨论下一个.感谢所有的帮助.

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.

这篇关于未报告的异常SQLException;必须被捕获或声明为抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆