-
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
HM JDBC #418
base: main
Are you sure you want to change the base?
HM JDBC #418
Changes from all commits
69e8344
c097cef
4c687e8
b87ce47
f18845a
f61b102
e0c219d
c05cc8a
346f873
260cd90
58e06a9
39510da
801737f
39326ca
70ae9ad
2016722
d0e6c99
950d728
2016a25
e8924fc
426dd56
878c6f9
7e7dec3
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,38 @@ | ||
package mate.academy; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import mate.academy.bookdao.BookDao; | ||
import mate.academy.lib.Injector; | ||
import mate.academy.model.Book; | ||
|
||
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 firstBook = Book.of("Effective Java", new BigDecimal(700)); | ||
Book secondBook = Book.of("Clean Code", new BigDecimal(900)); | ||
Book thirdBook = Book.of("Head of Java", new BigDecimal(600)); | ||
|
||
bookDao.create(firstBook); | ||
bookDao.create(secondBook); | ||
bookDao.create(thirdBook); | ||
|
||
List<Book> booksFromDb = bookDao.findAll(); | ||
System.out.println(booksFromDb); | ||
|
||
Optional<Book> bookById = bookDao.findById(3L); | ||
bookById.ifPresent(System.out::println); | ||
|
||
Book book = Book.of(3L, "Harry Potter", new BigDecimal(1000)); | ||
Book updated = bookDao.update(book); | ||
System.out.println(updated); | ||
|
||
bookDao.findById(3L).ifPresent(System.out::println); | ||
|
||
boolean isBookDeleted = bookDao.deleteById(3L); | ||
System.out.println(isBookDeleted); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package mate.academy.bookdao; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import mate.academy.model.Book; | ||
|
||
public interface BookDao { | ||
|
||
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 8 line |
||
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,122 @@ | ||||||||||||||||||||
package mate.academy.bookdao; | ||||||||||||||||||||
|
||||||||||||||||||||
import java.math.BigDecimal; | ||||||||||||||||||||
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.dbconnection.ConnectionUtil; | ||||||||||||||||||||
import mate.academy.exception.DataProcessingException; | ||||||||||||||||||||
import mate.academy.lib.Dao; | ||||||||||||||||||||
import mate.academy.model.Book; | ||||||||||||||||||||
|
||||||||||||||||||||
@Dao | ||||||||||||||||||||
public class BookDaoImpl implements BookDao { | ||||||||||||||||||||
@Override | ||||||||||||||||||||
public Book create(Book book) { | ||||||||||||||||||||
String sql = "INSERT INTO books(title, price) VALUES(?, ?)"; | ||||||||||||||||||||
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 |
||||||||||||||||||||
try (Connection connection = ConnectionUtil.getConnection(); | ||||||||||||||||||||
PreparedStatement preparedStatement = connection | ||||||||||||||||||||
.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { | ||||||||||||||||||||
if (storeBook(preparedStatement, book, false)) { | ||||||||||||||||||||
throw new RuntimeException("Book could not be created"); | ||||||||||||||||||||
} | ||||||||||||||||||||
Comment on lines
+25
to
+27
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 generatedKeys = preparedStatement.getGeneratedKeys(); | ||||||||||||||||||||
if (generatedKeys.next()) { | ||||||||||||||||||||
Long id = generatedKeys.getObject(1, Long.class); | ||||||||||||||||||||
book.setId(id); | ||||||||||||||||||||
} | ||||||||||||||||||||
} catch (SQLException e) { | ||||||||||||||||||||
throw new DataProcessingException("Can't add a book " + book, e); | ||||||||||||||||||||
} | ||||||||||||||||||||
return book; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Override | ||||||||||||||||||||
public Optional<Book> findById(Long id) { | ||||||||||||||||||||
String sql = "SELECT * FROM books WHERE id = ?"; | ||||||||||||||||||||
try (Connection connection = ConnectionUtil.getConnection(); | ||||||||||||||||||||
PreparedStatement preparedStatement = connection | ||||||||||||||||||||
.prepareStatement(sql)) { | ||||||||||||||||||||
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 |
||||||||||||||||||||
preparedStatement.setLong(1, id); | ||||||||||||||||||||
ResultSet resultSet = preparedStatement.executeQuery(); | ||||||||||||||||||||
if (resultSet.next()) { | ||||||||||||||||||||
return Optional.of(mapToBook(resultSet)); | ||||||||||||||||||||
} | ||||||||||||||||||||
} catch (SQLException e) { | ||||||||||||||||||||
throw new DataProcessingException("Can't get book by id " + id, e); | ||||||||||||||||||||
} | ||||||||||||||||||||
return Optional.empty(); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Override | ||||||||||||||||||||
public List<Book> findAll() { | ||||||||||||||||||||
String sql = "SELECT * FROM books"; | ||||||||||||||||||||
List<Book> books = new ArrayList<>(); | ||||||||||||||||||||
try (Connection connection = ConnectionUtil.getConnection(); | ||||||||||||||||||||
Statement statement = connection.createStatement(); | ||||||||||||||||||||
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 |
||||||||||||||||||||
ResultSet resultSet = statement.executeQuery(sql)) { | ||||||||||||||||||||
while (resultSet.next()) { | ||||||||||||||||||||
books.add(mapToBook(resultSet)); | ||||||||||||||||||||
} | ||||||||||||||||||||
} catch (SQLException e) { | ||||||||||||||||||||
throw new DataProcessingException("Can't get all books", e); | ||||||||||||||||||||
} | ||||||||||||||||||||
return books; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Override | ||||||||||||||||||||
public Book update(Book book) { | ||||||||||||||||||||
String sql = "UPDATE books SET title = ?, price = ? WHERE id = ?"; | ||||||||||||||||||||
try (Connection connection = ConnectionUtil.getConnection(); | ||||||||||||||||||||
PreparedStatement preparedStatement = connection | ||||||||||||||||||||
.prepareStatement(sql)) { | ||||||||||||||||||||
if (storeBook(preparedStatement, book, true)) { | ||||||||||||||||||||
throw new RuntimeException("Book not found"); | ||||||||||||||||||||
} | ||||||||||||||||||||
Comment on lines
+78
to
+80
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("Can't update book " + book.getTitle(), e); | ||||||||||||||||||||
} | ||||||||||||||||||||
return book; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Override | ||||||||||||||||||||
public boolean deleteById(Long id) { | ||||||||||||||||||||
String sql = "DELETE FROM books WHERE id = ?"; | ||||||||||||||||||||
try (Connection connection = ConnectionUtil.getConnection(); | ||||||||||||||||||||
PreparedStatement statement = connection.prepareStatement(sql)) { | ||||||||||||||||||||
statement.setLong(1, id); | ||||||||||||||||||||
int affectedRow = statement.executeUpdate(); | ||||||||||||||||||||
Comment on lines
+91
to
+93
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. Don't return 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 (affectedRow < 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. Don't use |
||||||||||||||||||||
throw new RuntimeException("Book not found by id " + id); | ||||||||||||||||||||
} | ||||||||||||||||||||
} catch (SQLException e) { | ||||||||||||||||||||
throw new DataProcessingException("Can't delete book by " + id, e); | ||||||||||||||||||||
} | ||||||||||||||||||||
return true; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
private Book mapToBook(ResultSet resultSet) throws SQLException { | ||||||||||||||||||||
Long id = resultSet.getLong("id"); | ||||||||||||||||||||
String title = resultSet.getString("title"); | ||||||||||||||||||||
BigDecimal price = resultSet.getBigDecimal("price"); | ||||||||||||||||||||
return Book.of(id, title, price); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
private boolean storeBook(PreparedStatement preparedStatement, | ||||||||||||||||||||
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 |
||||||||||||||||||||
Book book, boolean idPresent) throws SQLException { | ||||||||||||||||||||
if (idPresent) { | ||||||||||||||||||||
preparedStatement.setString(1, book.getTitle()); | ||||||||||||||||||||
preparedStatement.setBigDecimal(2, book.getPrice()); | ||||||||||||||||||||
preparedStatement.setLong(3, book.getId()); | ||||||||||||||||||||
return preparedStatement.executeUpdate() < 1; | ||||||||||||||||||||
} | ||||||||||||||||||||
preparedStatement.setString(1, book.getTitle()); | ||||||||||||||||||||
preparedStatement.setBigDecimal(2, book.getPrice()); | ||||||||||||||||||||
return preparedStatement.executeUpdate() < 1; | ||||||||||||||||||||
} | ||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package mate.academy.dbconnection; | ||
|
||
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/test"; | ||
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 DB_URL is incomplete and missing the actual database URL. It should be a full JDBC URL of the form jdbc:mysql://:/<database_name>, for example: "jdbc:mysql://localhost:3306/my_database". 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 DB_URL is incomplete. It should contain the full JDBC URL to the database, including the host, port, and database name. For example: jdbc:mysql://localhost:3306/myDatabase |
||
private static final Properties DB_PROPERTIES; | ||
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. It is not secure to store database credentials in code. It's better to use environment variables or a configuration file that is not included in the version control. |
||
|
||
static { | ||
DB_PROPERTIES = new Properties(); | ||
DB_PROPERTIES.put("user", "root"); | ||
DB_PROPERTIES.put("password", "databasepractice1!"); | ||
|
||
try { | ||
Class.forName("com.mysql.cj.jdbc.Driver"); | ||
} catch (ClassNotFoundException e) { | ||
throw new RuntimeException("Can't load a 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,7 @@ | ||
package mate.academy.exception; | ||
|
||
public class DataProcessingException extends RuntimeException { | ||
public DataProcessingException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package mate.academy.model; | ||
|
||
import java.math.BigDecimal; | ||
|
||
public class Book { | ||
private Long id; | ||
private String title; | ||
private BigDecimal price; | ||
|
||
private Book(Long id, String title, BigDecimal price) { | ||
this.id = id; | ||
this.title = title; | ||
this.price = price; | ||
} | ||
|
||
private Book(String title, BigDecimal price) { | ||
this.title = title; | ||
this.price = price; | ||
} | ||
|
||
public static Book of(Long id, String title, BigDecimal price) { | ||
return new Book(id, title, price); | ||
} | ||
|
||
public static Book of(String title, BigDecimal price) { | ||
return new Book(title, price); | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public void setPrice(BigDecimal price) { | ||
this.price = price; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public BigDecimal getPrice() { | ||
return 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,6 @@ | ||
CREATE TABLE books | ||
( | ||
id BIGINT PRIMARY KEY AUTO_INCREMENT, | ||
title VARCHAR(100) NOT NULL, | ||
price DECIMAL NOT NULL | ||
); |
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.