-
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
Mydb #358
base: main
Are you sure you want to change the base?
Mydb #358
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.
CI checks failed, please make sure build passes locally with any maven build command like mvn clean package
pom.xml
Outdated
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.
revert this. we should keep 1 blank line at the EOF
} | ||
return Optional.empty(); | ||
} catch (SQLException e) { | ||
throw new RuntimeException("Failed to find a book by 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.
specify id in error message
preparedStatement.setLong(1, id); | ||
return preparedStatement.executeUpdate() > 0; | ||
} catch (SQLException e) { | ||
throw new RuntimeException("Failed to delete a book by 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.
specify id in error message
src/main/resources/init_db.sql
Outdated
title VARCHAR(255), | ||
price INT, | ||
PRIMARY KEY(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("Can not lod JDBC driver", 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.
throw new RuntimeException("Can not lod JDBC driver", e); | |
throw new RuntimeException("Can not load JDBC driver", e); |
public static void main(String[] args) { | ||
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.
Let's create list of books here
List<Book> books = List.of( | |
new Book(... |
src/main/java/mate/academy/Main.java
Outdated
bookDao.create(BOOK_2); | ||
bookDao.create(BOOK_3); | ||
|
||
bookDao.update(BOOK_1_UPDATE); |
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 updated book and print it
src/main/java/mate/academy/Main.java
Outdated
public class Main { | ||
private static final Injector INJECTOR = Injector.getInstance("mate.academy"); | ||
private static final Book BOOK_1 = new Book("OnePiece", new BigDecimal(195)); |
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.
remove these constants
PreparedStatement.RETURN_GENERATED_KEYS)) { | ||
preparedStatement.setString(1, book.getTitle()); | ||
preparedStatement.setBigDecimal(2, book.getPrice()); | ||
preparedStatement.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.
preparedStatement.executeUpdate(); | |
int affectedRows = statement.executeUpdate(); | |
if (affectedRows < 1) { | |
throw new DataProcessingException( | |
"Expected to insert at least 1 row, but 0 was inserted"); | |
} |
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.
Better use this case
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 fixed
preparedStatement.executeUpdate(); | ||
ResultSet resultSet = preparedStatement.getGeneratedKeys(); | ||
if (resultSet.next()) { | ||
book.setId(resultSet.getLong(1)); |
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.
book.setId(resultSet.getLong(1)); | |
Long id = generatedKeys.getObject(1, Long.class); | |
book.setId(id); |
Check common mistakes file
preparedStatement.setString(1, book.getTitle()); | ||
preparedStatement.setBigDecimal(2, book.getPrice()); | ||
preparedStatement.setLong(3, book.getId()); | ||
preparedStatement.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.
preparedStatement.executeUpdate(); | |
int affectedRows = statement.executeUpdate(); | |
if (affectedRows < 1) { | |
throw new DataProcessingException( | |
"Expected to update at least 1 row, but 0 was updated."); | |
} |
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 fixed
import java.util.Properties; | ||
|
||
public class ConnectionUtil { | ||
private static final String DB_URL = "jdbc:mysql:" |
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 String DB_URL = "jdbc:mysql:" | |
private static final String DB_URL = "jdbc:mysql://localhost:3306/mydatabase?serverTimezone=UTC"; |
any concatenation creating new String object.
} | ||
return book; | ||
} catch (SQLException e) { | ||
throw new RuntimeException("Failed to create a 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.
throw new RuntimeException("Failed to create a book", e); | |
throw new DataProcessingException("Failed to create a book", e); |
Check all places and use DataProcessingException
PreparedStatement.RETURN_GENERATED_KEYS)) { | ||
preparedStatement.setString(1, book.getTitle()); | ||
preparedStatement.setBigDecimal(2, book.getPrice()); | ||
preparedStatement.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.
Better use this case
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement preparedStatement = connection.prepareStatement(query, | ||
PreparedStatement.RETURN_GENERATED_KEYS)) { |
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 with resources use only in read operations
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 fixed
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement preparedStatement = connection.prepareStatement(query)) { |
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.
The same with Try-w-resources
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 fixed
public class ConnectionUtil { | ||
private static final String DB_URL = | ||
"jdbc:mysql://localhost:3306/mydatabase?serverTimezone=UTC"; | ||
private static final Properties DB_PROPERTIES; | ||
|
||
static { |
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 class should has only one instance
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.
(Singletone)
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.
Please fix comments
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement preparedStatement = connection.prepareStatement(query, | ||
PreparedStatement.RETURN_GENERATED_KEYS)) { |
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 fixed
PreparedStatement.RETURN_GENERATED_KEYS)) { | ||
preparedStatement.setString(1, book.getTitle()); | ||
preparedStatement.setBigDecimal(2, book.getPrice()); | ||
preparedStatement.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.
Not fixed
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement preparedStatement = connection.prepareStatement(query)) { |
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 fixed
preparedStatement.setString(1, book.getTitle()); | ||
preparedStatement.setBigDecimal(2, book.getPrice()); | ||
preparedStatement.setLong(3, book.getId()); | ||
preparedStatement.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.
Not fixed
No description provided.