Harmeet Singh Taara Harmeet Singh Taara - 6 months ago 40
SQL Question

Java: Face Cannot create PoolableConnectionFactory (Access denied for user 'root '@'localhost' (using password: YES)) Exception

I am trying to use Apache DBCP connection pooling technique with Spring and my DB is MySQL. My Apache DBCP configuration code is:

@Bean(destroyMethod = "close") // destroyMethod attribute is used to close the bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("db.driver"));
dataSource.setUrl(environment.getRequiredProperty("db.url"));
dataSource.setUsername(environment.getRequiredProperty("db.username"));
dataSource.setPassword(environment.getRequiredProperty("db.password"));
dataSource.setInitialSize(5);
dataSource.setMaxTotal(5);
return dataSource;
}


But When I try to run the program, it will throw an exception :

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


For Resolving this exception, i already google it, and find out this answer :

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'root' WITH GRANT OPTION;


Login as root user. But unfortunately this will not work and an exception still occurs. So what should I do?

I was also trying to create new user and grant all privileges to new user, but still i face the same exception.

Answer

Did you notice the extra spaces in the error message:

Access denied for user 'root    '@'localhost'
                            ^^^^

Remove the spaces and it will probably work.

Comments