简体   繁体   English

找不到合适的驱动程序。 Java JDBC mysql连接器

[英]No Suitable Driver Found. Java JDBC mysql connector

Here is my code: 这是我的代码:

import java.sql.*;
import com.mysql.jdbc.Driver;

public class Main {

    public void connect() {
        Connection con = null;


        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql//localhost:3306/mydb", "dev", "pass");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if (con != null) try { con.close(); } catch(Exception e) {}
        }
    }

    public static void main(String[] args) {
        Main m = new Main();

        m.connect();
    }
}

I have imported mysql-connector-java-5.1.39.jar as a library in IntelliJ and as you can see the driver is imported in the code. 我已将mysql-connector-java-5.1.39.jar作为IntelliJ中的库导入,并且您可以看到驱动程序已在代码中导入。

But I get: 但是我得到:

java.sql.SQLException: No suitable driver found for jdbc:mysql//localhost:3306/mydb

I have looked at countless stack overflow questions and I can't figure out what more it needs to at least attempt a connection. 我研究了无数堆栈溢出问题,但我想不出至少要尝试建立连接还需要什么。 I have a local SQL server running, and I've double checked the port and database name. 我有一个本地SQL服务器正在运行,并且我已经仔细检查了端口和数据库名称。

Any ideas would be appreciated, thanks. 任何想法将不胜感激,谢谢。

EDIT: this is not a duplicate. 编辑:这不是重复。 The solution to the other question that someone flagged is not relevant here. 有人标记的另一个问题的解决方案在这里不相关。 I had already added the jar as a library for the project, as described in the other question. 如另一个问题所述,我已经将jar添加为该项目的库。 Also, the crash doesn't happen on the "Class.forName" part, it crashes on the subsequent line. 此外,崩溃不会发生在“ Class.forName”部分,而是会在随后的行中崩溃。

You are missing a colon character. 您缺少冒号字符。

"jdbc:mysql//localhost:3306/mydb"

should be 应该

"jdbc:mysql://localhost:3306/mydb"

See Connecting to MySQL Using the JDBC DriverManager Interface . 请参阅使用JDBC DriverManager接口连接到MySQL

The missing colon means that the driver manager will attempt to look for a driver provider called "mysql//localhost" rather than "mysql" . 缺少冒号意味着驱动程序管理器将尝试查找名为"mysql//localhost"而不是"mysql"的驱动程序提供程序。 Of course, no such provider exists. 当然,不存在这样的提供者。


The Class.forName call in your code is not necessary. 您的代码中的Class.forName调用不是必需的。 But the fact that the call succeeding is a strong clue that the problem is NOT that the driver class is missing. 但是,调用成功的事实可以说明问题并非不是缺少驱动程序类。 It should cause the reader to look for other reasons why DriverManager couldn't find the driver. 它应该使读者查找DriverManager找不到驱动程序的其他原因

jdbc:mysql//localhost:3306/mydb

You are missing a colon after mysql . mysql之后,您缺少冒号。

NB the Class.forName() line hasn't been necessary for ten years. 注意Class.forName()行已经十年没有必要了。

The JDBC connection string for MySQL has been well explained here in the MySQL documentation. MySQL的JDBC连接字符串已经很好地解释这里 MySQL的文档。 Based on that, the mistake that you've done here is a simple miss of colon after the database type, mysql 基于此,您在这里犯的错误是在数据库类型为mysql之后简单地冒号了

You're connection string (without the credentials, though) is going to change from this: 您的连接字符串(尽管没有凭据)将从此更改:

"jdbc:mysql//localhost:3306/mydb"

To this: 对此:

"jdbc:mysql://localhost:3306/mydb"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JDBC JAVA找不到适合的驱动程序jdbc:mysql:// localhost:3306 / voting - JDBC JAVA No suitable driver found for jdbc:mysql://localhost:3306/voting 与 JDBC 的 Java 集成 - 找不到适用于 jdbc 的驱动程序:mysql 错误 - Java-Integration with JDBC - No suitable driver found for jdbc:mysql error MySQL JDBC:找不到合适的驱动程序 - MySQL JDBC: no suitable driver found Java JDBC - 找不到合适的驱动程序 - Java JDBC - No suitable Driver found 在java中没有为jdbc找到合适的驱动程序 - No suitable driver found for jdbc in java 找不到适合jdbc mysql驱动程序的驱动程序 - No suitable driver found for jdbc mysql driver java.sql.SQLException:找不到合适的驱动程序。 已经将连接器jar添加到Tomcat的lib文件夹中 - java.sql.SQLException: No suitable driver found. Already added connector jar to Tomcat's lib folder java.sql.SQLException:找不到适用于jdbc:mysql的驱动程序 - java.sql.SQLException: No suitable driver found for jdbc:mysql 在带有 MySQL 的 IntellJ 中找不到适合 jdbc:mysql 的驱动程序 - No suitable driver found for jdbc:mysql in IntellJ with MySQL java.sql.SQLException:找不到适用于jdbc:mysql // 127.0.0.1:3306 / java错误的驱动程序-NetBeans IDE,JDK 1.8,mysql-connector 8.0.12 - java.sql.SQLException: No suitable driver found for jdbc:mysql//127.0.0.1:3306/java Error - NetBeans IDE, JDK 1.8, mysql-connector 8.0.12
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM