大家好。为什么我可以连接到数据库的连接connection = getConnection(URL,LOGIN,PASSWORD)。?这个连接是工作。逻辑上在Java中这个连接不应该工作。你需要写连接conn = drivermanager.getConnection(URL,LOGIN,PASSWORD)
private static void displayAuthorAndBooksWherePagesMore300() throws SQLException {
Connection connection = null;
Statement stm = null;
ResultSet rs = null;
try {
connection = getConnection(URL, LOGIN, PASSWORD);
connection.setAutoCommit(false);
stm = connection.createStatement();
rs = stm.executeQuery("select DISTINCT a1.name AS Author, a1.surname, a2.title AS Book from author a1\n" +
"join book a2 on a1.id = a2.author_id\n" +
"where a2.pages > 100");
while (rs.next()) {
String str = rs.getString("Author") + " " + rs.getString("surname") + " "
+ rs.getString("Book");
System.out.println(str);
}
} catch (SQLException | RuntimeException e) {
System.err.println(e);
logger.error(e);
} finally {
rs.close();
stm.close();
connection.close();
logger.info("Resources are closed.");
}
}
1条答案
按热度按时间ldioqlga1#
有趣的问题。我也遇到过这个问题,我不知道如何解决它(