Skip to content

Commit

Permalink
Changed BookDaoImpl and BookServiceImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
GlebPashko committed May 22, 2024
1 parent 05d6d79 commit 2a2957f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 24 deletions.
87 changes: 65 additions & 22 deletions src/main/java/mate/academy/dao/BookDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,51 @@
public class BookDaoImpl implements BookDao {
@Override
public Book create(Book book) {
String sql = "INSERT INTO books (title, price) VALUES (?,?)";

try (Connection connection = ConnectionUtil.getConnections();
PreparedStatement preparedStatement =
connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
preparedStatement.setString(1, book.getTitle());
preparedStatement.setBigDecimal(2, book.getPrice());

int affectedRows = preparedStatement.executeUpdate();
if (affectedRows == 0) {
throw new DataProcessingException("Failed to insert book into the database");
String sql = "INSERT INTO books (title, price) VALUES (?, ?)";
Connection connection = null;
PreparedStatement statement = null;
ResultSet generatedKeys = null;
try {
connection = ConnectionUtil.getConnections();
statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
statement.setString(1, book.getTitle());
statement.setBigDecimal(2, book.getPrice());

int affectedRows = statement.executeUpdate();
if (affectedRows < 1) {
throw new DataProcessingException(
"Can not insert into books table");
}

ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
generatedKeys = statement.getGeneratedKeys();
if (generatedKeys.next()) {
Long id = generatedKeys.getObject(1, Long.class);
book.setId(id);
}
} catch (SQLException e) {
throw new DataProcessingException("Can not insert book into database ", e);
throw new DataProcessingException("Failed to insert new book record", e);
} finally {
if (generatedKeys != null) {
try {
generatedKeys.close();
} catch (SQLException e) {
throw new DataProcessingException("Failed to insert new book record", e);
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
throw new DataProcessingException("Failed to insert new book record", e);
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
throw new DataProcessingException("Failed to insert new book record", e);
}
}
}
return book;
}
Expand All @@ -47,7 +72,7 @@ public Optional<Book> findById(Long id) {
Book book = null;
String sql = "SELECT * FROM books WHERE id = ?";
try (Connection connection = ConnectionUtil.getConnections();
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setLong(1, id);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
Expand All @@ -68,7 +93,7 @@ public List<Book> findAll() {
String sql = "SELECT * FROM books";

try (Connection connection = ConnectionUtil.getConnections();
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
ResultSet resultSet = preparedStatement.executeQuery();

while (resultSet.next()) {
Expand All @@ -83,9 +108,13 @@ public List<Book> findAll() {

@Override
public Book update(Book book) {
String sql = "UPDATE books SET title=?, price=? WHERE id=?";
try (Connection connection = ConnectionUtil.getConnections();
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
String sql = "UPDATE books SET title = ?, price = ? WHERE id = ?";
Connection connection = null;
PreparedStatement preparedStatement = null;

try {
connection = ConnectionUtil.getConnections();
preparedStatement = connection.prepareStatement(sql);

preparedStatement.setString(1, book.getTitle());
preparedStatement.setBigDecimal(2, book.getPrice());
Expand All @@ -96,8 +125,22 @@ public Book update(Book book) {
throw new SQLException("Failed to update book, no rows affected.");
}
} catch (SQLException e) {
throw new DataProcessingException("Can not update book into the database"
+ book + " ", e);
throw new DataProcessingException("Cannot update book in the database: " + book, e);
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
System.err.println("Failed to close PreparedStatement: " + e.getMessage());
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
System.err.println("Failed to close Connection: " + e.getMessage());
}
}
}
return book;
}
Expand All @@ -107,14 +150,14 @@ public boolean deleteById(Long id) {
String sql = "DELETE FROM books WHERE id=?";

try (Connection connection = ConnectionUtil.getConnections();
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setLong(1, id);
int affectedRows = preparedStatement.executeUpdate();
if (affectedRows == 0) {
throw new DataProcessingException("Failed delete book with id "
+ id + " from database");
}
return true;
return affectedRows > 0;
} catch (SQLException e) {
throw new DataProcessingException(("Failed delete book with id "
+ id + " from database"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@

@Dao
public class BookServiceImpl implements BookService {
private static final Injector injector = Injector.getInstance("mate.academy");
private final BookDao bookDao = (BookDaoImpl) injector.getInstance(BookDao.class);
private final BookDao bookDao;

public BookServiceImpl(BookDao bookDao) {
this.bookDao = bookDao;
}

@Override
public Book create(Book book) {
Expand Down

0 comments on commit 2a2957f

Please sign in to comment.