作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一生都无法弄清楚我的代码中的错误是什么。有人有主意吗?所有错误均来自距底部约 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/
我正在开发一个 voip 调用应用程序。我需要做的是在接到来电时将 Activity 带到前台。我在应用程序中使用 Twilio,并在收到推送消息时开始调用。 问题是我试图在接到任何电话时显示 Act
我是一名优秀的程序员,十分优秀!