-
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
implemented task #63
base: main
Are you sure you want to change the base?
implemented task #63
Conversation
src/main/java/mate/academy/Main.java
Outdated
bookDao.create(book); | ||
Long bookId = 1L; | ||
Optional<Book> foundBook = bookDao.findById(bookId); |
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.
i think it is bad idea to find book by hardcoded id
bookDao.create(book); | |
Long bookId = 1L; | |
Optional<Book> foundBook = bookDao.findById(bookId); | |
Book createdBook = bookDao.create(book); | |
Optional<Book> foundBook = bookDao.findById(ceatedBook.getId()); |
PreparedStatement statement = connection.prepareStatement(CREATE_QUERY, | ||
PreparedStatement.RETURN_GENERATED_KEYS)) { | ||
setBookParameters(statement, book); | ||
statement.executeUpdate(); |
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.
i think there should be
statement.executeUpdate(); | |
int updatedRows = statement.executeUpdate(); | |
if(updatedRows < 1) { | |
throw new DataProcassException ... |
PreparedStatement statement = connection.prepareStatement(UPDATE_QUERY)) { | ||
setBookParameters(statement, book); | ||
statement.setLong(3, book.getId()); | ||
statement.executeUpdate(); |
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.
i think there should be
statement.executeUpdate(); | |
int updatedRows = statement.executeUpdate(); | |
if(updatedRows < 1) { | |
throw new DataProcassException ... |
Book create(Book book); | ||
Optional<Book> findById(Long id); | ||
List<Book> findAll(); | ||
Book update(Book book); | ||
boolean deleteById(Long id); |
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.
add empty lines between methods
also add empty line to the end of file
private static final String CREATE_QUERY = "INSERT INTO books (title, price) VALUES (?, ?)"; | ||
private static final String FIND_BY_ID_QUERY = "SELECT * FROM books WHERE id = ?"; | ||
private static final String FIND_ALL_QUERY = "SELECT * FROM books"; | ||
private static final String UPDATE_QUERY = "UPDATE books SET title = ?, price = ? WHERE id = ?"; | ||
private static final String DELETE_BY_ID_QUERY = "DELETE FROM books WHERE id = ?"; |
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.
make queries as local variables for method where you use it
throw new DataProcessingException("Failed to create book: no rows were updated."); | ||
} |
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.
its okay to affect zero rows
if (updatedRows < 1) { | ||
throw new DataProcessingException("Failed to create book: no rows were updated."); | ||
} |
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.
same here
} | ||
try (ResultSet generatedKeys = statement.getGeneratedKeys()) { | ||
if (generatedKeys.next()) { | ||
book.setId(generatedKeys.getLong(1)); |
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.
see common mistakes how to get long values forom result set
statement.setString(1, book.getTitle()); | ||
statement.setBigDecimal(2, book.getPrice()); | ||
} | ||
} |
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.
add empty line to the end of file
public void setPrice(BigDecimal price) { | ||
this.price = 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.
add empty line to the end of file
<version>8.1.0</version> | ||
</dependency> | ||
</dependencies> | ||
|
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.
No description provided.