Skip to content
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

commit #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,19 @@
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.18.20.0</version>
<scope>provided</scope>
</dependency>

</dependencies>

</project>
23 changes: 22 additions & 1 deletion src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
package mate.academy;

import mate.academy.dao.BookDao;
import mate.academy.lib.Injector;
import mate.academy.model.Book;

import java.math.BigDecimal;

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("Harry Potter", BigDecimal.valueOf(400));
Book book2 = new Book("Harry Potter2", BigDecimal.valueOf(450));
Book book3 = new Book("Harry Potter3", BigDecimal.valueOf(490));
System.out.println("added " + bookDao.create(book));
System.out.println("added " + bookDao.create(book2));
System.out.println("added " + bookDao.create(book3));
System.out.println("all books: " + bookDao.findAll());
book3.setTitle("Will");
book3.setPrice(BigDecimal.valueOf(300));
System.out.println("updated " + bookDao.update(book3));
System.out.println("all books with update " + bookDao.findAll());
System.out.println("find by id " + bookDao.findById(3L));
bookDao.deleteById(3l);
System.out.println("all books after delete " + bookDao.findAll());
}
}
14 changes: 14 additions & 0 deletions src/main/java/mate/academy/dao/BookDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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);
}
103 changes: 103 additions & 0 deletions src/main/java/mate/academy/dao/impl/BookDaoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package mate.academy.dao.impl;

import mate.academy.dao.BookDao;
import mate.academy.exception.DataProcessingException;
import mate.academy.lib.Dao;
import mate.academy.model.Book;
import mate.academy.util.ConnectionUtil;

import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Dao
public class BookDaoImpl implements BookDao {
@Override
public Book create(Book book) {
String insert = "INSERT INTO books (title, price) VALUES (?, ?)";
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(insert, Statement.RETURN_GENERATED_KEYS)) {
statement.setString(1, book.getTitle());
statement.setBigDecimal(2, book.getPrice());
statement.executeUpdate();
ResultSet generatedKeys = statement.getGeneratedKeys();
if (generatedKeys.next()) {
Long id = generatedKeys.getObject(1, Long.class);
book.setId(id);
}
} catch (SQLException e) {
throw new DataProcessingException("Can't add book to db ", e);
}
return book;
}

@Override
public Optional<Book> findById(Long id) {
String query = "SELECT id, title, price FROM books WHERE id = ?;";
Book book = null;
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
statement.setLong(1, id);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
book = getBookFromResultSet(resultSet);
}
} catch(SQLException e){
throw new DataProcessingException("Couldn't find book by id " + id, e);
}
return Optional.ofNullable(book);
}

@Override
public List<Book> findAll () {
String query = "SELECT id, title, price FROM books";
List<Book> bookList = new ArrayList<>();
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
bookList.add(getBookFromResultSet(resultSet));
}
} catch (SQLException e) {
throw new DataProcessingException("Couldn't get all books", e);
}
return bookList;
}

@Override
public Book update (Book book) {
String query = "UPDATE books SET title = ?, price = ? WHERE id = ?";
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, book.getTitle());
statement.setBigDecimal(2, book.getPrice());
statement.setLong(3, book.getId());
statement.executeUpdate();
} catch (SQLException e) {
throw new DataProcessingException("Couldn't update book by id " + book.getId(), e);
}
return book;
}

@Override
public boolean deleteById (Long id) {
String query = "DELETE FROM books WHERE id = ?";
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
statement.setLong(1, id);
int deletedRows = statement.executeUpdate();
return deletedRows > 0;
} catch (SQLException e) {
throw new DataProcessingException("Couldn't delete book by id " + id, e);
}
}

private Book getBookFromResultSet(ResultSet resultSet) throws SQLException {
long id = resultSet.getObject(1, Long.class);
String title = resultSet.getString("title");
BigDecimal price = resultSet.getBigDecimal("price");
Book book = new Book(id, title, price);
return book;
}
}
11 changes: 11 additions & 0 deletions src/main/java/mate/academy/exception/DataProcessingException.java
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) {
super(message);
}

public DataProcessingException(String message, Throwable cause) {
super(message, cause);
}
}
53 changes: 53 additions & 0 deletions src/main/java/mate/academy/model/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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 Book(String title, BigDecimal price) {
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
+ '}';
}
}
32 changes: 32 additions & 0 deletions src/main/java/mate/academy/util/ConnectionUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package mate.academy.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionUtil {
public static final String URL = "jdbc:mysql://localhost:3306/library";
public static final String USERNAME = "root";
public static final String PASSWORD = "olegAndriy";
private static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";

static {
try {
Class.forName(JDBC_DRIVER);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Can't load JDBC driver for MySQL", e);
}
}

public static Connection getConnection() {
Properties dbProperties = new Properties();
dbProperties.setProperty("user", USERNAME);
dbProperties.setProperty("password", PASSWORD);
try {
return DriverManager.getConnection(URL, dbProperties);
} catch (SQLException e) {
throw new RuntimeException("Can't create connection to DataBase", e);
}
}
}
6 changes: 6 additions & 0 deletions src/main/resources/init_db.sql
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 NOT NULL,
PRIMARY KEY (id)
);
Loading