IDr*_*nov 7 java postgresql tomcat8
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;
}
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
its*_*ont 17
我希望现在还不算太晚。我在我的项目中修复了这个问题,只是为数据库用户分配了一个密码,在我的例子中是“postgres”。
ALTER USER "postgres" WITH PASSWORD 'password';
Run Code Online (Sandbox Code Playgroud)
在你的 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
Run Code Online (Sandbox Code Playgroud)
P.D:我是 Stack Overflow 的新手。我希望我能帮上忙。
| 归档时间: |
|
| 查看次数: |
34609 次 |
| 最近记录: |