没有找到适合 'jdbc:mysql://localhost:3306/mysql 的驱动程序 [英] No suitable driver found for 'jdbc:mysql://localhost:3306/mysql

查看:38
本文介绍了没有找到适合 '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 驱动程序.它在我的构建路径中.我已经重新启动了 MySQL.我还使用 root 用户且没有密码从命令行登录,并且连接正常.我目前在 netstat 中没有看到端口 3306.以前我遇到了不同的错误(我没有更改代码).错误是jdbc mysql Access denied for user 'root'@'localhost password 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意味着Driver#acceptsURL() 已返回 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 连接迷你教程
  • 这篇关于没有找到适合 'jdbc:mysql://localhost:3306/mysql 的驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆