Skip to content

Commit

Permalink
work on errors has been carried out
Browse files Browse the repository at this point in the history
  • Loading branch information
IDMFokin committed Aug 23, 2024
1 parent 14e6e76 commit f12c3ea
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import mate.academy.model.Book;

public class Main {
private static final Injector injector = Injector.getInstance("dao");
private static final Injector injector = Injector.getInstance("mate.academy");

public static void main(String[] args) {
BookDao bookDao = (BookDao) injector.getInstance(BookDao.class);
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/mate/academy/dao/BookDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Optional<Book> findById(Long id) {
statement.setLong(1, id);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
Book book = createBookObject(resultSet);
Book book = mapResultSetToBook(resultSet);
return Optional.of(book);
}
} catch (SQLException e) {
Expand All @@ -67,13 +67,13 @@ public Optional<Book> findById(Long id) {
public List<Book> findAll() {
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(FIND_ALL_BOOK)) {
List<Book> allBooks = new ArrayList<>();
List<Book> books = new ArrayList<>();
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Book bookObject = createBookObject(resultSet);
allBooks.add(bookObject);
Book book = mapResultSetToBook(resultSet);
books.add(book);
}
return allBooks;
return books;
} catch (SQLException e) {
throw new DataProcessingException("Can't find all books from db ", e);
}
Expand Down Expand Up @@ -103,14 +103,13 @@ public boolean deleteById(Long id) {
PreparedStatement statement =
connection.prepareStatement(DELETE_BOOK)) {
statement.setObject(1, id);
int row = statement.executeUpdate();
return row > 0;
return statement.executeUpdate() > 0;
} catch (SQLException e) {
throw new DataProcessingException("Can't delete a book by id " + id, e);
}
}

private static Book createBookObject(ResultSet resultSet) {
private Book mapResultSetToBook(ResultSet resultSet) {
try {
Book book = new Book();
book.setId(resultSet.getObject(ID, Long.class));
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/init_db.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE books (
id BIGINT NOT NULL AUTO_INCREMENT,
id BIGINT AUTO_INCREMENT,
title VARCHAR(200),
price DECIMAL(5,2),
PRIMARY KEY (id)
Expand Down

0 comments on commit f12c3ea

Please sign in to comment.