单击按钮时,我试图运行代码,但是这告诉我需要捕获异常,但是我认为我已经这样做了。我想念什么吗?看起来很简单,但我似乎无法弄清楚。我在线路76-83上收到此错误

class EmpList {

private static JFrame frame;
private static JTextField textField;

public static void main (String args [])
  throws SQLException {

DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());

final String user, pass, query;

user = "asdf";
pass = "asdf";




query = "select * from customers";

try
{

        Connection conn = DriverManager.getConnection
        ("jdbc:oracle:thin:@Picard2:1521:itec2",user,pass);
        final Statement stmt = conn.createStatement ();
        final ResultSet rset = stmt.executeQuery (query);


        EmpList window = new EmpList();

        frame = new JFrame();
        frame.setBounds(100, 100, 630, 470);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        final JTextArea textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setLineWrap(true);
        textArea.setBounds(10, 179, 594, 241);
        frame.getContentPane().add(textArea);

        textField = new JTextField();
        textField.setBounds(255, 69, 86, 20);
        frame.getContentPane().add(textField);
        textField.setColumns(10);

        JLabel lblEnterCustomerId = new JLabel("Enter Customer ID (1-6)");
        lblEnterCustomerId.setBounds(240, 43, 153, 14);
        frame.getContentPane().add(lblEnterCustomerId);

        JButton btnGetInfo = new JButton("Get Info");
        btnGetInfo.setBounds(255, 115, 89, 23);
        frame.getContentPane().add(btnGetInfo);

        btnGetInfo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

                while (rset.next ())
                    {

                        textArea.append((rset.getString("CUSTID") + "  -  " + rset.getString("CUSTSSN")
                        + "  -  " + rset.getString("LNAME") + "  -  " + rset.getString("FNAME") + "  -  " +
                        rset.getString("STREET") + "  -  " + rset.getString("CITY") + "  -  " + rset.getString("STATE") +
                        "  -  " + rset.getString("ZIP") + "  -  " + rset.getString("PHONE") + System.getProperty("line.separator")
                        + System.getProperty("line.separator")));

                    }


            }

        });


        window.frame.setVisible(true);


 }


我以为这会捕获异常,但我想不会。

 catch (SQLException e)
 {
    System.out.println ("Could not load the db"+e);
 }

}
}

最佳答案

actionPerformed()方法具有可能引发SQLException的JDBC语句。您必须在该方法内处理异常。在其中创建匿名内部类的try块未涵盖此内容。

关于java - 我的Java程序中出现SQLException错误,不确定为什么,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19941157/

10-09 12:49