-
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
bookDao methods implemented and tested #343
base: main
Are you sure you want to change the base?
Conversation
src/main/resources/init_db.sql
Outdated
@@ -0,0 +1,5 @@ | |||
CREATE TABLE `books` ( | |||
`id` BIGINT AUO_INCREMENT PRIMARY KEY, | |||
`title` VARCHAR(255), |
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.
Not null for title and price
import java.sql.SQLException; | ||
import java.util.Properties; | ||
|
||
public class ConnectionUtil { |
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 to util package
PreparedStatement statement = connection.prepareStatement(sql)) { | ||
statement.setLong(1, id); | ||
|
||
int affectedRows = statement.executeUpdate(); |
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.
int affectedRows = statement.executeUpdate(); | |
return statement.executeUpdate() > 0; |
+ " inserted 0 rows", null); | ||
} | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Failed to add new book: " + book, 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.
wrong message
int affectedRows = statement.executeUpdate(); | ||
if (affectedRows < 1) { | ||
throw new DataProcessingException("Expected to insert at least 1 row," | ||
+ " inserted 0 rows", 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.
Don't put null, create other constructor public DataProcessingException(String message) if needed
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement statement = connection.prepareStatement(sql)) { | ||
statement.setLong(1, id); | ||
try (ResultSet resultSet = statement.executeQuery()) { |
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 do you need try block here?
No description provided.