-
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 dao, connected to db, tested all of this in main #317
base: main
Are you sure you want to change the base?
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.
Look great, only 1 minor comment
src/main/java/mate/academy/Main.java
Outdated
public static void main(String[] args) { | ||
BookDao bookDao = (BookDao) injector.getInstance(BookDao.class); | ||
|
||
Book book1 = new Book("Book #1", BigDecimal.valueOf(10)); |
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.
why book1?
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.
why book1?
Oh, at first I added several books, and then I decided to leave only one and forgot to rename it, I'll have to do that, thanks
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.
magic numbers, check other methods as well
Long id = resultSet.getLong("id"); | ||
String title = resultSet.getString("title"); | ||
BigDecimal price = 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.
replace literals with constants
|
||
ResultSet resultSet = statement.getGeneratedKeys(); | ||
if (resultSet.next()) { | ||
book = new Book(resultSet.getLong(1), book.getTitle(), 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.
use getBook()
that you already have
String title = resultSet.getString("title"); | ||
BigDecimal price = resultSet.getBigDecimal("price"); | ||
|
||
Book book = new Book(id, title, 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.
and here too
BigDecimal price = resultSet.getBigDecimal("price"); | ||
|
||
Book book = new Book(id, title, 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.
you can check the difference in behavior with the null if you want 😉
return Optional.of(book); | |
return Optional.ofNullable(book); |
} | ||
|
||
private static Book getBook(ResultSet resultSet) throws SQLException { | ||
Long id = resultSet.getLong("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.
check the common mistakes on that getLong()
DB_PROPERTIES.put("user", "root"); | ||
DB_PROPERTIES.put("password", "$miskPass@"); |
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.
magic values, make them constant
src/main/resources/init_db.sql
Outdated
`id` BIGINT AUTO_INCREMENT PRIMARY KEY, | ||
`title` VARCHAR(255) NOT NULL, | ||
`price` decimal NOT NULL | ||
) |
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.
) | |
) | |
try { | ||
Class.forName(DRIVER); | ||
} catch (ClassNotFoundException e) { | ||
throw new RuntimeException("Can not load JDBC driver" + DRIVER, e); |
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.
throw new RuntimeException("Can not load JDBC driver" + DRIVER, e); | |
throw new RuntimeException("Can not load JDBC driver: " + DRIVER, e); | |
public static Connection getConnection() throws SQLException { | ||
return DriverManager.getConnection(DB_URL, DB_PROPERTIES); | ||
} |
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.
let's use try/catch here to get the error closer to it's reason
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.
LGTM 👍
No description provided.