Skip to content

Commit

Permalink
BookDaoImpl.java
Browse files Browse the repository at this point in the history
  • Loading branch information
VladSlob authored Jul 6, 2024
1 parent 23fd752 commit 0f3d216
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/main/java/mate/academy/dao/impl/BookDaoImpl.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package mate.academy.dao.impl;

import mate.academy.dao.BookDao;
import mate.academy.exception.DataProcessingException;
import mate.academy.lib.Dao;
import mate.academy.model.Book;
import mate.academy.util.ConnectionUtil;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
Expand All @@ -15,15 +9,20 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import mate.academy.dao.BookDao;
import mate.academy.exception.DataProcessingException;
import mate.academy.lib.Dao;
import mate.academy.model.Book;
import mate.academy.util.ConnectionUtil;

@Dao
public class BookDaoImpl implements BookDao {
@Override
public Book create(Book book) {
String query = "INSERT INTO books (title, price) VALUES (?, ?)";
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query,
Statement.RETURN_GENERATED_KEYS)) {
PreparedStatement preparedStatement = connection.prepareStatement(query,
Statement.RETURN_GENERATED_KEYS)) {
preparedStatement.setString(1, book.getTitle());
preparedStatement.setBigDecimal(2, book.getPrice());
int affectedRows = preparedStatement.executeUpdate();
Expand All @@ -44,7 +43,7 @@ public Book create(Book book) {
public Optional<Book> findById(Long id) {
String query = "SELECT * FROM books WHERE id = ?";
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setLong(1, id);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
Expand All @@ -61,7 +60,7 @@ public List<Book> findAll() {
String query = "SELECT * FROM books";
List<Book> books = new ArrayList<>();
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
books.add(parseBookFromResultSet(resultSet));
Expand All @@ -76,7 +75,7 @@ public List<Book> findAll() {
public Book update(Book book) {
String query = "UPDATE books SET title = ?, price = ? WHERE id = ?";
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setString(1, book.getTitle());
preparedStatement.setBigDecimal(2, book.getPrice());
preparedStatement.setLong(3, book.getId());
Expand All @@ -94,7 +93,7 @@ public Book update(Book book) {
public boolean deleteById(Long id) {
String query = "DELETE FROM books WHERE id = ?";
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setLong(1, id);
return preparedStatement.executeUpdate() > 0;
} catch (SQLException e) {
Expand Down

0 comments on commit 0f3d216

Please sign in to comment.