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:
- Delete the project from Eclipse (not on the disk)
- Close Eclipse
- Go to Project folder and delete
.classpath
and.project
files - Open eclipse, go to
File > Open projects from File System
and choose the project directory - Right click on the project > Run As > Run Configurations and set the right Main class
Related Topics
Using Eclipse Java Compiler (Ecj) in Maven Builds
Jsoup Cookies for Https Scraping
How to Copy the Contents of One Arraylist into Another
Hashmap to Return Default Value for Non-Found Keys
"Not Allowed to Load Local Resource: File:///C:....Jpg" Java Ee Tomcat
Does Java Read Integers in Little Endian or Big Endian
Converting a Date Object to a Calendar Object
When Do You Need to Explicitly Call a Superclass Constructor
String Array Initialization in Java
Key Existence Check in Hashmap
Is Doing a Lot in Constructors Bad
"Faceted Project Problem (Java Version Mismatch)" Error Message
Java.Lang.Classnotfoundexception: Org.Springframework.Boot.Springapplication Maven
Import Sun.Misc.Base64Encoder Results in Error Compiled in Eclipse
Pass Array to Oracle Procedure
Eclipse Memory Settings When Getting "Java Heap Space" and "Out of Memory"