-
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
first commit #381
base: main
Are you sure you want to change the base?
first commit #381
Changes from 1 commit
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package mate.academy; | ||
|
||
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", "v1f9v7f7"); | ||
|
||
try { | ||
Class.forName("com.mysql.cj.jdbc.Driver"); | ||
} catch (ClassNotFoundException e) { | ||
throw new RuntimeException("Could not load MySQL 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 |
---|---|---|
@@ -1,7 +1,24 @@ | ||
package mate.academy; | ||
|
||
import java.math.BigDecimal; | ||
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"); | ||
|
||
public static void main(String[] args) { | ||
BookDao bookDao = (BookDao) injector.getInstance(BookDao.class); | ||
Book book = new Book(); | ||
book.setTitle("Java Programming"); | ||
book.setPrice(new BigDecimal("99.99")); | ||
|
||
System.out.println(bookDao.create(book)); | ||
System.out.println(bookDao.findById(4L)); | ||
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. you don't have book with id = 4 |
||
System.out.println(bookDao.findAll()); | ||
System.out.println(bookDao.update(book)); | ||
System.out.println(bookDao.deleteById(4L)); | ||
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 |
||
} | ||
} |
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,126 @@ | ||||||
package mate.academy.dao; | ||||||
|
||||||
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.ConnectionUtil; | ||||||
import mate.academy.exception.DataProcessingException; | ||||||
import mate.academy.lib.Dao; | ||||||
import mate.academy.model.Book; | ||||||
|
||||||
@Dao | ||||||
public class BookDaoImpl implements 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.
Suggested change
|
||||||
@Override | ||||||
public Book create(Book book) { | ||||||
String query = "INSERT INTO book (title, price) VALUES (?, ?);"; | ||||||
|
||||||
try (Connection connection = ConnectionUtil.getConnection(); | ||||||
PreparedStatement preparedStatement = connection.prepareStatement( | ||||||
query, Statement.RETURN_GENERATED_KEYS)) { | ||||||
preparedStatement.setString(1, book.getTitle()); | ||||||
preparedStatement.setBigDecimal(2, book.getPrice()); | ||||||
int affectRows = preparedStatement.executeUpdate(); | ||||||
if (affectRows == 0) { | ||||||
throw new DataProcessingException( | ||||||
"Expected to add at least one row but added 0", | ||||||
new SQLException()); | ||||||
} | ||||||
ResultSet generatedKeys = preparedStatement.getGeneratedKeys(); | ||||||
if (generatedKeys.next()) { | ||||||
Long id = generatedKeys.getObject(1, Long.class); | ||||||
book.setId(id); | ||||||
} | ||||||
} catch (SQLException e) { | ||||||
throw new DataProcessingException("Failed to create book " + book, e); | ||||||
} | ||||||
return book; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public Optional<Book> findById(Long id) { | ||||||
String query = "SELECT * FROM book 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(creatingBook(resultSet, id)); | ||||||
} | ||||||
} catch (SQLException e) { | ||||||
throw new DataProcessingException("Failed to find book by id: " + id, e); | ||||||
} | ||||||
return Optional.empty(); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public List<Book> findAll() { | ||||||
String query = "SELECT * FROM book;"; | ||||||
List<Book> books = new ArrayList<>(); | ||||||
try (Connection connection = ConnectionUtil.getConnection(); | ||||||
PreparedStatement preparedStatement = connection.prepareStatement(query)) { | ||||||
ResultSet resultSet = preparedStatement.executeQuery(); | ||||||
while (resultSet.next()) { | ||||||
Long id = resultSet.getObject("id", Long.class); | ||||||
books.add(creatingBook(resultSet, id)); | ||||||
return books; | ||||||
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("Failed to find books", e); | ||||||
} | ||||||
return new ArrayList<>(); | ||||||
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 Book update(Book book) { | ||||||
String query = "UPDATE book SET title = ?, price = ? WHERE id = ?;"; | ||||||
try (Connection connection = ConnectionUtil.getConnection(); | ||||||
PreparedStatement preparedStatement = connection.prepareStatement( | ||||||
query, Statement.RETURN_GENERATED_KEYS)) { | ||||||
preparedStatement.setString(1, book.getTitle()); | ||||||
preparedStatement.setBigDecimal(2, book.getPrice()); | ||||||
preparedStatement.setLong(3, book.getId()); | ||||||
int affectRows = preparedStatement.executeUpdate(); | ||||||
if (affectRows == 0) { | ||||||
throw new DataProcessingException( | ||||||
"Expected to update at least one row but updated 0", | ||||||
new SQLException()); | ||||||
} | ||||||
} catch (SQLException e) { | ||||||
throw new DataProcessingException("Failed to update book " + book, e); | ||||||
} | ||||||
return book; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public boolean deleteById(Long id) { | ||||||
String query = "DELETE FROM book 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 DataProcessingException("Can't delete an object with id: " + id, e); | ||||||
} | ||||||
} | ||||||
|
||||||
private Book creatingBook(ResultSet resultSet, Long 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
|
||||||
try { | ||||||
String title = resultSet.getString("title"); | ||||||
BigDecimal price = resultSet.getObject("price", BigDecimal.class); | ||||||
Book book = new Book(); | ||||||
book.setId(id); | ||||||
book.setTitle(title); | ||||||
book.setPrice(price); | ||||||
return book; | ||||||
} catch (SQLException e) { | ||||||
throw new DataProcessingException("Can't create a book with id = " + id, e); | ||||||
} | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package mate.academy.exception; | ||
|
||
public class DataProcessingException extends RuntimeException { | ||
public DataProcessingException(String message, Throwable 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 BigDecimal getPrice() { | ||
return price; | ||
} | ||
|
||
public void setPrice(BigDecimal price) { | ||
this.price = price; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
@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 @@ | ||
USE test; | ||
CREATE TABLE book ( | ||
id BIGINT AUTO_INCREMENT PRIMARY KEY, | ||
title VARCHAR(255), | ||
price DECIMAL(10, 2) | ||
); |
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.