首页 > 解决方案 > 将 MySQL 数据库连接到 NetBeans 错误

问题描述

在 NetBeans 中运行我的代码以查看 mySQL 是否已连接时,我遇到了问题。这是代码:

public static void main(String[] args) {
    Connection connect = null;

    try{
        connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/tUsers?autoReconnect=true/useSSL=TRUE","root","password");
        if(connect!=null)
        {
        System.out.println("Connected");
        }
    }catch (Exception e)
    {
        System.out.println("RIP");
    }
  }
}

当我运行它时,它会打印出“RIP”。当我逐行调试它时,它从“connect = DriverManager.getConnection ...”变为“System.out.println(“RIP”),当我查看“Exception e”时,它显示“e = (java.sql.SQLNonTransientConnectionException)java.sql.SQLNonTransientConnectionException:由于基础异常而无法加载连接类:com.mysql.cj.exceptions.WrongArgumentException:数据库 URL 格式错误,无法解析 '=TRUE' 附近的连接字符串。”

现在,这是为什么呢????

标签: javamysqlnetbeans

解决方案


我认为您需要添加 Class.forName("com.mysql.jdbc.Driver");.

另外,请确保所有内容Connection conn = DriverManager.getConnection (String url, String user, String password);都正确设置。

从代码中的 url 格式来看,就像您试图直接连接到tUsers数据库中的特定表一样。而且我认为这行不通。如果我错了,请纠正我,无论它是否是您的数据库名称。

因为我知道的基本url 格式应该是jdbc:mysql://localhost:3306/yourDBname.

如果您已经按照帖子中的说明正确设置了网址,则代码为

public static void main(String[] args) {

try{
    Class.forName("com.mysql.jdbc.Driver"); 
    Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/tUsers?autoReconnect=true/useSSL=TRUE","root","password");
    if(connect!=null)
    {
    System.out.println("Connected");
    }
}catch (Exception e)
{
    System.out.println("RIP");
}}}

希望可以完成这项工作。


推荐阅读