-
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
Create BookDao interface, MySqlBookDao class #49
base: main
Are you sure you want to change the base?
Conversation
Create Book, DataProcessingException, MySqlConnectionManager classes
public static Connection getConnection() { | ||
try { | ||
return DriverManager.getConnection(DB_URL, DB_PROPERTIES); | ||
} catch (SQLException e) { | ||
throw new RuntimeException("Cannot get connection", 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.
Maybe you shouldn't catch it there, for me looks better to catch this in Dao in try-with-resources
|
||
@Override | ||
public String toString() { | ||
return "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.
'+' should be on new line
src/main/java/mate/academy/Main.java
Outdated
public class Main { | ||
public static final Injector injector = Injector.getInstance("mate.academy"); |
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.
Constant has naming rules
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.
Empty lines should be between methods
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.
Nice, but some improvements required
src/main/java/mate/academy/Main.java
Outdated
public class Main { | ||
public static final Injector INJECTOR = Injector.getInstance("mate.academy"); |
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.
Wht public?
} | ||
|
||
@Override | ||
public boolean equals(Object o) { |
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.
Do you use hachCode and equals somewhere?
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 used equals() in main method
try (Connection connection = MySqlConnectionManager.getConnection(); | ||
PreparedStatement statement = connection.prepareStatement(query, | ||
Statement.RETURN_GENERATED_KEYS)) { | ||
statement.setString(1, book.getTitle()); |
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
Statement.RETURN_GENERATED_KEYS)) { | ||
statement.setString(1, book.getTitle()); | ||
statement.setBigDecimal(2, book.getPrice()); | ||
if (statement.executeUpdate() < 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.
magic numbers
|
||
@Override | ||
public Book update(Book book) { | ||
String query = """ |
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.
Looks like too much quotes, it is better to make this query in one line or make all queries like this to be consistent
statement.setString(1, book.getTitle()); | ||
statement.setBigDecimal(2, book.getPrice()); | ||
statement.setLong(3, book.getId()); | ||
if (statement.executeUpdate() < 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.
If no rows updates it is ok
Statement.RETURN_GENERATED_KEYS)) { | ||
statement.setString(TITLE_PARAMETER_INDEX, book.getTitle()); | ||
statement.setBigDecimal(PRICE_PARAMETER_INDEX, book.getPrice()); | ||
if (statement.executeUpdate() < ONE_AFFECTED_ROW) { |
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, it's a very rare case when you can receive 0 affected rows on insert, not sure if this check is very useful.
PreparedStatement statement = connection.prepareStatement(query)) { | ||
statement.setString(TITLE_PARAMETER_INDEX, book.getTitle()); | ||
statement.setBigDecimal(PRICE_PARAMETER_INDEX, book.getPrice()); | ||
statement.setLong(UPDATE_ID_PARAMETER_INDEX, book.getId()); |
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.
Where is executeUpdate()
call?
} | ||
|
||
private Book parseToObject(ResultSet resultSet) throws SQLException { | ||
long id = resultSet.getObject(ID_ROW_LABEL, Long.class); |
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.
If you are expecting that null can appear then NPE can be thrown here.
long id = resultSet.getObject(ID_ROW_LABEL, Long.class); | |
Long id = resultSet.getObject(ID_ROW_LABEL, Long.class); |
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.
Nice, one issue left
@@ -17,43 +17,11 @@ | |||
https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml | |||
</maven.checkstyle.plugin.configLocation> | |||
</properties> | |||
|
|||
<build> |
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.
Is it required to delete this?
Create Book, DataProcessingException, MySqlConnectionManager classes