IDragunov
IDragunov

Reputation: 101

The server requested SCRAM-based authentication, but no password was provided

I has been writing java web-app using the servlets+jsp, JDBC, and tomcat how a servlet container. When I connect to the database and trying to get some data, i given a current exception : enter image description here

Project structure : enter image description here

DataSource code :

public class LibraryDataSource {
    private static final Logger LOGGER = Logger.getLogger(LibraryDataSource.class);

    private LibraryDataSource() {}

    public static DataSource getLibraryDataSource() {
        PGSimpleDataSource libraryDatasource = new PGSimpleDataSource();

        try(FileReader propertiesReader =
                    new FileReader("src/main/resources/application.properties")) {

            Properties databaseProperties = new Properties();
            databaseProperties.load(propertiesReader);
            libraryDatasource.setURL(databaseProperties.getProperty("postgresUrl"));
            libraryDatasource.setUser(databaseProperties.getProperty("postgresUser"));
            libraryDatasource.setPassword(databaseProperties.getProperty("postgresPassword"));
        } catch (FileNotFoundException e) {
            LOGGER.info("LibraryDataSource::getLibraryDataSource : ", e);
        } catch (IOException e) {
            LOGGER.info("LibraryDataSource::getLibraryDataSource : ", e);
        }

        return libraryDatasource;
    }
}

BookDAO method where the error detected :

@Override
    public List<Book> getAll() {
        List<Book> books = new ArrayList<>();
        try(Connection connection = dataSource.getConnection()) {
            Statement getAllStatement = connection.createStatement();
            ResultSet resultSet = getAllStatement.executeQuery("SELECT * FROM Book");

            while (resultSet.next()) {
                Book book = new Book();
                book.setId(resultSet.getLong(1));
                book.setTitle(resultSet.getString(2));
                book.setYear(resultSet.getInt(3));
                book.setQuantity(resultSet.getInt(4));
                book.setAuthors(resultSet.getString(5));

                books.add(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();

        }
        return books;
    }

Upvotes: 10

Views: 58303

Answers (5)

Tiya
Tiya

Reputation: 1

I fixed this in my project by add a username and password in application.properties. Before that the setting of spring.datasource.username and spring.datasource.password are empty.

Upvotes: 0

Andrey Katanskiy
Andrey Katanskiy

Reputation: 1

I fixed this in my project by changing params in application.properties from spring.datasource to spring.jpa.hibernate, just like that:

spring.jpa.properties.hibernate.connection.url=jdbc:postgresql://localhost:5432/{DB_NAME}
spring.jpa.properties.hibernate.connection.username = {USERNAME}
spring.jpa.properties.hibernate.connection.password = {PASSWORD}
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

Upvotes: 0

Sospeter Mong&#39;are
Sospeter Mong&#39;are

Reputation: 121

I fixed this in my project by just assigning a password to the database user, in my case 'postgres'

Upvotes: 2

Sam
Sam

Reputation: 51

spring.datasource.password = your_password

You just have to check if that line of code is active in your application.properties or not commented out. Then you are good to run the project again.

Upvotes: 4

itsMont
itsMont

Reputation: 361

I hope it's not too late. I fixed this in my project just assigning a password to the database user, in my case 'postgres'.

ALTER USER "postgres" WITH PASSWORD 'password';

And in your application.properties file you must have something like (I'm using postgreSQL):

spring.datasource.url=jdbc:postgresql://localhost:5432/db_name
spring.datasource.username=postgres
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true

P. D: I'm new in Stack Overflow. I hope I could have helped.

Upvotes: 26

Related Questions