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

jdbc-solution - V1.0 #313

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

Conversation

Nikname2303
Copy link

No description provided.

Copy link

@JohnGrey17 JohnGrey17 left a comment

Choose a reason for hiding this comment

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

LGFM

Copy link

@yevheniisynytsia yevheniisynytsia left a comment

Choose a reason for hiding this comment

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

Let's use DB, not list =)

Comment on lines 28 to 30
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(DB_PATH, PROPERTIES);
}

Choose a reason for hiding this comment

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

let's catch exception closer to its origin

Comment on lines 10 to 12
public class BookDaoImpl implements BookDao {
private List<Book> books = new ArrayList<>();
private Long idCounter = 1L;

Choose a reason for hiding this comment

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

this implementation is wrong

we are learning to use MySQL. Please check common mistakes to see some examples
Or come to QnA to discuss it

id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255),
price DECIMAL
);

Choose a reason for hiding this comment

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

Suggested change
);
);

Comment on lines 26 to 30
statement.setString(1, book.getTitle());
statement.setBigDecimal(2, book.getPrice());

int affectedRows = statement.executeUpdate();
if (affectedRows < 1) {
Copy link

Choose a reason for hiding this comment

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

magic numbers, check other methods as well

Comment on lines 30 to 34
if (affectedRows < 1) {
throw new DataProcessingException(
"Expected to insert at least one row, but inserted 0 rows for book: "
+ book);
}
Copy link

Choose a reason for hiding this comment

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

code duplication in the update method, extract it to a method

}

private Book mapResultToBook(ResultSet resultSet) throws SQLException {
Long id = 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.

as it was mentioned in the common mistakes

Suggested change
Long id = resultSet.getLong("id");
Long id = resultSet.getObject("id", Long.class);

Comment on lines 116 to 118
Long id = resultSet.getLong("id");
String title = resultSet.getString("title");
BigDecimal price = resultSet.getObject("price", BigDecimal.class);
Copy link

Choose a reason for hiding this comment

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

let's make those strings constant

@@ -0,0 +1,6 @@
USE jdbc;
CREATE TABLE books (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Copy link

Choose a reason for hiding this comment

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

Primary key already contains not null constraint in it

Suggested change
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
id INT AUTO_INCREMENT PRIMARY KEY,

CREATE TABLE books (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255),
price DECIMAL
Copy link

Choose a reason for hiding this comment

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

I'd rather specify the ranges

Suggested change
price DECIMAL
price DECIMAL(10,2)

@Nikname2303 Nikname2303 requested a review from karma-o April 2, 2024 09:43
Comment on lines 58 to 63
long columnId = resultSet.getObject(ID, Long.class);
BigDecimal columnPrice = resultSet.getObject(PRICE, BigDecimal.class);
String columnTitle = resultSet.getString(TITLE);
book.setTitle(columnTitle);
book.setPrice(columnPrice);
book.setId(columnId);

Choose a reason for hiding this comment

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

move it back to a separate method please =) this logic is duplicated so it should live in a separate method

book.setTitle(title);
book.setPrice(price);
return book;
private boolean checkChanges(int affectedRows, Book book) {

Choose a reason for hiding this comment

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

please think about a more meaningful name

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