diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index 8d43b505..c2b117ae 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -9,36 +9,33 @@ public class Main { private static final Injector INJECTOR = Injector.getInstance("mate.academy"); - private static final Book BOOK_1 = new Book("OnePiece", new BigDecimal(195)); - private static final Book BOOK_1_UPDATE = new Book(1L, "OnePice", new BigDecimal(250)); - private static final Book BOOK_2 = new Book("Naruto", new BigDecimal(235)); - private static final Book BOOK_3 = new Book("Jujutsu Kaisen", new BigDecimal(243)); public static void main(String[] args) { BookDao bookDao = (BookDao) INJECTOR.getInstance(BookDao.class); - bookDao.create(BOOK_1); - bookDao.create(BOOK_2); - bookDao.create(BOOK_3); + List books = List.of( + new Book("OnePiece", new BigDecimal(195)), + new Book("Naruto", new BigDecimal(235)), + new Book("Jujutsu Kaisen", new BigDecimal(243)) + ); - bookDao.update(BOOK_1_UPDATE); + books.forEach(bookDao::create); - List books = bookDao.findAll(); + Book book1Update = new Book(1L, "OnePiece", new BigDecimal(250)); + Book updatedBook = bookDao.update(book1Update); + System.out.println("Updated book: " + updatedBook); + + List allBooks = bookDao.findAll(); System.out.println("All books:"); - for (Book book : books) { - System.out.println(book); - } + allBooks.forEach(System.out::println); - Optional findById = bookDao.findById(1L); - System.out.println("Book found id: " + findById); + Optional bookById = bookDao.findById(1L); + System.out.println("Book found by id: " + bookById.orElse(null)); bookDao.deleteById(3L); List listAfterDelete = bookDao.findAll(); System.out.println("List after delete by id:"); - for (Book book : listAfterDelete) { - System.out.println(book); - } - + listAfterDelete.forEach(System.out::println); } } diff --git a/src/main/java/mate/academy/dao/BookDaoImpl.java b/src/main/java/mate/academy/dao/BookDaoImpl.java index 20b2d9ec..0403c076 100644 --- a/src/main/java/mate/academy/dao/BookDaoImpl.java +++ b/src/main/java/mate/academy/dao/BookDaoImpl.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; +import mate.academy.exception.DataProcessingException; import mate.academy.lib.Dao; import mate.academy.model.Book; import mate.academy.util.ConnectionUtil; @@ -17,18 +18,19 @@ public class BookDaoImpl implements BookDao { public Book create(Book book) { String query = "INSERT INTO books (title, price) VALUES (?, ?)"; try (Connection connection = ConnectionUtil.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(query, + PreparedStatement preparedStatement = connection.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS)) { preparedStatement.setString(1, book.getTitle()); preparedStatement.setBigDecimal(2, book.getPrice()); preparedStatement.executeUpdate(); - ResultSet resultSet = preparedStatement.getGeneratedKeys(); - if (resultSet.next()) { - book.setId(resultSet.getLong(1)); + ResultSet generatedKeys = preparedStatement.getGeneratedKeys(); + if (generatedKeys.next()) { + Long id = generatedKeys.getLong(1); + book.setId(id); } return book; } catch (SQLException e) { - throw new RuntimeException("Failed to create a book", e); + throw new DataProcessingException("Failed to create a book", e); } } @@ -41,13 +43,13 @@ public Optional findById(Long id) { ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { return Optional.of(new Book( - resultSet.getLong("id"), - resultSet.getString("title"), - resultSet.getBigDecimal("price"))); + resultSet.getLong("id"), + resultSet.getString("title"), + resultSet.getBigDecimal("price"))); } return Optional.empty(); } catch (SQLException e) { - throw new RuntimeException("Failed to find a book by id: " + id, e); + throw new DataProcessingException("Failed to find a book by id: " + id, e); } } @@ -66,7 +68,7 @@ public List findAll() { } return books; } catch (SQLException e) { - throw new RuntimeException("Failed to find all books", e); + throw new DataProcessingException("Failed to find all books", e); } } @@ -81,7 +83,7 @@ public Book update(Book book) { preparedStatement.executeUpdate(); return book; } catch (SQLException e) { - throw new RuntimeException("Failed to update a book", e); + throw new DataProcessingException("Failed to update a book", e); } } @@ -93,7 +95,7 @@ public boolean deleteById(Long id) { preparedStatement.setLong(1, id); return preparedStatement.executeUpdate() > 0; } catch (SQLException e) { - throw new RuntimeException("Failed to delete a book with id: " + id, e); + throw new DataProcessingException("Failed to delete a book with id: " + id, e); } } } diff --git a/src/main/java/mate/academy/util/ConnectionUtil.java b/src/main/java/mate/academy/util/ConnectionUtil.java index 0fa9bb1b..52db8f1e 100644 --- a/src/main/java/mate/academy/util/ConnectionUtil.java +++ b/src/main/java/mate/academy/util/ConnectionUtil.java @@ -6,8 +6,8 @@ import java.util.Properties; public class ConnectionUtil { - private static final String DB_URL = "jdbc:mysql:" - + "//localhost:3306/mydatabase?serverTimezone=UTC"; + private static final String DB_URL = + "jdbc:mysql://localhost:3306/mydatabase?serverTimezone=UTC"; private static final Properties DB_PROPERTIES; static {