繁体   English   中英

使用JButton通过JDBC执行SQL查询

[英]Using JButtons to execute SQL queries with JDBC

我正在编写一个简单的JAVA GUI,以从JTextFrame读取SQL查询并执行它。

连接和执行按钮都是JButton,但是编译器不会编译我的代码,因为我无法在“侦听器”私有类中的actionPerformed后面附加“ throws SQLException”。 我尝试编写单独的方法,但是相同的问题仍然存在。 这是一个例子:

public void connect() throws SQLException {
    conxn = DriverManager.getConnection(URL, Username, Password);
}
private class SelectBut implements ActionListener {  
    public void actionPerformed(ActionEvent event){
        connect();
    }
}

编译器只是把这个扔给我:

TextFrame.java:123: unreported exception java.sql.SQLException; must be caught or declared to be thrown  
public void actionPerformed(ActionEvent event){connect();}}

有什么建议么?

由于SQLException是检查异常,因此必须重新抛出或捕获它。

在您的情况下,您的actionPerformed方法可以是这样的:

public void actionPerformed(ActionEvent event){
    try{
         connect();
    }catch(SQLException e){
         e.printStackTrace();
     }
 }

这里是有关捕获和处理异常的教程

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM