简体   繁体   English

ActionListerner中的ClassNotFoundException

[英]ClassNotFoundException in ActionListerner

I have the following error unreported exception ClassNotFoundException in my button action Listener and I'm unsure how i can resolve it, I tried adding Throw ClassNotFoundException but it was still giving me an error when trying to compile, I added it at 我在按钮操作监听器中有以下错误未报告的异常ClassNotFoundException ,我不确定如何解决,我尝试添加Throw ClassNotFoundException但尝试编译时仍给我一个错误,我在添加它

public void actionPerformed(ActionEvent e) throw ClassNotFoundException {
//action listeners for Login in button and menu item
        submitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {

                    DisplaySearch.Search(jtfImagename.getText(), jtfLocation.getText(), jtfTimestamp.getText());

                    String Iname = jtfImagename.getText();
                    String Loc = jtfLocation.getText();
                    String Time = jtfTimestamp.getText();
                    accountReportArea.setText("Search Criteria \n" + "Image name : " + Iname + "\nLocation : " + Loc + "\nTime Stamp : " + Time);                 
                } catch (SQLException ex) {
                    ex.printStackTrace();
                    //    JOptionPane.showMessageDialog(null,
                    //            "Sorry, couldn't check your credentials. Check the logs and report the problem to an administrator.");
                    return;
                }
            }
        });

DisplaySearch {
    // database URL                              

    static final String DATABASE_URL = "jdbc:mysql://localhost:3306/mysql";
    static final String USERNAME = "root";
    static final String PASSWORD = "root";

    // launch the application
    public static void Search(String imageName, String DateStamp, String imageTag) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Connection connection = null; // manages connection
        Statement statement = null; // query statement
        ResultSet resultSet = null; // manages results

        // connect to database books and query database
        try {
            // establish connection to database
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            connection = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);

            // create Statement for querying database
            statement = connection.createStatement();

            // query database                                        
            resultSet = statement.executeQuery(
                    "SELECT imageName, DateStamp, imageTag FROM images");

            // process query results   
            System.out.println("Search Criteria");

            while (resultSet.next()) {
                String Tag = resultSet.getString(imageTag);
                String Date = resultSet.getString(DateStamp);
                String Name = resultSet.getString(imageName);
                System.out.print("Image Name: " + Name);
                System.out.print(", Date: " + Date);
                System.out.print(", Tag: " + Tag);
            } // end while
        } // end try
        catch (SQLException sqlException) {
            sqlException.printStackTrace();
        } // end catch                                                     
        finally // ensure resultSet, statement and connection are closed
        {
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } // end try                                               
            catch (Exception exception) {
                exception.printStackTrace();
            } // end catch                                             
        } // end finally                                              
    } // end main
} // end class

By adding throws ClassNotFoundException you won't solve any problems. 通过添加throws ClassNotFoundException您将无法解决任何问题。 You just indicate to someone who call this function, it could throw such an exception. 您只是向调用此函数的人指出,它可能会抛出这样的异常。

Since you are using Class.forName() you should include com.mysql.jdbc.Driver at run-time. 由于使用的是Class.forName() ,因此应在运行时包含com.mysql.jdbc.Driver Maybe a JAR is missing in your classpath? 也许您的类路径中缺少JAR?

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

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