【问题标题】:Unable to connect to mysql using jbdc driver无法使用 jdbc 驱动程序连接到 mysql
【发布时间】:2018-01-05 17:52:22
【问题描述】:

我正在尝试访问 mysql 数据库,但出现以下错误:

No suitable driver found for jdbc:mysql//localhost:3306/...

我知道已经有人问过这个问题,但我还没有解决现有主题中的解决方案的问题。我重用了 2 年前工作的连接类。

我更换了我的电脑并重新安装了 wamp 和 eclipse,但具有良好构建路径配置的完全相同的类不起作用。这是我用来连接的代码:

Connection connection = null;
public java.sql.Statement stmt = null;
public java.sql.PreparedStatement prStmt;
public ResultSet rs = null;

public DbConnex(String sgbd, int port, String dbName, String user, String pass){

    String DbInfo = String.format("jdbc:%s//localhost:3306/%s", sgbd, dbName);//jdbc:mysql://address=(protocol=tcp)(host=localhost)(port=3306)/nekogame
                                                                              //jdbc:%s//localhost:3306/%s
    //test trouver le driver
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        System.out.println("Where is your MySQL JDBC Driver?");
        e.printStackTrace();
        return;
    }

    System.out.println("MySQL JDBC Driver Registered!");

    //test connexion
    try {
        connection = DriverManager
        .getConnection(DbInfo, user, pass);

    } catch (SQLException e) {
        System.out.println("Connection Failed! Check output console");
        e.printStackTrace();
        return;
    }

    if (connection != null) {
        System.out.println("it's all good!");
    } else {
        System.out.println("Failed to make connection :( !");
    }
 }

这是输出,表明我的驱动程序已被识别:

"MySQL JDBC Driver Registered! Connection Failed! Check output console"

问题似乎发生在getConnection 上。我验证了参数,并下载了最新的驱动程序。我也有最新的 wamp,应该嵌入最新的 MySQL。现在我不知道该怎么办了。

【问题讨论】:

  • 请原谅我的提问,但是,您当前使用的是什么 MySql 版本,什么 IDE,(您是否更改了以管理员身份连接到 MySql 的密码 - 根)?
  • 您对 JDBC 子协议的参数化没有多大意义,//localhost:3306/ 将您绑定到 MySQL(因为 3306 是默认的 MySQL 端口),或者具有类似 URL 格式的数据库和在(对于该数据库的)非默认端口 3306 上运行。如果您参数化,则参数化整个 JDBC url,除了 jdbc:<subprotocol>:-prefix,前缀之后的所有内容都是特定于驱动程序的。

标签: java mysql jdbc connection-string mysql-connector


【解决方案1】:

没有找到适合 jdbc:mysql//localhost:3306/ 的驱动

虽然连接字符串正确,但您在 mysql 之后错过了冒号 (:)。所以,应该是

jdbc:mysql://localhost:3306/<dbname>

所以,你需要在这里做出改变

String.format("jdbc:%s//localhost:3306/%s", sgbd, dbName);

应该是

String.format("jdbc:%s://localhost:3306/%s", sgbd, dbName);

仅供参考,请参考official page for connection string

【讨论】:

  • 不错,@Ravi,我什至没有注意到缺少冒号。
猜你喜欢
  • 2014-07-28
  • 2021-05-18
  • 2016-09-07
  • 2017-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-22
  • 1970-01-01
相关资源
最近更新 更多