簡體   English   中英

發現此錯誤“沒有找到適合 jdbc:mysql//localhost:3306/student 的驅動程序”

[英]Found this error "No suitable driver found for jdbc:mysql//localhost:3306/student"

我試圖在IntelliJ IDEA中創建簡單的 java 學生GUI應用程序,並且工作正常。 然后我只是嘗試連接數據庫以將所有數據插入 MYSQL,所以我在MySQL Workbatch中創建模式( student )和表( appointment ),然后在IntelliJ IDEA Database Z05B8C74CBD96FBF2DE4C1A3527 中連接該數據庫,您可以在圖片中看到它成功配置的 FBF2DE4C1A3527 .

現在我嘗試通過下面的行創建連接。

Connection con = DriverManager.getConnection("jdbc:mysql//localhost:3306/student","root","root");

但是這一行給了我這個錯誤Error: No suitable driver found for jdbc:mysql//localhost:3306/student

所以我谷歌它,我找到了這條線。

Class.forName("com.mysql.cj.jdbc.Driver");

但是這條線給出了這個錯誤Error: com.mysql.cj.jdbc.Driver

請大家看看它,它對我和其他面臨這個問題的人非常有幫助。

在此處輸入圖像描述

 private DBconnectionSingleton() throws SQLException {
    // create single instance of DB connection 
    Properties connectionProps = new Properties();
    connectionProps.put("user", login);
    connectionProps.put("password", password);// port & db name
    System.out.println("Loading driver...");


    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        System.out.println("Driver loaded!");
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException("Cannot find the driver in the classpath!", e);
    }

    conn = DriverManager.getConnection(
            "jdbc:mysql://localhost:3306/student",
            connectionProps
    );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM