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

Implemented BookDao with connection to the DB #362

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

Conversation

vfilimo
Copy link

@vfilimo vfilimo commented Jun 4, 2024

No description provided.

Comment on lines 52 to 58
String title = resultSet.getString("title");
BigDecimal price = resultSet.getObject("price", BigDecimal.class);
Book book = new Book();
book.setId(id);
book.setTitle(title);
book.setPrice(price);
return Optional.of(book);

Choose a reason for hiding this comment

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

move extracting book from resultSet to separate method and reuse it

`title` VARCHAR (255),
`price` INT,
PRIMARY KEY (`id`)
);

Choose a reason for hiding this comment

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

file should end with empty line

@vfilimo vfilimo requested a review from Vasul-Pankiv June 5, 2024 06:44
import mate.academy.model.Book;

@Dao

Choose a reason for hiding this comment

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

Suggested change

@Override
public Book create(Book book) {
String sql = "INSERT INTO books (title, price) VALUE (?, ?)";
try (Connection connection = ConnectionUtil.getConnection();

Choose a reason for hiding this comment

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

Use try with resources only for read operations

Copy link
Author

Choose a reason for hiding this comment

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

discussed in chat

CREATE TABLE `books` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`title` VARCHAR (255),
`price` INT,

Choose a reason for hiding this comment

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

Use DECIMAL or NUMERIC for BigDecimal value

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

Choose a reason for hiding this comment

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

Actually MySQL declares primary key as NOT NULL automatically
PRIMARY KEY
A unique index where all key columns must be defined as NOT NULL. If they are not explicitly declared as NOT NULL, MySQL declares them so implicitly (and silently).

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