0

I got this error "access denied for user using password=NO" where I already added the right password.please give me solution on this.

Here is the stack trace:

java.sql.SQLException: Access denied for user 'www.abcom.in'@'localhost' (using password: NO)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql- connector-java-8.0.26.jar:8.0.26]
at 
com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122  ) ~[mysql-connector-java-8.0.26.jar:8.0.26]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828) ~[mysql-connector-java-8.0.26.jar:8.0.26]
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448) ~[mysql-connector-java-8.0.26.jar:8.0.26]
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241) ~[mysql-connector-java-8.0.26.jar:8.0.26]
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) ~[mysql-connector-java-8.0.26.jar:8.0.26]
    at java.sql.DriverManager.getConnection(DriverManager.java:664) ~[na:1.8.0_292]
    at java.sql.DriverManager.getConnection(DriverManager.java:208) ~[na:1.8.0_292]
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:155) ~[spring-jdbc-5.3.9.jar:5.3.9]
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:146) ~[spring-jdbc-5.3.9.jar:5.3.9]
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:205) ~[spring-jdbc-5.3.9.jar:5.3.9]
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:169) ~[spring-jdbc-5.3.9.jar:5.3.9]
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) ~[hibernate-core-

Application configuration file Program:

package com.training.newspringquerydsl.config;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import com.querydsl.jpa.impl.JPAQueryFactory;

@Configuration
@ComponentScan(basePackages = "com")
public class AppConfiguration {

@Bean("datasource")
public DataSource getDataSource() {
    
    DriverManagerDataSource dataSource = new DriverManagerDataSource();

    dataSource.setUsername(dataSource.getUsername());
    dataSource.setPassword(dataSource.getPassword());
    dataSource.setUrl("jdbc:mysql://localhost/springwebservices");
    dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
    return dataSource;
}


@Bean
public JPAQueryFactory getjpaQueryFactory() {
    
    EntityManagerFactory emf = Persistence.createEntityManagerFactory(null);
    EntityManager em = emf.createEntityManager();
    JPAQueryFactory queryFactory = new JPAQueryFactory(em);
    return queryFactory;
    }
}

Application properties file:

spring.datasource.url=jdbc:mysql://localhost:3306/springwebservices
spring.datasource.Data-username=root
spring.datasource.Data-password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66

1 Answers1

0

The properties with username and password are wrong:

It must be:

spring.datasource.username=root
spring.datasource.password=root
Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • I tried for spring.datasource.username=root but it's not working – Rutuja Gharge Aug 12 '21 at 14:46
  • And what's the exception now? – Simon Martinelli Aug 12 '21 at 14:47
  • @SimonMartinelli if parsing properties would have been the problem, then `Access denied for user 'www.abcom.in'@'localhost'` would not have been the exception thrown. – Giorgi Tsiklauri Aug 12 '21 at 14:49
  • Still got "java.sql.SQLException: Access denied for user 'www.abcom.in'@'localhost' (using password: NO)" this exception – Rutuja Gharge Aug 12 '21 at 14:52
  • @GiorgiTsiklauri No username and password are not mandatory and if you connect to mysql without username/password it will use the username of the OS – Simon Martinelli Aug 12 '21 at 14:52
  • Can you please update your question with the application.properties file you are currently using? – Simon Martinelli Aug 12 '21 at 14:53
  • @SimonMartinelli I'm not sure I understand what you mean.. either you are missing a punctuation, or you have one extra "no". :) Also, it very much depends on how you have configured your MySQL instance.. doesn't it? – Giorgi Tsiklauri Aug 12 '21 at 14:53
  • @RutujaGharge can you log into your MySQL instance, with credentials used in `application.propertis`, from shell/cmd or any SQL Client IDE? – Giorgi Tsiklauri Aug 12 '21 at 14:54
  • @GiorgiTsiklauri There was missing a , after no. If no username is present: "The default user name is ODBC on Windows or your Unix login name on Unix." https://dev.mysql.com/doc/refman/8.0/en/connecting.html – Simon Martinelli Aug 12 '21 at 14:57
  • 1
    @SimonMartinelli now I understand. Still.. it depends on how the instance is configured. Let's wait for what the OP will update us with. – Giorgi Tsiklauri Aug 12 '21 at 15:00
  • @SimonMartinelli here is application properties----spring.datasource.url=jdbc:mysql://localhost:3306/springwebservices spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update – Rutuja Gharge Aug 12 '21 at 16:26
  • How is your MySQL server configured and do you run this locally or on a server? – Simon Martinelli Aug 13 '21 at 05:59