java - 服务器请求基于 SCRAM 的身份验证,但未提供密码

标签 java postgresql tomcat8

我一直在使用 servlets+jsp、JDBC 和 tomcat 如何作为 servlet 容器编写 java web-app。当我连接到数据库并尝试获取一些数据时,我给出了当前异常:enter image description here

项目结构:enter image description here

数据源代码:

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 方法:

@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;
    }

最佳答案

我希望还不算太晚。 我在我的项目中修复了这个问题,只是为数据库用户分配了一个密码,在我的例子中是“postgres”。

ALTER USER "postgres" WITH PASSWORD 'password';

在你的 application.properties 文件中你必须有类似的东西(我使用的是 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:我是 Stack Overflow 的新手。我希望我能帮上忙。

关于java - 服务器请求基于 SCRAM 的身份验证,但未提供密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71545170/

相关文章:

java - IllegalArgumentException... url 异常中没有 ':'

java - 我的 Java 代码无法编译。错误可能出在哪里?输出是什么?

java - 确保 Java 类不从某些包中导入

postgresql - Postgres DDL 错误 : 'syntax error at or near "user"'

sql - 来自不同表的条件连接

postgresql - 在 AWS - EC2 上安装 Postgresql 9.6

java - debug onoutofmemory 无法启动tomcat

java - 尝试以数独样式打印二维数组

java 8 , tamcat 8 : Invalid byte tag in constant pool: 19

tomcat - p6spy使得Tomcat8在使用时不再重新部署