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

Create BookDao interface, MySqlBookDao class #49

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

Conversation

Dimagaa
Copy link

@Dimagaa Dimagaa commented Aug 10, 2023

Create Book, DataProcessingException, MySqlConnectionManager classes

Create Book, DataProcessingException, MySqlConnectionManager classes
Comment on lines 24 to 30
public static Connection getConnection() {
try {
return DriverManager.getConnection(DB_URL, DB_PROPERTIES);
} catch (SQLException e) {
throw new RuntimeException("Cannot get connection", e);
}
}

Choose a reason for hiding this comment

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

Maybe you shouldn't catch it there, for me looks better to catch this in Dao in try-with-resources


@Override
public String toString() {
return "Book{" +

Choose a reason for hiding this comment

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

'+' should be on new line

public class Main {
public static final Injector injector = Injector.getInstance("mate.academy");

Choose a reason for hiding this comment

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

Constant has naming rules

Comment on lines 8 to 12
Book create(Book book);
Optional<Book> findById(Long id);
List<Book> findAll();
Book update(Book book);
boolean deleteById(Long id);

Choose a reason for hiding this comment

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

Empty lines should be between methods

@Dimagaa Dimagaa requested a review from keda-vlad August 11, 2023 06:15
Copy link

@andrii-hoienko andrii-hoienko left a comment

Choose a reason for hiding this comment

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

Nice, but some improvements required

public class Main {
public static final Injector INJECTOR = Injector.getInstance("mate.academy");

Choose a reason for hiding this comment

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

Wht public?

}

@Override
public boolean equals(Object o) {

Choose a reason for hiding this comment

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

Do you use hachCode and equals somewhere?

Copy link
Author

Choose a reason for hiding this comment

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

I used equals() in main method

try (Connection connection = MySqlConnectionManager.getConnection();
PreparedStatement statement = connection.prepareStatement(query,
Statement.RETURN_GENERATED_KEYS)) {
statement.setString(1, book.getTitle());

Choose a reason for hiding this comment

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

magic numbers

Statement.RETURN_GENERATED_KEYS)) {
statement.setString(1, book.getTitle());
statement.setBigDecimal(2, book.getPrice());
if (statement.executeUpdate() < 1) {

Choose a reason for hiding this comment

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

magic numbers


@Override
public Book update(Book book) {
String query = """

Choose a reason for hiding this comment

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

Looks like too much quotes, it is better to make this query in one line or make all queries like this to be consistent

statement.setString(1, book.getTitle());
statement.setBigDecimal(2, book.getPrice());
statement.setLong(3, book.getId());
if (statement.executeUpdate() < 1) {

Choose a reason for hiding this comment

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

If no rows updates it is ok

Statement.RETURN_GENERATED_KEYS)) {
statement.setString(TITLE_PARAMETER_INDEX, book.getTitle());
statement.setBigDecimal(PRICE_PARAMETER_INDEX, book.getPrice());
if (statement.executeUpdate() < ONE_AFFECTED_ROW) {

Choose a reason for hiding this comment

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

Actually, it's a very rare case when you can receive 0 affected rows on insert, not sure if this check is very useful.

PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(TITLE_PARAMETER_INDEX, book.getTitle());
statement.setBigDecimal(PRICE_PARAMETER_INDEX, book.getPrice());
statement.setLong(UPDATE_ID_PARAMETER_INDEX, book.getId());

Choose a reason for hiding this comment

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

Where is executeUpdate() call?

}

private Book parseToObject(ResultSet resultSet) throws SQLException {
long id = resultSet.getObject(ID_ROW_LABEL, Long.class);

Choose a reason for hiding this comment

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

If you are expecting that null can appear then NPE can be thrown here.

Suggested change
long id = resultSet.getObject(ID_ROW_LABEL, Long.class);
Long id = resultSet.getObject(ID_ROW_LABEL, Long.class);

Copy link

@andrii-hoienko andrii-hoienko left a comment

Choose a reason for hiding this comment

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

Nice, one issue left

@@ -17,43 +17,11 @@
https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml
</maven.checkstyle.plugin.configLocation>
</properties>

<build>

Choose a reason for hiding this comment

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

Is it required to delete this?

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