简体   繁体   中英

Connecting MySQL Database to NetBeans Error

I have a problem when running my code in NetBeans in order to see if mySQL is connected. 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". 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'."

Now, why is that?????

I think you need to add Class.forName("com.mysql.jdbc.Driver"); .

Also, make sure everything in 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. 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 .

If you already set the url correctly as is written in your post, then the code is

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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