Skip to content
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

Jv jdbc intro hw solution #319

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

JohnGrey17
Copy link

No description provided.

Copy link

@karma-o karma-o left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some modifications required


public class Book {
private Long id;

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

@@ -0,0 +1,6 @@
CREATE TABLE `books` (
`id` INT NOT NULL AUTO_INCREMENT,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`id` INT NOT NULL AUTO_INCREMENT,
`id` BIGINT NOT NULL AUTO_INCREMENT,

public class BookDaoImpl implements BookDao {
@Override
public Book create(Book book) {
String sql = "INSERT INTO books(title, price) VALUES (?, ?)";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this var could use a better naming, same in other places


int affectedRows = statement.executeUpdate();
if (affectedRows < 1) {
throw new RuntimeException("Expected to insert at least one row");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why raw RuntimeExcepion?

book.setId(id);
}
} catch (SQLException e) {
throw new RuntimeException("Cannot save data", e);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here and in other places

Comment on lines 49 to 55
Long bookId = resultSet.getLong("id");
String title = resultSet.getString("title");
BigDecimal price = resultSet.getBigDecimal("price");
Book book = new Book();
book.setId(bookId);
book.setTitle(title);
book.setPrice(price);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code duplication, extract a private method parseBook() from it

Comment on lines 49 to 51
Long bookId = resultSet.getLong("id");
String title = resultSet.getString("title");
BigDecimal price = resultSet.getBigDecimal("price");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make those literals constants

Comment on lines 93 to 95
statement.setString(1, book.getTitle());
statement.setBigDecimal(2, book.getPrice());
statement.setLong(3, book.getId());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

magic numbers, check other methods as well

int rowsDeleted = statement.executeUpdate();
return rowsDeleted > 0;
} catch (SQLException e) {
throw new DataProcessingException("Failed to delete book with id: " + id + e);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new DataProcessingException("Failed to delete book with id: " + id + e);
throw new DataProcessingException("Failed to delete book with id: " + id, e);

}
return book;
} catch (SQLException e) {
throw new DataProcessingException("Failed to update book with id: " + book.getId() + e);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, we should pass it as a separate parameter, because we need the whole original exception info, like stack trace and it's message to be saved, and passed ll the way to the user or dev

CREATE TABLE `books` (
`id` INT NOT NULL AUTO_INCREMENT,
`bookTitle` VARCHAR(50),
`price` BIGINT,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

price can be like 10.99, does integer allow any value after point?

@JohnGrey17 JohnGrey17 requested a review from karma-o April 2, 2024 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants