-
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
added solution #81
base: main
Are you sure you want to change the base?
added solution #81
Conversation
public static Connection getConnection() throws SQLException { | ||
return DriverManager.getConnection("jdbc:mysql://localhost:3306/booksDB", 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.
add empty line to the end of file
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.
add empty line to the end of file
if (updates < 1) { | ||
throw new DataProcessingException("Error during creating book " + 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 do not need this check, affect zero rows - is okay
if (updates < 1) { | ||
throw new DataProcessingException("Error during updating book " + 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.
same here
public DataProcessingException(String message) { | ||
super(message); | ||
} | ||
} |
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.
add empty line to the end of file
", price=" + 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.
add empty line to the end of file
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.
Good solution, let's improve it a little bit :)
statement.setString(1, book.getTitle()); | ||
statement.setBigDecimal(2, book.getPrice()); | ||
statement.setLong(3, 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.
Create constants for magic numbers and reuse them
statement.setLong(3, book.getId()); | ||
statement.executeUpdate(); | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Can not update book in table", 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.
Which book? Add more info
@@ -0,0 +1,26 @@ | |||
package 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.
I'd recommend you create separate package for util classes
book.setId(resultSet.getObject(1, Long.class)); | ||
} | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Cant save book with title = " + book.getTitle(), 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.
print all info about book that cannot be saved
statement.setLong(1, id); | ||
return statement.executeUpdate() > 0; | ||
} catch (SQLException e) { | ||
throw new RuntimeException("Can not delete book by id " + id, 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 delete book by id " + id, e); | |
throw new RuntimeException("Cannot delete book by id " + id, e); |
src/main/resources/init_db.sql
Outdated
CREATE TABLE `books` ( | ||
`id` INT 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.
Int? How do you want to put a big decimal into the 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.
Good job!
No description provided.