gpt4 book ai didi

Java - 连接到 MYSQL 数据库 try/catch 的代码出错?

转载 作者:行者123 更新时间:2023-12-02 05:34:50 24 4
gpt4 key购买 nike

我一生都无法弄清楚我的代码中的错误是什么。有人有主意吗?所有错误均来自距底部约 5 行的 catch 语句。

public void connectToDB(){
try {
// this will load the MySQL driver, each DB has its own driver
Class.forName(dbDriver);
// setup the connection with the DB.
connect = DriverManager
.getConnection("jdbc:mysql://localhost/?"
+ "user=root&password=");
ResultSet resultSet = connect.getMetaData().getCatalogs();

//iterate each catalog in the ResultSet
while (resultSet.next()) {
// Get the database name, which is at position 1
String databaseName = resultSet.getString(1);
if (databaseName.equals("Ballers")) {
preparedStatement = connect.prepareStatement("DROP DATABASE Ballers");
preparedStatement.execute();
}
}
ScriptRunner runner = new ScriptRunner(connect, false, false);
try {
runner.runScript(new BufferedReader(new FileReader(dumpPath)));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} catch (ClassNotFoundException | SQLException e) {
System.out.println("not connecting");
// TODO Auto-generated catch block
e.printStackTrace();
}

}

错误内容如下:

DatabaseHelper.java:64: <identifier> expected
} catch (ClassNotFoundException | SQLException e) {
^
DatabaseHelper.java:64: '{' expected
} catch (ClassNotFoundException | SQLException e) {
^
DatabaseHelper.java:64: not a statement
} catch (ClassNotFoundException | SQLException e) {
^
DatabaseHelper.java:64: ';' expected
} catch (ClassNotFoundException | SQLException e) {

需要什么标识符?这不是一个有效的陈述吗?

最佳答案

您必须拥有旧版本的 Java

从 Java 7 开始支持多个异常捕获。您必须单独捕获它们。

转换

} catch (ClassNotFoundException | SQLException e) {
System.out.println("not connecting");
// TODO Auto-generated catch block
e.printStackTrace();
}

} catch (ClassNotFoundException  e) {
System.out.println("not connecting");
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch ( SQLException e) {
System.out.println("not connecting");
// TODO Auto-generated catch block
e.printStackTrace();
}

关于Java - 连接到 MYSQL 数据库 try/catch 的代码出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25087067/

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