简体   繁体   中英

JDBC Connection to MySql error - Access denied for user 'root'@'localhost' (using password: NO)

I get an error when trying to connect with my MySql, here is my code:

String url = "jdbc:mysql://localhost:3306/new_schema";
Connection conn = DriverManager.getConnection(url, "root", "");

And here is the error:

Exception in thread "main" java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
..etc

I have tried:

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/new_schema","root",null);
String url = "jdbc:mysql://localhost:3306/new_schema";
Connection conn = DriverManager.getConnection(url, "root", null);

as "" is marked as empty string not null.

also try to give

grant all privileges on new_schema.* to 'root'@'%'

Are you able to connect MySQL locally using?

mysql -u root -p

if you are not able to connect locally, then must be something wrong with your MySQL setup. Also please take to take a backup before uninstalling the server

Try

  1. Uninstall MYSQL server
  2. Remove everything from the base folder/directory where MySQL is installed
  3. Install a fresh MySQL server

Please elaborate more about the issue Java code seems to be correct Also, how are you connecting MySQL with Java code, is it via mysql-connector-XXXX.jar or something else?

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