简体   繁体   English

将 MySQL 数据库连接到 NetBeans 错误

[英]Connecting MySQL Database to NetBeans Error

I have a problem when running my code in NetBeans in order to see if mySQL is connected.在 NetBeans 中运行我的代码以查看 mySQL 是否已连接时遇到问题。 This is the code:这是代码:

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");
    }
  }
}

when I run it prints out "RIP".当我运行它时会打印出“RIP”。 When I debugged it line by line, it went from the "connect = DriverManager.getConnection..." to "System.out.println("RIP"), and when I look at the "Exception e" it says "e = (java.sql.SQLNonTransientConnectionException) java.sql.SQLNonTransientConnectionException: Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the connection string near '=TRUE'."当我一行一行地调试它时,它从“connect = DriverManager.getConnection...”到“System.out.println(“RIP”),当我查看“Exception e”时,它说“e = (java.sql.SQLNonTransientConnectionException) java.sql.SQLNonTransientConnectionException:无法加载连接类,因为底层异常:com.mysql.cj.exceptions.WrongArgumentException:格式错误的数据库 URL,无法解析 '=TRUE' 附近的连接字符串。”

Now, why is that?????现在,这是为什么????

I think you need to add Class.forName("com.mysql.jdbc.Driver");我认为你需要添加Class.forName("com.mysql.jdbc.Driver"); . .

Also, make sure everything in Connection conn = DriverManager.getConnection (String url, String user, String password);另外,请确保Connection conn = DriverManager.getConnection (String url, String user, String password); are set correctly.设置正确。

From the url format in your code, it's like you are trying to get direct connect to specific table tUsers in your database.从代码中的 url 格式来看,就像您试图直接连接到数据库中的特定表tUsers And I dont think that would work.我不认为那会奏效。 Correct me if I'm wrong whether it's literally your database name or not.如果我错了,请纠正我,无论它是否是您的数据库名称。

Because the basic url format i know, should be like jdbc:mysql://localhost:3306/yourDBname .因为我知道的基本url 格式应该像jdbc:mysql://localhost:3306/yourDBname

If you already set the url correctly as is written in your post, then the code is如果您已经按照帖子中所写的方式正确设置了 url,则代码为

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");
}}}

Hope that could do the work.希望可以完成这项工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM