Skip to content

Commit

Permalink
'database is done'
Browse files Browse the repository at this point in the history
  • Loading branch information
MarynaBodina committed May 29, 2024
1 parent 9f1fec9 commit d2dd729
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
33 changes: 15 additions & 18 deletions src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,33 @@

public class Main {
private static final Injector INJECTOR = Injector.getInstance("mate.academy");
private static final Book BOOK_1 = new Book("OnePiece", new BigDecimal(195));
private static final Book BOOK_1_UPDATE = new Book(1L, "OnePice", new BigDecimal(250));
private static final Book BOOK_2 = new Book("Naruto", new BigDecimal(235));
private static final Book BOOK_3 = new Book("Jujutsu Kaisen", new BigDecimal(243));

public static void main(String[] args) {
BookDao bookDao = (BookDao) INJECTOR.getInstance(BookDao.class);

bookDao.create(BOOK_1);
bookDao.create(BOOK_2);
bookDao.create(BOOK_3);
List<Book> books = List.of(
new Book("OnePiece", new BigDecimal(195)),
new Book("Naruto", new BigDecimal(235)),
new Book("Jujutsu Kaisen", new BigDecimal(243))
);

bookDao.update(BOOK_1_UPDATE);
books.forEach(bookDao::create);

List<Book> books = bookDao.findAll();
Book book1Update = new Book(1L, "OnePiece", new BigDecimal(250));
Book updatedBook = bookDao.update(book1Update);
System.out.println("Updated book: " + updatedBook);

List<Book> allBooks = bookDao.findAll();
System.out.println("All books:");
for (Book book : books) {
System.out.println(book);
}
allBooks.forEach(System.out::println);

Optional<Book> findById = bookDao.findById(1L);
System.out.println("Book found id: " + findById);
Optional<Book> bookById = bookDao.findById(1L);
System.out.println("Book found by id: " + bookById.orElse(null));

bookDao.deleteById(3L);

List<Book> listAfterDelete = bookDao.findAll();
System.out.println("List after delete by id:");
for (Book book : listAfterDelete) {
System.out.println(book);
}

listAfterDelete.forEach(System.out::println);
}
}
26 changes: 14 additions & 12 deletions src/main/java/mate/academy/dao/BookDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;
Expand All @@ -17,18 +18,19 @@ public class BookDaoImpl implements BookDao {
public Book create(Book book) {
String query = "INSERT INTO books (title, price) VALUES (?, ?)";
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query,
PreparedStatement preparedStatement = connection.prepareStatement(query,
PreparedStatement.RETURN_GENERATED_KEYS)) {
preparedStatement.setString(1, book.getTitle());
preparedStatement.setBigDecimal(2, book.getPrice());
preparedStatement.executeUpdate();
ResultSet resultSet = preparedStatement.getGeneratedKeys();
if (resultSet.next()) {
book.setId(resultSet.getLong(1));
ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
if (generatedKeys.next()) {
Long id = generatedKeys.getLong(1);
book.setId(id);
}
return book;
} catch (SQLException e) {
throw new RuntimeException("Failed to create a book", e);
throw new DataProcessingException("Failed to create a book", e);
}
}

Expand All @@ -41,13 +43,13 @@ public Optional<Book> findById(Long id) {
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
return Optional.of(new Book(
resultSet.getLong("id"),
resultSet.getString("title"),
resultSet.getBigDecimal("price")));
resultSet.getLong("id"),
resultSet.getString("title"),
resultSet.getBigDecimal("price")));
}
return Optional.empty();
} catch (SQLException e) {
throw new RuntimeException("Failed to find a book by id: " + id, e);
throw new DataProcessingException("Failed to find a book by id: " + id, e);
}
}

Expand All @@ -66,7 +68,7 @@ public List<Book> findAll() {
}
return books;
} catch (SQLException e) {
throw new RuntimeException("Failed to find all books", e);
throw new DataProcessingException("Failed to find all books", e);
}
}

Expand All @@ -81,7 +83,7 @@ public Book update(Book book) {
preparedStatement.executeUpdate();
return book;
} catch (SQLException e) {
throw new RuntimeException("Failed to update a book", e);
throw new DataProcessingException("Failed to update a book", e);
}
}

Expand All @@ -93,7 +95,7 @@ public boolean deleteById(Long id) {
preparedStatement.setLong(1, id);
return preparedStatement.executeUpdate() > 0;
} catch (SQLException e) {
throw new RuntimeException("Failed to delete a book with id: " + id, e);
throw new DataProcessingException("Failed to delete a book with id: " + id, e);
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/mate/academy/util/ConnectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import java.util.Properties;

public class ConnectionUtil {
private static final String DB_URL = "jdbc:mysql:"
+ "//localhost:3306/mydatabase?serverTimezone=UTC";
private static final String DB_URL =
"jdbc:mysql://localhost:3306/mydatabase?serverTimezone=UTC";
private static final Properties DB_PROPERTIES;

static {
Expand Down

0 comments on commit d2dd729

Please sign in to comment.