找不到'jdbc:mysql:// localhost:3306 / mysql的合适驱动程序 [英] No suitable driver found for 'jdbc:mysql://localhost:3306/mysql
问题描述
使用Java,我在尝试连接到mysql数据库时遇到此错误:
Using Java, I get this error when attempting to connect to a mysql database:
java.sql.SQLException: No suitable driver found for
jdbc:mysql://localhost:3306/mysql at
java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at MyTest1.main(MyTest1.java:28)
我正在使用 mysql-connector-java-5.1.18-bin.jar
driver。它在我的构建路径中。我重启了MySQL。我还从root命令行登录,没有密码,连接正常。我目前没有在netstat中看到端口3306。以前我得到了一个不同的错误(我没有更改代码)。错误是jdbc mysql访问被拒绝用户'root'@'localhost密码NO
I'm using the mysql-connector-java-5.1.18-bin.jar
driver. It is in my build path. I have restarted MySQL. I've also logged on from the command line with root and no password and it connected fine. I'm not currently seeing a port 3306 in netstat. Previously I was getting a different error (I didn't change the code). The error was "jdbc mysql Access denied for user 'root'@'localhost password NO"
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
String url = "jdbc:mysql://localhost:3306/mysql";
Connection con = DriverManager.getConnection(url, "root", "");
}
catch (Exception e){
e.printStackTrace();
}
推荐答案
在这种特殊情况下(假设 Class#forName()
没有抛出异常;你的代码就是继续运行而不是抛出异常),这个 SQLException
表示 驱动程序#restactsURL()
已返回 false
对于任何已加载的驱动程序。
In this particular case (assuming that the Class#forName()
didn't throw an exception; your code is namely continuing with running instead of throwing the exception), this SQLException
means that Driver#acceptsURL()
has returned false
for any of the loaded drivers.
实际上,您的JDBC URL错误:
And indeed, your JDBC URL is wrong:
String url = "'jdbc:mysql://localhost:3306/mysql";
删除单引号:
String url = "jdbc:mysql://localhost:3306/mysql";
参见:
- 关于MySQL + JDBC连接的迷你教程
- Mini tutorial on MySQL + JDBC connectivity
See also:
这篇关于找不到'jdbc:mysql:// localhost:3306 / mysql的合适驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!