连接到Java中的MySQL数据库

ntjbwcob  于 2023-03-17  发布在  Mysql
关注(0)|答案(1)|浏览(134)

大家好。为什么我可以连接到数据库的连接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.");
    }
}
ldioqlga

ldioqlga1#

有趣的问题。我也遇到过这个问题,我不知道如何解决它(

相关问题