-
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
Mydb #358
base: main
Are you sure you want to change the base?
Mydb #358
Changes from 3 commits
db83d97
d7a5051
d69c9ab
9f1fec9
d2dd729
7a90f6e
e9b50b6
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,44 @@ | ||||||||
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 Book BOOK_1 = new Book("OnePiece", new BigDecimal(195)); | ||||||||
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. remove these constants |
||||||||
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); | ||||||||
|
||||||||
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. Let's create list of books here
Suggested change
|
||||||||
bookDao.create(BOOK_1); | ||||||||
bookDao.create(BOOK_2); | ||||||||
bookDao.create(BOOK_3); | ||||||||
|
||||||||
bookDao.update(BOOK_1_UPDATE); | ||||||||
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. return updated book and print it |
||||||||
|
||||||||
List<Book> books = bookDao.findAll(); | ||||||||
System.out.println("All books:"); | ||||||||
for (Book book : books) { | ||||||||
System.out.println(book); | ||||||||
} | ||||||||
|
||||||||
Optional<Book> findById = bookDao.findById(1L); | ||||||||
System.out.println("Book found id: " + findById); | ||||||||
|
||||||||
bookDao.deleteById(3L); | ||||||||
|
||||||||
List<Book> listAfterDelete = bookDao.findAll(); | ||||||||
System.out.println("List after delete by id:"); | ||||||||
for (Book book : listAfterDelete) { | ||||||||
System.out.println(book); | ||||||||
} | ||||||||
|
||||||||
} | ||||||||
} |
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,99 @@ | ||||||||||||||
package mate.academy.dao; | ||||||||||||||
|
||||||||||||||
import java.sql.Connection; | ||||||||||||||
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 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 query = "INSERT INTO books (title, price) VALUES (?, ?)"; | ||||||||||||||
try (Connection connection = ConnectionUtil.getConnection(); | ||||||||||||||
PreparedStatement preparedStatement = connection.prepareStatement(query, | ||||||||||||||
PreparedStatement.RETURN_GENERATED_KEYS)) { | ||||||||||||||
preparedStatement.setString(1, book.getTitle()); | ||||||||||||||
preparedStatement.setBigDecimal(2, book.getPrice()); | ||||||||||||||
preparedStatement.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
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. Better use this case 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. Not fixed |
||||||||||||||
ResultSet resultSet = preparedStatement.getGeneratedKeys(); | ||||||||||||||
if (resultSet.next()) { | ||||||||||||||
book.setId(resultSet.getLong(1)); | ||||||||||||||
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
Check common mistakes file |
||||||||||||||
} | ||||||||||||||
return book; | ||||||||||||||
} catch (SQLException e) { | ||||||||||||||
throw new RuntimeException("Failed to create a 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.
Suggested change
Check all places and use DataProcessingException |
||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public Optional<Book> findById(Long id) { | ||||||||||||||
String query = "SELECT * FROM books WHERE id = ?"; | ||||||||||||||
try (Connection connection = ConnectionUtil.getConnection(); | ||||||||||||||
PreparedStatement preparedStatement = connection.prepareStatement(query)) { | ||||||||||||||
preparedStatement.setLong(1, id); | ||||||||||||||
ResultSet resultSet = preparedStatement.executeQuery(); | ||||||||||||||
if (resultSet.next()) { | ||||||||||||||
return Optional.of(new Book( | ||||||||||||||
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", 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. specify id in error message |
||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public List<Book> findAll() { | ||||||||||||||
String query = "SELECT * FROM books"; | ||||||||||||||
try (Connection connection = ConnectionUtil.getConnection(); | ||||||||||||||
PreparedStatement preparedStatement = connection.prepareStatement(query)) { | ||||||||||||||
ResultSet resultSet = preparedStatement.executeQuery(); | ||||||||||||||
List<Book> books = new ArrayList<>(); | ||||||||||||||
while (resultSet.next()) { | ||||||||||||||
books.add(new Book( | ||||||||||||||
resultSet.getLong("id"), | ||||||||||||||
resultSet.getString("title"), | ||||||||||||||
resultSet.getBigDecimal("price"))); | ||||||||||||||
} | ||||||||||||||
return books; | ||||||||||||||
} catch (SQLException e) { | ||||||||||||||
throw new RuntimeException("Failed to find all books", e); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public Book update(Book book) { | ||||||||||||||
String query = "UPDATE books SET title = ?, price = ? WHERE id = ?"; | ||||||||||||||
try (Connection connection = ConnectionUtil.getConnection(); | ||||||||||||||
PreparedStatement preparedStatement = connection.prepareStatement(query)) { | ||||||||||||||
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 same with Try-w-resources 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. Not fixed |
||||||||||||||
preparedStatement.setString(1, book.getTitle()); | ||||||||||||||
preparedStatement.setBigDecimal(2, book.getPrice()); | ||||||||||||||
preparedStatement.setLong(3, book.getId()); | ||||||||||||||
preparedStatement.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
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. Not fixed |
||||||||||||||
return book; | ||||||||||||||
} catch (SQLException e) { | ||||||||||||||
throw new RuntimeException("Failed to update a book", e); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public boolean deleteById(Long id) { | ||||||||||||||
String query = "DELETE FROM books WHERE id = ?"; | ||||||||||||||
try (Connection connection = ConnectionUtil.getConnection(); | ||||||||||||||
PreparedStatement preparedStatement = connection.prepareStatement(query)) { | ||||||||||||||
preparedStatement.setLong(1, id); | ||||||||||||||
return preparedStatement.executeUpdate() > 0; | ||||||||||||||
} catch (SQLException e) { | ||||||||||||||
throw new RuntimeException("Failed to delete a book by 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. specify id in error message |
||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package mate.academy.exception; | ||
|
||
public class DataProcessingException extends RuntimeException { | ||
public DataProcessingException(String message, Throwable ex) { | ||
super(message, ex); | ||
} | ||
} |
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(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,28 @@ | ||||||
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:" | ||||||
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
any concatenation creating new String object. |
||||||
+ "//localhost:3306/mydatabase?serverTimezone=UTC"; | ||||||
private static final Properties DB_PROPERTIES; | ||||||
|
||||||
static { | ||||||
DB_PROPERTIES = new Properties(); | ||||||
DB_PROPERTIES.put("user", "mary"); | ||||||
DB_PROPERTIES.put("password", "1905Z"); | ||||||
|
||||||
try { | ||||||
Class.forName("com.mysql.cj.jdbc.Driver"); | ||||||
} catch (ClassNotFoundException e) { | ||||||
throw new RuntimeException("Can not lod JDBC driver", 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.
Suggested change
|
||||||
} | ||||||
} | ||||||
|
||||||
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 IF NOT EXISTS books ( | ||||||||
id BIGINT NOT NULL AUTO_INCREMENT, | ||||||||
title VARCHAR(255), | ||||||||
price INT, | ||||||||
PRIMARY KEY(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
|
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.
revert this. we should keep 1 blank line at the EOF