【问题标题】:Connecting MySQL Database to NetBeans Error将 MySQL 数据库连接到 NetBeans 错误
【发布时间】:2018-08-29 10:38:25
【问题描述】:

在 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' 附近的连接字符串。”

现在,为什么会这样??????

【问题讨论】:

  • 我建议从Connection URL Syntax开始
  • 我想知道jdbc:mysql://localhost:3306/tUsers?autoReconnect=true&useSSL=true 是否会更好

标签: java mysql netbeans


【解决方案1】:

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

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

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

因为我知道基本的url format,所以应该像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");
}}}

希望可以完成这项工作。

【讨论】:

  • 感谢您的提示,但是当我添加“Class.forName...”时,它说没有必要,并且“tUsers”是一个方案而不是一个表格。