首页 > 解决方案 > 使用 IntelliJ 连接到 mysql 数据库

问题描述

我正在尝试创建一个第一次使用数据库的程序。我一直在关注本指南,但在第 3.1 步,我不想每次我想启动程序时都通过控制台,所以我开始研究如何通过 IntelliJ 进行操作。Jetbrains 官方文档(以及我找到的所有其他来源)说我应该进入数据库工具窗口(查看 | 工具窗口 | 数据库),但是,数据库并没有作为选项出现在该菜单中

我正在尝试运行以下代码


public class JdbcSelectTest {
    public static void main(String[] args) {
        try (
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ebookshop?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC",
                        "stefan", "xxxx");
                Statement stmt = conn.createStatement()
                ) {
            String strSelect = "select title, price, qty from books";
            System.out.println("The SQL statement is: " + strSelect + "\n");

            ResultSet rset = stmt.executeQuery(strSelect);
            System.out.println("The records selected are:");
            int rowCount = 0;
            while (rset.next()) {
                String title = rset.getString("title");
                double price = rset.getDouble("price");
                int qty = rset.getInt("qty");
                System.out.println(title + ", " + price + ", " + qty);
                ++rowCount;
            }
            System.out.println("Total number of records = " +  rowCount);

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}

导致此错误:

C:\Users\Stefan\.jdks\openjdk-14.0.1\bin\java.exe "-javaagent:N:\IntelliJ IDEA Community Edition 2020.1.1\lib\idea_rt.jar=59866:N:\IntelliJ IDEA Community Edition 2020.1.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\Stefan\IdeaProjects\SQL select\out\production\SQL select" JdbcSelectTest
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/ebookshop?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
    at JdbcSelectTest.main(JdbcSelectTest.java:6)

Process finished with exit code 0

标签: javamysqlintellij-idea

解决方案


推荐阅读