-
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
Hw jv jdbc intro #359
base: main
Are you sure you want to change the base?
Hw jv jdbc intro #359
Changes from 7 commits
6317543
bc5b093
3977e7d
5a6c191
c8eae8f
34a3878
92bc63f
180a878
e42429d
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,33 @@ | ||||||||
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"); | ||||||||
private static final BookDao bookDao = (BookDao) injector.getInstance(BookDao.class); | ||||||||
|
||||||||
public static void main(String[] args) { | ||||||||
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.
Suggested change
|
||||||||
Book book = new Book("book", BigDecimal.valueOf(100)); | ||||||||
Book createdBook = bookDao.create(book); | ||||||||
System.out.println(createdBook); | ||||||||
|
||||||||
Optional<Book> getBookById = bookDao.findById(1L); | ||||||||
System.out.println("Book by id " + getBookById); | ||||||||
|
||||||||
book.setTitle("Updated Book"); | ||||||||
book.setPrice(BigDecimal.valueOf(200)); | ||||||||
Book updatedBook = bookDao.update(book); | ||||||||
System.out.println("Updated Book: " + updatedBook); | ||||||||
|
||||||||
List<Book> getAllBook = bookDao.findAll(); | ||||||||
System.out.println("All Books : " + getAllBook); | ||||||||
|
||||||||
boolean deleteBook = bookDao.deleteById(3L); | ||||||||
System.out.println("Deleted book: " + deleteBook); | ||||||||
} | ||||||||
} |
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,115 @@ | ||||||||||||||
package mate.academy.dao.impl; | ||||||||||||||
|
||||||||||||||
import java.sql.Connection; | ||||||||||||||
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.dao.BookDao; | ||||||||||||||
import mate.academy.exception.DataProcessingException; | ||||||||||||||
import mate.academy.lib.Dao; | ||||||||||||||
import mate.academy.model.Book; | ||||||||||||||
import mate.academy.util.db.ConnectionUtil; | ||||||||||||||
|
||||||||||||||
@Dao | ||||||||||||||
public class BookDaoImpl implements BookDao { | ||||||||||||||
private static final String CANT_CREATE = "Can't store the book to the DB"; | ||||||||||||||
private static final String CANT_FIND_BY_ID = "Can't find the book with id = "; | ||||||||||||||
private static final String CANT_FIND_ALL = "Can't find all books"; | ||||||||||||||
private static final String CANT_UPDATE = "Can't update the book with id = "; | ||||||||||||||
private static final String CANT_DELETE = "Can't delete the book with id = "; | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public Book create(Book book) { | ||||||||||||||
String insertQuery = "INSERT INTO books (title,price) VALUES (?,?);"; | ||||||||||||||
try (Connection connection = ConnectionUtil.getConnection(); | ||||||||||||||
PreparedStatement statement = connection.prepareStatement( | ||||||||||||||
insertQuery, Statement.RETURN_GENERATED_KEYS)) { | ||||||||||||||
statement.setString(1, book.getTitle()); | ||||||||||||||
statement.setBigDecimal(2, book.getPrice()); | ||||||||||||||
statement.executeUpdate(); | ||||||||||||||
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.
Suggested change
|
||||||||||||||
ResultSet generatedKey = statement.getGeneratedKeys(); | ||||||||||||||
if (generatedKey.next()) { | ||||||||||||||
Long id = generatedKey.getObject(1, Long.class); | ||||||||||||||
book.setId(id); | ||||||||||||||
} | ||||||||||||||
} catch (SQLException e) { | ||||||||||||||
throw new DataProcessingException(CANT_CREATE, e); | ||||||||||||||
} | ||||||||||||||
return book; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public Optional<Book> findById(Long id) { | ||||||||||||||
String getByIdQuery = "SELECT * FROM books WHERE id = ? AND is_deleted = 0"; | ||||||||||||||
try (Connection connection = ConnectionUtil.getConnection(); | ||||||||||||||
PreparedStatement statement = connection.prepareStatement(getByIdQuery)) { | ||||||||||||||
statement.setLong(1, id); | ||||||||||||||
ResultSet resultSet = statement.executeQuery(); | ||||||||||||||
if (resultSet.next()) { | ||||||||||||||
return Optional.of(createBook(resultSet)); | ||||||||||||||
} | ||||||||||||||
} catch (SQLException e) { | ||||||||||||||
throw new DataProcessingException(CANT_FIND_BY_ID + id, e); | ||||||||||||||
} | ||||||||||||||
return Optional.empty(); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public List<Book> findAll() { | ||||||||||||||
String getAllQuery = "SELECT * FROM books WHERE is_deleted = 0"; | ||||||||||||||
List<Book> books = new ArrayList<>(); | ||||||||||||||
try (Connection connection = ConnectionUtil.getConnection(); | ||||||||||||||
PreparedStatement statement = connection.prepareStatement(getAllQuery)) { | ||||||||||||||
ResultSet resultSet = statement.executeQuery(); | ||||||||||||||
while (resultSet.next()) { | ||||||||||||||
books.add(createBook(resultSet)); | ||||||||||||||
} | ||||||||||||||
} catch (SQLException e) { | ||||||||||||||
throw new DataProcessingException(CANT_FIND_ALL, e); | ||||||||||||||
} | ||||||||||||||
return books; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public Book update(Book book) { | ||||||||||||||
String updateQuery = "UPDATE books SET title = ?, price = ? " | ||||||||||||||
+ "WHERE id = ? AND is_deleted = 0"; | ||||||||||||||
Long 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.
Suggested change
|
||||||||||||||
try (Connection connection = ConnectionUtil.getConnection(); | ||||||||||||||
PreparedStatement statement = connection.prepareStatement(updateQuery)) { | ||||||||||||||
statement.setString(1, book.getTitle()); | ||||||||||||||
statement.setBigDecimal(2, book.getPrice()); | ||||||||||||||
statement.setLong(3, 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.
Suggested change
|
||||||||||||||
int affectedRows = statement.executeUpdate(); | ||||||||||||||
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.
Suggested change
|
||||||||||||||
if (affectedRows > 0) { | ||||||||||||||
return book; | ||||||||||||||
} | ||||||||||||||
} catch (SQLException e) { | ||||||||||||||
throw new DataProcessingException(CANT_UPDATE + id, e); | ||||||||||||||
} | ||||||||||||||
throw new DataProcessingException(CANT_FIND_BY_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.
Suggested change
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public boolean deleteById(Long id) { | ||||||||||||||
String deletedQuery = "UPDATE books SET is_deleted = 1 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 TRUE and FALSE instead |
||||||||||||||
try (Connection connection = ConnectionUtil.getConnection(); | ||||||||||||||
PreparedStatement statement = connection.prepareStatement(deletedQuery)) { | ||||||||||||||
statement.setLong(1, id); | ||||||||||||||
return 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.
Suggested change
|
||||||||||||||
} catch (SQLException e) { | ||||||||||||||
throw new DataProcessingException(CANT_DELETE + id, e); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
private Book createBook(ResultSet resultSet) throws SQLException { | ||||||||||||||
return new Book( | ||||||||||||||
resultSet.getLong("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. resultSet.getObject("id", Long.class); |
||||||||||||||
resultSet.getString("title"), | ||||||||||||||
resultSet.getBigDecimal("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) { | ||
super(message); | ||
} | ||
|
||
public DataProcessingException(String message, Throwable exception) { | ||
super(message, exception); | ||
} | ||
} |
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() { | ||
} | ||
|
||
public Book(String title, BigDecimal price) { | ||
this.title = title; | ||
this.price = 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 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,26 @@ | ||
package mate.academy.util.db; | ||
|
||
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/mate"; | ||
private static final Properties DB_PROPERTIES; | ||
|
||
static { | ||
DB_PROPERTIES = new Properties(); | ||
DB_PROPERTIES.put("user", "root"); | ||
DB_PROPERTIES.put("password", "12345678"); | ||
try { | ||
Class.forName("com.mysql.cj.jdbc.Driver"); | ||
} catch (ClassNotFoundException e) { | ||
throw new RuntimeException("Can not load JDBC driver", 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,6 @@ | ||||||||
CREATE TABLE books ( | ||||||||
id BIGINT AUTO_INCREMENT PRIMARY KEY, | ||||||||
title VARCHAR(255) NOT NULL, | ||||||||
price DECIMAL(6, 2) NOT NULL, | ||||||||
is_deleted TINYINT NOT NULL DEFAULT 0 | ||||||||
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; | ||||||||
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.
Suggested change
|
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.