-
Notifications
You must be signed in to change notification settings - Fork 438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jdbc-intro #399
base: main
Are you sure you want to change the base?
jdbc-intro #399
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there! 👋 It looks like there are a few critical issues that need to be addressed before we can approve this Pull Request. Specifically, the handling of preparedStatement
parameters starting from the wrong index and the incorrect usage of Statement.RETURN_GENERATED_KEYS
in methods where it's not required could lead to bugs and unexpected behavior. Also, the table name in SQL queries should be consistent and follow best practices. Let's make these changes to ensure the code functions correctly and adheres to the guidelines. Keep up the good work, and I'm sure with a few tweaks, we'll get there! 🚀
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
@@ -0,0 +1,36 @@ | |||
package mate.academy; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
package mate.academy; | |
package mate.academy.util; |
replace to util package
import mate.academy.domain.Book; | ||
|
||
public interface BookDao { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Statement.RETURN_GENERATED_KEYS)) { | ||
preparedStatement.setString(2, book.getTitle()); | ||
preparedStatement.setBigDecimal(3, book.getPrice()); | ||
preparedStatement.execute(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preparedStatement.execute(); | |
preparedStatement.execute(); | |
int affectedRows = preparedStatement.executeUpdate(); | |
if (affectedRows < 1) { | |
throw new DataProcessingException( | |
"Expected to insert at least 1 row, but 0 was inserted"); | |
} |
|
||
@Override | ||
public Book create(Book book) { | ||
Book createdBook; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Book createdBook; |
resultSet.next(); | ||
createdBook = new Book( | ||
resultSet.getString("title"), | ||
resultSet.getBigDecimal("price")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resultSet.next(); | |
createdBook = new Book( | |
resultSet.getString("title"), | |
resultSet.getBigDecimal("price")); | |
if (generatedKeys.next()) { | |
Long id = generatedKeys.getObject(1, Long.class); | |
book.setId(id); |
PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_BOOK_QUERY, | ||
Statement.RETURN_GENERATED_KEYS)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix comment
preparedStatement.setLong(1, id); | ||
isDeleted = preparedStatement.execute(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preparedStatement.setLong(1, id); | |
isDeleted = preparedStatement.execute(); | |
preparedStatement.setLong(1, id); | |
return preparedStatement.executeUpdate() > 0; |
} catch (SQLException throwables) { | ||
throw new RuntimeException(throwables); | ||
} | ||
return isDeleted; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return isDeleted; |
|
||
@Override | ||
public boolean deleteById(int id) { | ||
boolean isDeleted; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
boolean isDeleted; |
src/main/resources/init_db.sql
Outdated
create schema if not exists test; | ||
|
||
create table if not exists book ( | ||
book_id serial primary key, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
book_id serial primary key, | |
id bigint primary key, |
No description provided.