-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96f414d
commit 15c78f6
Showing
7 changed files
with
215 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package mate.academy; | ||
|
||
public class DataProcessingException extends RuntimeException { | ||
public DataProcessingException(String message, Throwable ex) { | ||
super(message, ex); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,25 @@ | ||
package mate.academy; | ||
|
||
import mate.academy.dao.BookDao; | ||
import mate.academy.lib.Injector; | ||
import mate.academy.model.Book; | ||
import java.math.BigDecimal; | ||
import java.util.Optional; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
private static final Injector injector = Injector.getInstance("mate.academy.dao"); | ||
|
||
public static void main(String[] args) { | ||
BookDao bookDao = (BookDao) injector.getInstance(BookDao.class); | ||
Book book = new Book(); | ||
book.setTitle("Sample"); | ||
book.setPrice(BigDecimal.valueOf(5.99)); | ||
bookDao.create(book); | ||
Long bookId = 1L; | ||
Optional<Book> foundBook = bookDao.findById(bookId); | ||
bookDao.findAll(); | ||
Book updatedBook = foundBook.orElseThrow(); | ||
bookDao.update(updatedBook); | ||
bookDao.deleteById(bookId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package mate.academy.dao; | ||
|
||
import mate.academy.model.Book; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface BookDao { | ||
Book create(Book book); | ||
Optional<Book> findById(Long id); | ||
List<Book> findAll(); | ||
Book update(Book book); | ||
boolean deleteById(Long id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package mate.academy.dao; | ||
|
||
import mate.academy.DataProcessingException; | ||
import mate.academy.model.Book; | ||
import mate.academy.lib.Dao; | ||
import java.math.BigDecimal; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
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 java.util.Properties; | ||
|
||
@Dao | ||
public class BookDaoImpl implements BookDao { | ||
private static final String CREATE_QUERY = "INSERT INTO books (title, price) VALUES (?, ?)"; | ||
private static final String FIND_BY_ID_QUERY = "SELECT * FROM books WHERE id = ?"; | ||
private static final String FIND_ALL_QUERY = "SELECT * FROM books"; | ||
private static final String UPDATE_QUERY = "UPDATE books SET title = ?, price = ? WHERE id = ?"; | ||
private static final String DELETE_BY_ID_QUERY = "DELETE FROM books WHERE id = ?"; | ||
|
||
@Override | ||
public Book create(Book book) { | ||
try (Connection connection = getConnection(); | ||
PreparedStatement statement = connection.prepareStatement(CREATE_QUERY, | ||
PreparedStatement.RETURN_GENERATED_KEYS)) { | ||
setBookParameters(statement, book); | ||
statement.executeUpdate(); | ||
try (ResultSet generatedKeys = statement.getGeneratedKeys()) { | ||
if (generatedKeys.next()) { | ||
book.setId(generatedKeys.getLong(1)); | ||
} else { | ||
throw new SQLException("Creating book " + book + " failed, no ID obtained."); | ||
} | ||
} | ||
return book; | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Failed to create book " + book, e); | ||
} | ||
} | ||
|
||
@Override | ||
public Optional<Book> findById(Long id) { | ||
try (Connection connection = getConnection(); | ||
PreparedStatement statement = connection.prepareStatement(FIND_BY_ID_QUERY)) { | ||
|
||
statement.setLong(1, id); | ||
try (ResultSet resultSet = statement.executeQuery()) { | ||
if (resultSet.next()) { | ||
return Optional.of(parseBook(resultSet)); | ||
} | ||
} | ||
return Optional.empty(); | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Failed to find book by ID " + id, e); | ||
} | ||
} | ||
|
||
@Override | ||
public List<Book> findAll() { | ||
try (Connection connection = getConnection(); | ||
PreparedStatement statement = connection.prepareStatement(FIND_ALL_QUERY); | ||
ResultSet resultSet = statement.executeQuery()) { | ||
|
||
List<Book> books = new ArrayList<>(); | ||
while (resultSet.next()) { | ||
books.add(parseBook(resultSet)); | ||
} | ||
return books; | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Failed to get all books", e); | ||
} | ||
} | ||
|
||
@Override | ||
public Book update(Book book) { | ||
try (Connection connection = getConnection(); | ||
PreparedStatement statement = connection.prepareStatement(UPDATE_QUERY)) { | ||
setBookParameters(statement, book); | ||
statement.setLong(3, book.getId()); | ||
statement.executeUpdate(); | ||
return book; | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Failed to update book" + book, e); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean deleteById(Long id) { | ||
try (Connection connection = getConnection(); | ||
PreparedStatement statement = connection.prepareStatement(DELETE_BY_ID_QUERY)) { | ||
|
||
statement.setLong(1, id); | ||
int rowsDeleted = statement.executeUpdate(); | ||
return rowsDeleted > 0; | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Failed to delete book by ID " + id, e); | ||
} | ||
} | ||
|
||
private Connection getConnection() throws SQLException { | ||
Properties dbProperties = new Properties(); | ||
dbProperties.put("user", "root"); | ||
dbProperties.put("password", "Vika07012000"); | ||
return DriverManager.getConnection("jdbc:mysql://localhost:3306/books", dbProperties); | ||
} | ||
|
||
private Book parseBook(ResultSet resultSet) throws SQLException { | ||
Long id = resultSet.getObject("id", Long.class); | ||
String title = resultSet.getString("title"); | ||
BigDecimal price = resultSet.getBigDecimal("price"); | ||
Book book = new Book(); | ||
book.setId(id); | ||
book.setTitle(title); | ||
book.setPrice(price); | ||
return book; | ||
} | ||
|
||
private void setBookParameters(PreparedStatement statement, Book book) throws SQLException { | ||
statement.setString(1, book.getTitle()); | ||
statement.setBigDecimal(2, book.getPrice()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package mate.academy.model; | ||
|
||
import java.math.BigDecimal; | ||
|
||
public class Book { | ||
private Long id; | ||
private String title; | ||
private BigDecimal price; | ||
|
||
public Book() { | ||
} | ||
|
||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CREATE TABLE `books` ( | ||
`id` BIGINT NOT NULL AUTO_INCREMENT, | ||
`title` VARCHAR(255) NOT NULL, | ||
`price` DECIMAL(10, 2) NOT NULL, | ||
PRIMARY KEY (id) | ||
); |