-
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 #39
base: main
Are you sure you want to change the base?
Hw jv jdbc intro #39
Changes from 3 commits
b464f2e
b0e298e
0373a84
cc1d9b8
5ebd938
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,41 @@ | ||
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"); | ||
private static final String LINE_SEPARATOR = System.lineSeparator(); | ||
|
||
public static void main(String[] args) { | ||
BookDao bookDao = (BookDao) injector.getInstance(BookDao.class); | ||
|
||
Book EffectiveJava = new Book(); | ||
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. when we start naming variables from upper case letter? |
||
Book CleanCode = new Book(); | ||
EffectiveJava.setTitle("Java: Effective programming 3th ed"); | ||
EffectiveJava.setPrice(new BigDecimal(15)); | ||
CleanCode.setTitle("Clean code: A Handbook of Agile Software Craftsmanship"); | ||
CleanCode.setPrice(new BigDecimal(10)); | ||
|
||
Book createdEffectiveJavaBook = bookDao.create(EffectiveJava); | ||
Book createdCleanCodeBook = bookDao.create(CleanCode); | ||
System.out.println("-----CREATING-----"); | ||
System.out.println(createdEffectiveJavaBook); | ||
System.out.println(createdCleanCodeBook); | ||
System.out.println(LINE_SEPARATOR + "-----FIND BY ID-----"); | ||
System.out.println(bookDao.findById(createdCleanCodeBook.getId())); | ||
System.out.println(LINE_SEPARATOR + "-----FIND ALL-----"); | ||
bookDao.findAll().forEach(System.out::println); | ||
System.out.println(LINE_SEPARATOR + "-----UPDATE-----"); | ||
System.out.println("Before update: " + createdEffectiveJavaBook); | ||
createdEffectiveJavaBook.setPrice(new BigDecimal(20)); | ||
System.out.println("After update: " + bookDao.update(createdEffectiveJavaBook)); | ||
System.out.println(LINE_SEPARATOR + "-----DELETE-----"); | ||
System.out.println(bookDao.deleteById(createdEffectiveJavaBook.getId()) ? | ||
createdEffectiveJavaBook.getTitle() + " is successfully deleted" | ||
: createdEffectiveJavaBook.getTitle() + " is not deleted"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
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,111 @@ | ||||||
package mate.academy.dao; | ||||||
|
||||||
import mate.academy.exception.DataProcessingException; | ||||||
import mate.academy.lib.Dao; | ||||||
import mate.academy.model.Book; | ||||||
import mate.academy.services.ConnectionUtil; | ||||||
import java.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. try to avoid using asterix in imports |
||||||
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 query = "INSERT INTO books (title, price) VALUES (?, ?)"; | ||||||
try (Connection connection = ConnectionUtil.getConnection(); | ||||||
PreparedStatement statement = connection.prepareStatement(query, | ||||||
Statement.RETURN_GENERATED_KEYS)) { | ||||||
statement.setString(1, book.getTitle()); | ||||||
statement.setBigDecimal(2, book.getPrice()); | ||||||
int affectedRows = statement.executeUpdate(); | ||||||
checkInsertedRowsAmount(affectedRows); | ||||||
|
||||||
ResultSet generatedKeys = statement.getGeneratedKeys(); | ||||||
if(generatedKeys.next()) { | ||||||
Long id = generatedKeys.getObject(1, Long.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. whink which data should be returned the one you provide in request or the one which is actually saved? 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. This is not resolved. Here I wish to see that you populating book with data that actually in data base. Because you could do some manipulation with data and then save it, but in your implementation you will return not actual data |
||||||
book.setId(id); | ||||||
} | ||||||
} catch (SQLException e) { | ||||||
throw new DataProcessingException("Can't add new book: " + 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
|
||||||
} | ||||||
return book; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public Optional<Book> findById(Long id) { | ||||||
String query = "SELECT * FROM books WHERE id = ?"; | ||||||
try (Connection connection = ConnectionUtil.getConnection(); | ||||||
PreparedStatement statement = connection.prepareStatement(query)) { | ||||||
statement.setLong(1, id); | ||||||
ResultSet resultSet = statement.executeQuery(); | ||||||
if (resultSet.next()) { | ||||||
Book createdBook = createBook(resultSet); | ||||||
return Optional.of(createdBook); | ||||||
} | ||||||
} catch (SQLException e) { | ||||||
throw new DataProcessingException("Can't find book by id. ID = " + id, e); | ||||||
} | ||||||
return Optional.empty(); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public List<Book> findAll() { | ||||||
String query = "SELECT * FROM books"; | ||||||
try (Connection connection = ConnectionUtil.getConnection(); | ||||||
PreparedStatement statement = connection.prepareStatement(query)) { | ||||||
ResultSet resultSet = statement.executeQuery(); | ||||||
List<Book> books = new ArrayList<>(); | ||||||
while (resultSet.next()) { | ||||||
books.add(createBook(resultSet)); | ||||||
} | ||||||
return books; | ||||||
} catch (SQLException e) { | ||||||
throw new DataProcessingException("Can't 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 statement = connection.prepareStatement(query)) { | ||||||
statement.setString(1, book.getTitle()); | ||||||
statement.setBigDecimal(2, book.getPrice()); | ||||||
statement.setLong(3, book.getId()); | ||||||
int affectedRows = statement.executeUpdate(); | ||||||
checkInsertedRowsAmount(affectedRows); | ||||||
} catch (SQLException e) { | ||||||
throw new DataProcessingException("Can't update the book: " + book, 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); | ||||||
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. magic number (check others as well) |
||||||
int affectedRows = statement.executeUpdate(); | ||||||
return affectedRows > 0; | ||||||
} catch (SQLException e) { | ||||||
throw new DataProcessingException("Can't delete book by id. ID = " + id, e); | ||||||
} | ||||||
} | ||||||
|
||||||
private Book createBook(ResultSet resultSet) throws SQLException { | ||||||
Book book = new Book(); | ||||||
book.setId(resultSet.getObject("id", Long.class)); | ||||||
book.setTitle(resultSet.getString("title")); | ||||||
book.setPrice(resultSet.getBigDecimal("price")); | ||||||
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. What is price is not defined in db? |
||||||
return book; | ||||||
} | ||||||
|
||||||
private void checkInsertedRowsAmount(int affectedRows) { | ||||||
if (affectedRows < 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. magic number |
||||||
throw new RuntimeException("Expected to insert at least 1 row, but inserted 0 rows."); | ||||||
} | ||||||
} | ||||||
} |
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,42 @@ | ||
package mate.academy.model; | ||
|
||
import java.math.BigDecimal; | ||
|
||
public class Book { | ||
private Long id; | ||
private String title; | ||
private BigDecimal price; | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public BigDecimal getPrice() { | ||
return 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; | ||
} | ||
|
||
@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.services; | ||
|
||
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"; | ||
private static final Properties DB_PROPERTIES; | ||
|
||
static { | ||
DB_PROPERTIES = new Properties(); | ||
DB_PROPERTIES.put("user", "root"); | ||
DB_PROPERTIES.put("password", "Root12345"); | ||
try { | ||
Class.forName("com.mysql.cj.jdbc.Driver"); | ||
} catch (ClassNotFoundException e) { | ||
throw new RuntimeException("Can't 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 NOT NULL AUTO_INCREMENT, | ||
`title` VARCHAR(255), | ||
`price` BIGINT, | ||
PRIMARY KEY (`id`) | ||
); |
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.
What about naming constants rules?