-
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
Hw jv jdbc intro #359
base: main
Are you sure you want to change the base?
Hw jv jdbc intro #359
Conversation
|
||
private Book createBook(ResultSet resultSet) throws SQLException { | ||
return new Book( | ||
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.
resultSet.getObject("id", Long.class);
src/main/resources/init_db.sql
Outdated
title VARCHAR(255) NOT NULL, | ||
price DECIMAL(6, 2) NOT NULL, | ||
is_deleted TINYINT NOT NULL DEFAULT 0 | ||
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; |
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.
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; | |
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; | |
|
||
@Override | ||
public boolean deleteById(Long id) { | ||
String deletedQuery = "UPDATE books SET is_deleted = 1 WHERE 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.
use TRUE and FALSE instead
src/main/java/mate/academy/Main.java
Outdated
public class Main { | ||
private static final Injector injector = Injector.getInstance("mate.academy"); | ||
private static final BookDao bookDao = (BookDao) injector.getInstance(BookDao.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.
private static final BookDao bookDao = (BookDao) injector.getInstance(BookDao.class); |
public class Main { | ||
private static final Injector injector = Injector.getInstance("mate.academy"); | ||
private static final BookDao bookDao = (BookDao) injector.getInstance(BookDao.class); | ||
|
||
public static void main(String[] args) { |
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.
public static void main(String[] args) { | |
public static void main(String[] args) { | |
BookDao bookDao = (BookDao) injector.getInstance(BookDao.class); |
insertQuery, Statement.RETURN_GENERATED_KEYS)) { | ||
statement.setString(1, book.getTitle()); | ||
statement.setBigDecimal(2, book.getPrice()); | ||
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.
statement.executeUpdate(); | |
int affectedRows = statement.executeUpdate(); | |
if (affectedRows < 1) { | |
throw new DataProcessingException( | |
"Expected to insert at least 1 row, but 0 was inserted"); | |
} |
PreparedStatement statement = connection.prepareStatement(updateQuery)) { | ||
statement.setString(1, book.getTitle()); | ||
statement.setBigDecimal(2, book.getPrice()); | ||
statement.setLong(3, 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.
statement.setLong(3, id); | |
statement.setLong(3, book.getId()); |
public Book update(Book book) { | ||
String updateQuery = "UPDATE books SET title = ?, price = ? " | ||
+ "WHERE id = ? AND is_deleted = FALSE"; | ||
Long id = 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.
Long id = book.getId(); |
statement.setString(1, book.getTitle()); | ||
statement.setBigDecimal(2, book.getPrice()); | ||
statement.setLong(3, 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(); | |
int affectedRows = statement.executeUpdate(); | |
if (affectedRows < 1) { | |
throw new DataProcessingException( | |
"Expected to update at least 1 row, but 0 was updated."); | |
} |
} catch (SQLException e) { | ||
throw new DataProcessingException(CANT_UPDATE + id, e); | ||
} | ||
throw new DataProcessingException(CANT_FIND_BY_ID + 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.
throw new DataProcessingException(CANT_FIND_BY_ID + id); | |
return book; |
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement statement = connection.prepareStatement(deletedQuery)) { | ||
statement.setLong(1, id); | ||
return statement.executeUpdate() != 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.
return statement.executeUpdate() != 0; | |
return statement.executeUpdate() > 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.
Good job!
No description provided.