Skip to content

Commit

Permalink
jdbc-solution - V1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikname2303 committed Apr 2, 2024
1 parent 537a2d7 commit ac2b38f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
71 changes: 39 additions & 32 deletions src/main/java/mate/academy/dao/BookDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,28 @@

@Dao
public class BookDaoImpl implements BookDao {
private static final int COLUMN_TITLE = 1;
private static final int COLUMN_PRICE = 2;
private static final int COLUMN_ID = 3;
private static final String ID = "id";
private static final String TITLE = "title";
private static final String PRICE = "price";

@Override
public Book create(Book book) {
String sql = "INSERT INTO books (title, price) VALUES (?, ?)";
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement = connection
.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)
) {
statement.setString(1, book.getTitle());
statement.setBigDecimal(2, book.getPrice());
statement.setString(COLUMN_TITLE, book.getTitle());
statement.setBigDecimal(COLUMN_PRICE, book.getPrice());

int affectedRows = statement.executeUpdate();
if (affectedRows < 1) {
throw new DataProcessingException(
"Expected to insert at least one row, but inserted 0 rows for book: "
+ book);
}
checkChanges(affectedRows, book);
ResultSet generatedKeys = statement.getGeneratedKeys();
if (generatedKeys.next()) {
Long id = generatedKeys.getObject(1, Long.class);
Long id = generatedKeys.getObject(COLUMN_TITLE, Long.class);
book.setId(id);
}
} catch (SQLException e) {
Expand All @@ -45,19 +48,24 @@ public Book create(Book book) {

@Override
public Optional<Book> findById(Long id) {
Book book = null;
Book book = new Book();
String sql = "SELECT * FROM books WHERE id = ?";
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setLong(1, id);
statement.setLong(COLUMN_TITLE, id);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
book = mapResultToBook(resultSet);
long columnId = resultSet.getObject(ID, Long.class);
BigDecimal columnPrice = resultSet.getObject(PRICE, BigDecimal.class);
String columnTitle = resultSet.getString(TITLE);
book.setTitle(columnTitle);
book.setPrice(columnPrice);
book.setId(columnId);
}
} catch (SQLException e) {
throw new DataProcessingException("Can't get a book by id " + id, e);
}
return Optional.ofNullable(book);
return Optional.of(book);
}

@Override
Expand All @@ -68,7 +76,13 @@ public List<Book> findAll() {
PreparedStatement statement = connection.prepareStatement(sql)) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Book book = mapResultToBook(resultSet);
Book book = new Book();
long columnId = resultSet.getObject(ID, Long.class);
Object columnPrice = resultSet.getObject(PRICE);
String columnTitle = resultSet.getString(TITLE);
book.setPrice((BigDecimal) columnPrice);
book.setTitle(columnTitle);
book.setId(columnId);
books.add(book);
}
} catch (SQLException e) {
Expand All @@ -82,16 +96,12 @@ public Book update(Book book) {
String sql = "UPDATE books SET title = ?, price = ? WHERE id = ?";
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setString(1, book.getTitle());
statement.setBigDecimal(2, book.getPrice());
statement.setLong(3, book.getId());
statement.setString(COLUMN_TITLE, book.getTitle());
statement.setBigDecimal(COLUMN_PRICE, book.getPrice());
statement.setLong(COLUMN_ID, book.getId());

int affectedRows = statement.executeUpdate();
if (affectedRows < 1) {
throw new DataProcessingException(
"Expected to update at least one row, but updated 0 rows for book: "
+ book);
}
checkChanges(affectedRows, book);
} catch (SQLException e) {
throw new DataProcessingException("Can't update a book: " + book, e);
}
Expand All @@ -104,23 +114,20 @@ public boolean deleteById(Long id) {
int affectedRows;
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setLong(1, id);
statement.setLong(COLUMN_TITLE, id);
affectedRows = statement.executeUpdate();
} catch (SQLException e) {
throw new DataProcessingException("Can't delete a book with id: " + id, e);
}
return affectedRows > 0;
}

private Book mapResultToBook(ResultSet resultSet) throws SQLException {
Long id = resultSet.getLong("id");
String title = resultSet.getString("title");
BigDecimal price = resultSet.getObject("price", BigDecimal.class);

Book book = new Book();
book.setId(id);
book.setTitle(title);
book.setPrice(price);
return book;
private boolean checkChanges(int affectedRows, Book book) {
if (affectedRows < 1) {
throw new DataProcessingException(
"Expected to update at least one row, but updated nothing: "
+ book);
}
return true;
}
}
4 changes: 2 additions & 2 deletions src/main/resources/init_db.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
USE jdbc;
CREATE TABLE books (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255),
price DECIMAL
price DECIMAL(10,2)
);

0 comments on commit ac2b38f

Please sign in to comment.