diff --git a/.gitignore b/.gitignore index e1049ab4..6366460f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ .idea/* *.iml target/* -src/main/resources/db/db.properties \ No newline at end of file diff --git a/src/main/java/mate/academy/dao/BookDaoImpl.java b/src/main/java/mate/academy/dao/BookDaoImpl.java index 9b8777e0..1e8bbded 100644 --- a/src/main/java/mate/academy/dao/BookDaoImpl.java +++ b/src/main/java/mate/academy/dao/BookDaoImpl.java @@ -70,11 +70,8 @@ public Optional findById(Long id) { ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { - Long identify = resultSet.getObject(FIRST_COLUMN_INDEX, Long.class); - String title = resultSet.getObject(SECOND_COLUMN_INDEX, String.class); - BigDecimal price = resultSet.getObject(THIRD_COLUMN_INDEX, BigDecimal.class); + Book book = mapResultSetToBook(resultSet); - Book book = new Book(identify, title, price); optionalBook = Optional.of(book); } } catch (SQLException e) { @@ -89,15 +86,12 @@ public List findAll() { List bookList = new ArrayList<>(); try (Connection connection = ConnectionUtil.getConnection(); PreparedStatement preparedStatement = connection - .prepareStatement(SQL_OUTPUT_REQUEST)) { + .prepareStatement(SQL_OUTPUT_REQUEST)) { ResultSet resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { - Long id = resultSet.getObject(FIRST_COLUMN_INDEX, Long.class); - String title = resultSet.getObject(SECOND_COLUMN_INDEX, String.class); - BigDecimal price = resultSet.getObject(THIRD_COLUMN_INDEX, BigDecimal.class); + Book book = mapResultSetToBook(resultSet); - Book book = new Book(id, title, price); bookList.add(book); } @@ -112,7 +106,7 @@ public List findAll() { public Book update(Book book) { try (Connection connection = ConnectionUtil.getConnection(); PreparedStatement preparedStatement = connection - .prepareStatement(SQL_UPDATE_REQUEST)) { + .prepareStatement(SQL_UPDATE_REQUEST)) { preparedStatement.setString(FIRST_PARAMETER_INDEX, book.getTitle()); preparedStatement.setBigDecimal(SECOND_PARAMETER_INDEX, book.getPrice()); preparedStatement.setLong(THIRD_PARAMETER_INDEX, book.getId()); @@ -134,7 +128,7 @@ public boolean deleteById(Long id) { int affectedRows = 0; try (Connection connection = ConnectionUtil.getConnection(); PreparedStatement preparedStatement = connection - .prepareStatement(SQL_DELETE_REQUEST)) { + .prepareStatement(SQL_DELETE_REQUEST)) { preparedStatement.setLong(FIRST_PARAMETER_INDEX, id); affectedRows = preparedStatement.executeUpdate(); } catch (SQLException e) { @@ -143,4 +137,16 @@ public boolean deleteById(Long id) { } return affectedRows > 0; } + + private Book mapResultSetToBook(ResultSet resultSet) { + try { + Long id = resultSet.getObject(FIRST_COLUMN_INDEX, Long.class); + String title = resultSet.getObject(SECOND_COLUMN_INDEX, String.class); + BigDecimal price = resultSet.getObject(THIRD_COLUMN_INDEX, BigDecimal.class); + + return new Book(id, title, price); + } catch (SQLException e) { + throw new DataProcessingException("Error mapping ResultSet to Book", e); + } + } } diff --git a/src/main/java/mate/academy/db/db.properties b/src/main/resources/db.properties similarity index 70% rename from src/main/java/mate/academy/db/db.properties rename to src/main/resources/db.properties index f52bd9da..ab180655 100644 --- a/src/main/java/mate/academy/db/db.properties +++ b/src/main/resources/db.properties @@ -1,3 +1,3 @@ db.url=jdbc:mysql://localhost:3306/test db.user=root -db.password=QWERTY123 \ No newline at end of file +db.password=QWERTY123 diff --git a/src/main/resources/init_db.sql b/src/main/resources/init_db.sql index 7470c668..47876cc7 100644 --- a/src/main/resources/init_db.sql +++ b/src/main/resources/init_db.sql @@ -1,5 +1,5 @@ CREATE TABLE books ( - id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT, + id BIGINT PRIMARY KEY AUTO_INCREMENT, title VARCHAR(255) NOT NULL, price DECIMAL(10, 2) NOT NULL, UNIQUE (id)