-
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
Added task solution #408
base: main
Are you sure you want to change the base?
Added task solution #408
Changes from all commits
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,25 @@ | ||
package mate.academy; | ||
|
||
import java.math.BigDecimal; | ||
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); | ||
Book book = new Book(1L, "Java Programming", new BigDecimal(200)); | ||
|
||
bookDao.create(book); | ||
bookDao.findAll().forEach(System.out::println); | ||
System.out.println(); | ||
System.out.println("Id 1 " + System.lineSeparator() + bookDao.findById(1L)); | ||
bookDao.deleteById(2L); | ||
bookDao.findAll().forEach(System.out::println); | ||
System.out.println(); | ||
bookDao.update(book); | ||
bookDao.findAll().forEach(System.out::println); | ||
} | ||
} |
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,120 @@ | ||
package mate.academy.dao; | ||
|
||
import java.math.BigDecimal; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import mate.academy.exception.DataProcessingException; | ||
import mate.academy.lib.Dao; | ||
import mate.academy.model.Book; | ||
import mate.academy.util.ConnectionUtil; | ||
|
||
@Dao | ||
public class BookDaoImpl implements BookDao { | ||
|
||
@Override | ||
public Book create(Book book) { | ||
String sql = "INSERT INTO books (title, price) VALUES (?, ?)"; | ||
|
||
try (PreparedStatement statement = ConnectionUtil.getConnection().prepareStatement(sql, | ||
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. You should close the |
||
Statement.RETURN_GENERATED_KEYS)) { | ||
statement.setString(1, book.getTitle()); | ||
statement.setBigDecimal(2, book.getPrice()); | ||
|
||
int affectedRows = statement.executeUpdate(); | ||
|
||
if (affectedRows < 1) { | ||
throw new DataProcessingException("Creating book failed, no rows affected."); | ||
} | ||
|
||
try (ResultSet generatedKeys = statement.getGeneratedKeys()) { | ||
if (generatedKeys.next()) { | ||
book.setId(generatedKeys.getObject(1, Long.class)); | ||
} else { | ||
throw new DataProcessingException("Creating book failed, no ID obtained."); | ||
} | ||
} | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Can't create book " + book, 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. The exception message is good, it's informative and includes the book that failed to be created. |
||
} | ||
|
||
return book; | ||
} | ||
|
||
@Override | ||
public Optional<Book> findById(Long id) { | ||
String sql = "SELECT * FROM books WHERE id = ?"; | ||
|
||
try (PreparedStatement statement = ConnectionUtil.getConnection().prepareStatement(sql)) { | ||
statement.setLong(1, id); | ||
|
||
try (ResultSet resultSet = statement.executeQuery()) { | ||
if (resultSet.next()) { | ||
return Optional.of(getBook(resultSet)); | ||
} | ||
} | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Can't find by id for id = " + id, 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. The exception message is good, it's informative and includes the id that was not found. |
||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public List<Book> findAll() { | ||
List<Book> books = new ArrayList<>(); | ||
String sql = "SELECT * FROM books"; | ||
|
||
try (PreparedStatement statement = ConnectionUtil.getConnection().prepareStatement(sql); | ||
ResultSet resultSet = statement.executeQuery()) { | ||
|
||
while (resultSet.next()) { | ||
books.add(getBook(resultSet)); | ||
} | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Can't retrieve all books", e); | ||
} | ||
|
||
return books; | ||
} | ||
|
||
@Override | ||
public Book update(Book book) { | ||
String sql = "UPDATE books SET price = ?, title = ? WHERE id = ?"; | ||
try (PreparedStatement statement = ConnectionUtil.getConnection().prepareStatement(sql)) { | ||
statement.setBigDecimal(1, book.getPrice()); | ||
statement.setString(2, book.getTitle()); | ||
statement.setLong(3, book.getId()); | ||
if (statement.executeUpdate() > 0) { | ||
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 |
||
return book; | ||
} else { | ||
throw new DataProcessingException("No book found 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. Avoid throwing a generic |
||
new RuntimeException()); | ||
} | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Can't update book with id = " + book.getId(), 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. The exception message is good, it's informative and includes the id of the book that failed to update. |
||
} | ||
} | ||
Comment on lines
+86
to
+101
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. For the |
||
|
||
@Override | ||
public boolean deleteById(Long id) { | ||
String sql = "DELETE FROM books WHERE id = ?"; | ||
try (PreparedStatement statement = ConnectionUtil.getConnection().prepareStatement(sql)) { | ||
statement.setLong(1, id); | ||
return statement.executeUpdate() > 0; | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Can't delete book with id = " + id, 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. The exception message is good, it's informative and includes the id of the book that failed to be deleted. |
||
} | ||
} | ||
|
||
private Book getBook(ResultSet resultSet) throws SQLException { | ||
Long id = resultSet.getObject("id", Long.class); | ||
BigDecimal price = resultSet.getObject("price", BigDecimal.class); | ||
String title = resultSet.getString("title"); | ||
return new Book(id, title, price); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package mate.academy.exception; | ||
|
||
public class DataProcessingException extends RuntimeException { | ||
public DataProcessingException(String message, Throwable ex) { | ||
super(message, ex); | ||
} | ||
|
||
public DataProcessingException(String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package mate.academy.model; | ||
|
||
import java.math.BigDecimal; | ||
|
||
public class Book { | ||
private Long id; | ||
private String title; | ||
private BigDecimal price; | ||
|
||
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 BigDecimal getPrice() { | ||
return price; | ||
} | ||
|
||
public void setPrice(BigDecimal price) { | ||
this.price = price; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
@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,27 @@ | ||
package mate.academy.util; | ||
|
||
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://localhost:3306/bookstore"; | ||
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; | ||
|
||
static { | ||
DB_PROPERTIES = new Properties(); | ||
DB_PROPERTIES.put("user", "root"); | ||
DB_PROPERTIES.put("password", "qwertyasd"); | ||
|
||
try { | ||
Class.forName("com.mysql.cj.jdbc.Driver"); | ||
} catch (ClassNotFoundException e) { | ||
throw new RuntimeException("Can not load JDBC", e); | ||
} | ||
} | ||
|
||
public static Connection getConnection() throws SQLException { | ||
return DriverManager.getConnection(DB_URL, DB_PROPERTIES); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
CREATE DATABASE IF NOT EXISTS BookStore DEFAULT CHARACTER SET utf8; | ||
|
||
USE BookStore; | ||
|
||
CREATE TABLE Books ( | ||
id BIGINT AUTO_INCREMENT, | ||
title VARCHAR(255) NOT NULL, | ||
price DECIMAL(10, 2) NOT NULL, | ||
PRIMARY KEY (id) | ||
); | ||
|
||
INSERT INTO Books (title, price) VALUES ('The Great Gatsby', 10.99); | ||
INSERT INTO Books (title, price) VALUES ('1984', 8.99); | ||
INSERT INTO Books (title, price) VALUES ('To Kill a Mockingbird', 12.50); | ||
INSERT INTO Books (title, price) VALUES ('Pride and Prejudice', 9.75); | ||
INSERT INTO Books (title, price) VALUES ('The Catcher in the Rye', 11.20); |
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.
Add Override annotation (check all methods)