Skip to content

Commit

Permalink
made corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
trokhim03 committed Sep 28, 2024
1 parent 317d6b3 commit bf54eaa
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.idea/*
*.iml
target/*
src/main/resources/db/db.properties
28 changes: 17 additions & 11 deletions src/main/java/mate/academy/dao/BookDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,8 @@ public Optional<Book> 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) {
Expand All @@ -89,15 +86,12 @@ public List<Book> findAll() {
List<Book> 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);
}

Expand All @@ -112,7 +106,7 @@ public List<Book> 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());
Expand All @@ -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) {
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
db.url=jdbc:mysql://localhost:3306/test
db.user=root
db.password=QWERTY123
db.password=QWERTY123
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 PRIMARY KEY AUTO_INCREMENT,
id BIGINT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
UNIQUE (id)
Expand Down

0 comments on commit bf54eaa

Please sign in to comment.