gpt4 book ai didi

java - ActionListerner 中的 ClassNotFoundException

转载 作者:行者123 更新时间:2023-12-01 14:02:50 25 4
gpt4 key购买 nike

我的按钮操作监听器中有以下错误未报告的异常 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

最佳答案

通过添加抛出ClassNotFoundException,您不会解决任何问题。您只需向调用此函数的人表明,它可能抛出这样的异常。

由于您使用的是Class.forName(),因此您应该在运行时包含com.mysql.jdbc.Driver。也许您的类路径中缺少 JAR?

关于java - ActionListerner 中的 ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19207644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com