【问题标题】:Why does JDBC work on my computer, but not on my friends'? [duplicate]为什么 JDBC 可以在我的计算机上运行,​​但不能在我朋友的计算机上运行? [复制]
【发布时间】:2021-12-27 02:19:17
【问题描述】:

我们的项目在服务器上有一个数据库。
当我运行程序时,它工作得很好。我可以登录并注册另一个帐户。
但是当我的同学运行代码时,他得到了一个异常:No suitable driver found for jdbc:mysql://ServerIp//DBName

我的代码如下所示:

String hostURL = "jdbc:mysql://ServerIp:3306/Database"; //There's an actual ip of course
Connection con = DriverManager.getConnection(hostURL, "username", "password");
System.out.println("Connected successfully");

我们已经将 mysql-connector 添加到依赖项 (IntelliJ) 中,但它根本没有帮助。
此外,我什至不需要添加它,该项目对我来说工作得很好,根本不需要做任何事情。该项目位于 Java Enterprise -> Web 应用程序中

【问题讨论】:

    标签: java mysql jdbc driver


    【解决方案1】:

    如果 JDBC 无法找到合适的驱动程序,则会发生此错误 传递给 getConnection() 的 URL 格式 方法例如“jdbc:mysql://”在我们的例子中。

    为了解决这个错误, 您需要 MySQL JDBC 驱动程序,例如 类路径中的“mysql-connector-java-5.1.36.jar”。

    或者是因为您忘记在 java 应用程序中注册您的 mysql jdbc 驱动程序。

    Connection con = null;
    try {
        //registering the jdbc driver here, your string to use 
        //here depends on what driver you are using.
        Class.forName("something.jdbc.driver.yourdriver");   
        con = DriverManager.getConnection(hostURL, ...);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
    

    【讨论】:

    猜你喜欢
    • 2020-11-26
    • 2016-09-29
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    相关资源
    最近更新 更多