Skip to content
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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

Conversation

snowfallen
Copy link

No description provided.


private Book createBook(ResultSet resultSet) throws SQLException {
return new Book(
resultSet.getLong("id"),
Copy link

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);

title VARCHAR(255) NOT NULL,
price DECIMAL(6, 2) NOT NULL,
is_deleted TINYINT NOT NULL DEFAULT 0
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
)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 = ?";
Copy link

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

@snowfallen snowfallen requested a review from okuzan May 28, 2024 14:50
public class Main {
private static final Injector injector = Injector.getInstance("mate.academy");
private static final BookDao bookDao = (BookDao) injector.getInstance(BookDao.class);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Long id = book.getId();

statement.setString(1, book.getTitle());
statement.setBigDecimal(2, book.getPrice());
statement.setLong(3, id);
int affectedRows = statement.executeUpdate();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return statement.executeUpdate() != 0;
return statement.executeUpdate() > 0;

Copy link

@sarakhmen sarakhmen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants