-
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 CRUD operations #66
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.
Let's improve your solution :)
src/main/java/mate/academy/Main.java
Outdated
//update | ||
book.setTitle("JDBC for wood people, Part2"); | ||
book.setPrice(BigDecimal.valueOf(500)); | ||
book.setId(3L); |
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.
According to the query, you cannot change id for book.
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.
Add spaces between methods
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. Fix it in all places. Maybe you can reuse some of them :)
int affectedRows = statement.executeUpdate(); | ||
if (affectedRows < 1) { | ||
throw new RuntimeException("Expected to insert at least 1 row, but inserted 0 rows."); | ||
} |
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 don't need to check affected rows here
Book bookFromDb = null; | ||
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement statement = connection.prepareStatement(query)) { | ||
statement.setLong(1, 3L); |
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.
what is that? Where do you search by the id that method gets?
int affectedRow = statement.executeUpdate(); | ||
if (affectedRow < 1) { | ||
throw new RuntimeException("Expected to delete at least 1 row but was 0."); | ||
} | ||
return affectedRow > 0; |
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 delete 0 rows. Also, how are you expecting to get false in this method according to your solution?
int affectedRow = statement.executeUpdate(); | |
if (affectedRow < 1) { | |
throw new RuntimeException("Expected to delete at least 1 row but was 0."); | |
} | |
return affectedRow > 0; | |
return statement.executeUpdate() > 0; |
} | ||
return affectedRow > 0; | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Can not delete row by the 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.
row or book?
} | ||
|
||
} catch (SQLException e) { | ||
throw new DataProcessingException("Can not update row by the book with id: " + book.getId(), 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.
row or book?
src/main/resources/init_db.sql
Outdated
PRIMARY KEY (id) | ||
); | ||
|
||
SHOW TABLES; |
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.
SHOW TABLES; |
src/main/resources/init_db.sql
Outdated
@@ -0,0 +1,14 @@ | |||
SHOW DATABASES; |
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.
SHOW DATABASES; |
…ntro-homework # Conflicts: # pom.xml
pom.xml
Outdated
@@ -56,4 +62,4 @@ | |||
</plugins> | |||
</pluginManagement> | |||
</build> | |||
</project> | |||
</project> |
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 not forget about empty line at the end of files.
book.setId(id); | ||
} | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Can`t get a 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.
Not resolved.
throw new DataProcessingException("Can`t get a new Book: " + book, e); | |
throw new DataProcessingException("Can`t create a new book: " + book, e); |
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement statement = connection.prepareStatement(query)) { | ||
statement.setLong(1, 3L); | ||
|
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 resolved.
|
||
int affectedRow = statement.executeUpdate(); | ||
if (affectedRow < 1) { | ||
throw new RuntimeException("Expected to update at least 1 row but was 0."); | ||
} | ||
|
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 resolved.
|
||
private Book parseResultSetInBook(ResultSet resultSet) throws SQLException { | ||
Book dbBook = new Book(); | ||
dbBook.setId(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.
try { | ||
Class.forName("com.mysql.cj.jdbc.Driver"); | ||
} catch (ClassNotFoundException e) { | ||
throw new RuntimeException(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.
Give some valuable message here.
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.
price INT, | |
price DECIMAL, |
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.