【问题标题】:No suitable driver found for jdbc:mysql//localhost:3306/Test in Eclipse在 Eclipse 中找不到适合 jdbc:mysql//localhost:3306/Test 的驱动程序
【发布时间】:2019-08-14 00:10:35
【问题描述】:

我知道这个问题被问了好几次,但答案总是将 .jar 添加到构建路径中。我已经这样做了,但仍然得到“没有为 jdbc:mysql//localhost:3306/Test 找到合适的驱动程序”异常。

还有什么其他的想法可能是错的吗?我添加了一些镜头和我的代码来测试连接。

干杯!

已经在 lib 文件夹中添加了“mysql-connector-java-8.0.15.jar”并且 构建路径see settings here

package jdbcdemo;

import java.sql.*;

public class Driver {

    public static void main(String[] args) {

        try {   

        Connection myConn = DriverManager.getConnection("jdbc:mysql//localhost:3306/Test","root","password");

        Statement myStmt = myConn.createStatement();

        ResultSet myRs = myStmt.executeQuery("select * from TestTable");

        while (myRs.next()) {
            System.out.println(myRs.getString("Name"));
        }
        }


        catch (Exception exc) {
            exc.printStackTrace();
    }
    }
}

【问题讨论】:

    标签: mysql eclipse mysql-connector


    【解决方案1】:

    在创建连接之前,您必须注册驱动程序。你可以在这里找到一个例子:https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-usagenotes-connect-drivermanager.html

    或者在创建连接之前添加这一行:

    Class.forName("com.mysql.jdbc.Driver").newInstance();
    

    【讨论】:

      猜你喜欢
      • 2012-05-08
      • 1970-01-01
      • 2017-09-20
      • 2016-10-01
      • 2014-07-28
      • 1970-01-01
      • 1970-01-01
      • 2012-05-27
      • 2013-05-09
      相关资源
      最近更新 更多