将MySQL数据库连接到NetBeans错误

时间:2018-08-29 10:38:25

标签: java 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'附近的连接字符串。 / p>

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

1 个答案:

答案 0 :(得分:0)

我认为您需要添加 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");
}}}

希望可以完成这项工作。