-
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 BookDao with connection to the DB #362
base: main
Are you sure you want to change the base?
Conversation
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); |
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.
move extracting book from resultSet to separate method and reuse it
src/main/resources/init_db.sql
Outdated
`title` VARCHAR (255), | ||
`price` INT, | ||
PRIMARY KEY (`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.
file should end with empty line
import mate.academy.model.Book; | ||
|
||
@Dao | ||
|
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.
@Override | ||
public Book create(Book book) { | ||
String sql = "INSERT INTO books (title, price) VALUE (?, ?)"; | ||
try (Connection connection = ConnectionUtil.getConnection(); |
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.
Use try with resources only for read operations
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.
discussed in chat
src/main/resources/init_db.sql
Outdated
CREATE TABLE `books` ( | ||
`id` BIGINT NOT NULL AUTO_INCREMENT, | ||
`title` VARCHAR (255), | ||
`price` INT, |
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.
Use DECIMAL or NUMERIC for BigDecimal value
src/main/resources/init_db.sql
Outdated
@@ -0,0 +1,6 @@ | |||
CREATE TABLE `books` ( | |||
`id` BIGINT NOT NULL AUTO_INCREMENT, |
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.
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).
No description provided.