[英]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.