0

当我单击按钮时,我试图让代码运行,但是它告诉我我需要捕获异常,但我认为我已经做到了。我错过了什么吗?这似乎很简单,但我似乎无法弄清楚。我在第 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); 
 }

} 
} 
4

4 回答 4

3

actionPerformed()方法具有 JDBC 语句,可能会抛出SQLException. 您必须在该方法中处理异常。它不在try您创建匿名内部类的块中。

于 2013-11-12T22:24:16.460 回答
1

这是相关的问题:

new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
            while (rset.next ()) {...}
    }

}

这是一个新类,并且该函数正在从没有封闭上下文的 try catch 块的函数上下文中调用 rset,并且不会从函数中抛出异常。

将该代码更改为适当的代码,但这将使您继续前进:

new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        try {
            while (rset.next()) {...}
        } catch (SQLException ex) {
          // do something cool
        }
    }
}
于 2013-11-12T22:36:33.277 回答
0

也使用下面的 Catch 子句,那么它肯定会捕获你的异常。抛出的异常可能不是 SQLException,因此它不会被捕获

catch (Exception e)
 {
    System.out.println ("Exception Thrown: "+e); 
 }
于 2013-11-12T22:26:26.027 回答
0

Your code seems to be catching the Exception correctly. Are you sure you really have an SQL exception? You can also add this:

catch (Throwable t) {
   t.printStackTrace();
}

right after your catch block. And run your code again. This will catch whatever exception that is thrown and will print the stack trace.

But I bet you are already seeing that on your console, aren't you? Can you copy the stack trace as well?

于 2013-11-12T22:27:25.173 回答