Exception in Thread "Main" Java.Sql.Sqlexception: Access Denied for User ''@'Localhost' (Using Password: No)

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

As you are creating a database from scratch, you could use:

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=rootpassword"); 
PreparedStatement ps = connection.prepareStatement("CREATE DATABASE databasename");
int result = ps.executeUpdate();

Here is an identical scenario.

Exception in thread main java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)

Your eclipse is running the class "com.mysql.cj.jdbc.admin.TimezoneDump" (which also contain a main method) instead of your MainApp class.

Try this manipulation:

Right click on MainApp.java -> Run As -> Java Application

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) Spring boot

This error is not related to Spring Framework or Spring Boot.
The root cause is your MySQL configuration.

TLRD: you need to grant access to the root user from localhost. The same issue was asked and answered many times on StackOverflow already 1, 2, ...

You can try to configure you application like the following in order to fix your problem:

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/employee_management_system

java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO) in Spring

Adding name and password like this worked

# Connection url for the database "photo_app"
spring.datasource.url = jdbc:mysql://localhost:3306/photo_app
# Username and password
spring.datasource.username = suyash
spring.datasource.password = suyash
# Show or not log for each sql query
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

Access denied for user ''@'localhost' (using password: NO) in Eclipse, instead of 'root'@'localhost', classpath corrupted

Solution

The problem was not about MySQL login credentials, but on Eclipse configuration.

In my folder project the .classpath and the .project files were corrupted. Infact going to myproject>right click>Run As..>Run Configurations the main class was setted as com.mysql.cj.jdbc.admin.TimezoneDump.main instead of my mainApp class. Trying to change there the main class file not worked, because for Eclipse the mainApp doesn't exist.

So this is what i've done to solve the error:

  1. Delete the project from Eclipse (not on the disk)
  2. Close Eclipse
  3. Go to Project folder and delete .classpath and .project files
  4. Open eclipse, go to File > Open projects from File System and choose the project directory
  5. Right click on the project > Run As > Run Configurations and set the right Main class


Related Topics



Leave a reply



Submit