-
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
Devolepment #412
base: main
Are you sure you want to change the base?
Devolepment #412
Changes from all commits
1680052
180f166
df391fe
688c379
8e06965
761d644
8708c72
0380514
427fe7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,40 @@ | ||
package mate.academy; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import mate.academy.dao.BookDao; | ||
import mate.academy.lib.Injector; | ||
import mate.academy.model.Book; | ||
|
||
public class Main { | ||
private static final Injector injector = Injector.getInstance("mate.academy"); | ||
|
||
public static void main(String[] args) { | ||
BookDao bookDao = (BookDao) injector.getInstance(BookDao.class); | ||
List<Book> books = List.of( | ||
new Book("Can't Hurt Me: Master Your Mind and Defy the Odds", | ||
new BigDecimal("8.99")), | ||
new Book("Atomic Habits", new BigDecimal("25.99")), | ||
new Book("The Great Gatsby", new BigDecimal("15.00")) | ||
); | ||
|
||
for (Book book: books) { | ||
bookDao.create(book); | ||
} | ||
|
||
List<Book> getBooksFromDB = bookDao.findAll(); | ||
|
||
System.out.println(getBooksFromDB); | ||
|
||
Optional<Book> optionalBookFromDb = bookDao.findById(getBooksFromDB.get(2).getId()); | ||
|
||
Book bookFromDb = getBooksFromDB.get(2); | ||
bookFromDb.setPrice(new BigDecimal("40.99")); | ||
bookFromDb.setTitle("Head First Java: A Brain-Friendly Guide"); | ||
|
||
bookDao.update(bookFromDb); | ||
|
||
bookDao.deleteById(books.get(1).getId()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package mate.academy.dao; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import mate.academy.model.Book; | ||
|
||
public interface BookDao { | ||
Book create(Book book); | ||
|
||
Optional<Book> findById(Long id); | ||
|
||
List<Book> findAll(); | ||
|
||
Book update(Book book); | ||
|
||
boolean deleteById(Long id); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package mate.academy.dao; | ||
|
||
import java.math.BigDecimal; | ||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import mate.academy.exeption.DataProcessingException; | ||
import mate.academy.lib.Dao; | ||
import mate.academy.model.Book; | ||
import mate.academy.service.ConnectionUtil; | ||
|
||
@Dao | ||
public class BookDaoImpl implements BookDao { | ||
@Override | ||
public Book create(Book book) { | ||
String sql = "INSERT INTO books (title, price) VALUES (?, ?)"; | ||
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement statement = | ||
connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS)) { | ||
|
||
statement.setString(1, book.getTitle()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember to avoid code duplication. Since you're setting parameters for |
||
statement.setBigDecimal(2, book.getPrice()); | ||
|
||
int affectedRows = statement.executeUpdate(); | ||
if (affectedRows < 1) { | ||
throw new DataProcessingException("Expected to insert at least one row " | ||
+ ", but inserted 0 rows."); | ||
} | ||
|
||
ResultSet generatedKeys = statement.getGeneratedKeys(); | ||
if (generatedKeys.next()) { | ||
Long id = generatedKeys.getObject(1, Long.class); | ||
book.setId(id); | ||
} | ||
} catch (SQLException e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use informative messages for exceptions. Instead of a generic message, include the book's details for better debugging. For example: "Can't save a book: " + book |
||
throw new DataProcessingException("Can't save a book to DB: " + book); | ||
} | ||
return book; | ||
} | ||
|
||
@Override | ||
public Optional<Book> findById(Long id) { | ||
String sql = "SELECT * FROM books WHERE id = ?"; | ||
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement preparedStatement = connection.prepareStatement(sql)) { | ||
preparedStatement.setLong(1, id); | ||
ResultSet resultSet = preparedStatement.executeQuery(); | ||
if (resultSet.next()) { | ||
return Optional.of(getBookFromResultSet(resultSet)); | ||
} | ||
} catch (SQLException e) { | ||
throw new RuntimeException("Can't get a book by id: " + id); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public List<Book> findAll() { | ||
List<Book> books = new ArrayList<>(); | ||
String sql = "SELECT * FROM books"; | ||
try (Connection connection = ConnectionUtil.getConnection(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
PreparedStatement preparedStatement = connection.prepareStatement(sql)) { | ||
ResultSet resultSet = preparedStatement.executeQuery(); | ||
while (resultSet.next()) { | ||
books.add(getBookFromResultSet(resultSet)); | ||
} | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Can't get all books from the database: " + books); | ||
} | ||
return books; | ||
} | ||
|
||
@Override | ||
public Book update(Book book) { | ||
String sql = "UPDATE books SET title = ?, price = ? WHERE id = ?"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use uppercase for SQL keywords in your queries. For example, use 'UPDATE' instead of 'update'. |
||
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement statement = connection.prepareStatement(sql)) { | ||
statement.setString(1, book.getTitle()); | ||
statement.setBigDecimal(2, book.getPrice()); | ||
statement.setLong(3, book.getId()); | ||
|
||
int affectedRows = statement.executeUpdate(); | ||
if (affectedRows < 1) { | ||
throw new DataProcessingException("Can't update the book with id: " | ||
+ book.getId()); | ||
} | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Can't update the book with id: " | ||
+ book.getId()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use informative messages for exceptions. Include the book's ID for better debugging. For example: "Can't update the book with id: " + book.getId() |
||
} | ||
return book; | ||
} | ||
|
||
@Override | ||
public boolean deleteById(Long id) { | ||
String sql = "DELETE FROM books WHERE id = ?"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use uppercase for SQL keywords in your queries. For example, use 'DELETE FROM' instead of 'delete from'. |
||
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement statement = connection.prepareStatement(sql)) { | ||
statement.setLong(1, id); | ||
return statement.executeUpdate() > 0; | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Can't delete the book with id: " | ||
+ id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The exception message should be more informative. For example, 'Can't delete the book with id: ' + id. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use informative messages for exceptions. Include the book's ID for better debugging. For example: "Can't delete the book with id: " + id |
||
} | ||
} | ||
|
||
private Book getBookFromResultSet(ResultSet resultSet) throws SQLException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider moving the logic for retrieving data from the ResultSet into a separate private method to avoid code duplication. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When converting |
||
Long id = resultSet.getObject("id", Long.class); | ||
String title = resultSet.getString("title"); | ||
BigDecimal price = resultSet.getObject("price", BigDecimal.class); | ||
|
||
Book book = new Book(); | ||
book.setId(id); | ||
book.setTitle(title); | ||
book.setPrice(price); | ||
return book; | ||
Comment on lines
+111
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package mate.academy.exeption; | ||
|
||
public class DataProcessingException extends RuntimeException { | ||
public DataProcessingException(String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package mate.academy.model; | ||
|
||
import java.math.BigDecimal; | ||
|
||
public class Book { | ||
private Long id; | ||
private String title; | ||
private BigDecimal price; | ||
|
||
public Book() { | ||
} | ||
|
||
public Book(String title, BigDecimal price) { | ||
this.title = title; | ||
this.price = price; | ||
} | ||
Comment on lines
+13
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When you convert |
||
|
||
public Book(Long id, String title, BigDecimal price) { | ||
this.id = id; | ||
this.title = title; | ||
this.price = price; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public BigDecimal getPrice() { | ||
return price; | ||
} | ||
|
||
public void setPrice(BigDecimal price) { | ||
this.price = price; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Book(" | ||
+ "id=" + id | ||
+ ", title=" + title | ||
+ ", price=" + price; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package mate.academy.service; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
import java.util.Properties; | ||
|
||
public class ConnectionUtil { | ||
private static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/test?user=root"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
private static final Properties DB_PROPERTIES; | ||
private static final String USERNAME = System.getenv("DB_USERNAME"); | ||
private static final String PASSWORD = System.getenv("DB_PASSWORD"); | ||
|
||
static { | ||
DB_PROPERTIES = new Properties(); | ||
DB_PROPERTIES.put("user", USERNAME); | ||
DB_PROPERTIES.put("password", PASSWORD); | ||
|
||
try { | ||
Class.forName("com.mysql.cj.jdbc.Driver"); | ||
} catch (ClassNotFoundException e) { | ||
throw new RuntimeException("Can not load JDBC driver", e); | ||
} | ||
} | ||
|
||
public static Connection getConnection() { | ||
try { | ||
return DriverManager.getConnection(DB_URL,DB_PROPERTIES); | ||
} catch (SQLException e) { | ||
throw new RuntimeException("Can't connection DriverManager"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CREATE TABLE books ( | ||
id BIGINT AUTO_INCREMENT, | ||
title VARCHAR(255), | ||
price DECIMAL(10, 2), | ||
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.
Use uppercase for SQL keywords in your queries. For example, use 'INSERT INTO' instead of 'insert into'.