-
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
connected to DB, implemented DAO #370
base: main
Are you sure you want to change the base?
Changes from 2 commits
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,36 @@ | ||
package mate.academy; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.List; | ||
import mate.academy.dao.BookDao; | ||
import mate.academy.lib.Injector; | ||
import mate.academy.model.Book; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
private static final Injector injector = Injector.getInstance("mate.academy"); | ||
|
||
public static void main(String[] args) { | ||
BookDao bookDao = (BookDao) injector.getInstance(BookDao.class); | ||
List<Book> books = List.of( | ||
new Book("Whats the Point of Maths?", | ||
BigDecimal.valueOf(200)), | ||
new Book("Upstream Intermediate Workbook", | ||
BigDecimal.valueOf(350)), | ||
new Book("See Inside Your Body", | ||
BigDecimal.valueOf(400)), | ||
new Book("Henry Ford: My life and My work", | ||
BigDecimal.valueOf(250)), | ||
new Book("GCSE Edexcel Mathematics for the Grade 9-1 Course", | ||
BigDecimal.valueOf(350)) | ||
); | ||
for (Book book : books) { | ||
bookDao.create(book); | ||
} | ||
System.out.println(bookDao.findById(3L)); | ||
System.out.println(bookDao.deleteById(5L)); | ||
System.out.println(bookDao.findAll().toString()); | ||
Book book = books.get(4); | ||
book.setPrice(BigDecimal.valueOf(225)); | ||
System.out.println(bookDao.update(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,129 @@ | ||
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.exception.DataProcessingException; | ||
import mate.academy.model.Book; | ||
import mate.academy.util.ConnectionUtil; | ||
|
||
@Dao | ||
public class BookDaoImpl implements BookDao { | ||
@Override | ||
public Book create(Book book) { | ||
String sql = "INSERT INTO books (title, price) VALUES (?, ?)"; | ||
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement statement = | ||
connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { | ||
|
||
statement.setString(1, book.getTitle()); | ||
statement.setBigDecimal(2, book.getPrice()); | ||
int updatedRows = statement.executeUpdate(); | ||
if (updatedRows < 1) { | ||
throw new RuntimeException( | ||
"expected to insert at least one row, but instead 0 rows" | ||
); | ||
} | ||
ResultSet generatedKeys = statement.getGeneratedKeys(); | ||
if (generatedKeys.next()) { | ||
Long id = generatedKeys.getObject(1, Long.class); | ||
book.setId(id); | ||
} | ||
|
||
} catch (SQLException e) { | ||
throw new DataProcessingException("Can't create a connection to a DB", e); | ||
} | ||
return book; | ||
} | ||
|
||
@Override | ||
public Optional<Book> findById(Long id) { | ||
String sql = "SELECT * FROM books WHERE id = ?"; | ||
|
||
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement statement = connection.prepareStatement(sql)) { | ||
|
||
statement.setLong(1, id); | ||
ResultSet resultSet = statement.executeQuery(); | ||
List<Book> list = mappingResSet(resultSet); | ||
|
||
if (!list.isEmpty()) { | ||
return Optional.of(list.get(0)); | ||
} | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Can't create a connection to a DB", e); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public List<Book> findAll() { | ||
String sql = "SELECT * FROM books"; | ||
|
||
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement statement = connection.prepareStatement(sql)) { | ||
|
||
ResultSet resultSet = statement.executeQuery(); | ||
return mappingResSet(resultSet); | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Can't create a connection to a DB", e); | ||
} | ||
} | ||
|
||
@Override | ||
public Book update(Book book) { | ||
String sql = "UPDATE books SET price = ? WHERE title = ?"; | ||
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement statement = connection.prepareStatement(sql)) { | ||
|
||
statement.setBigDecimal(1, book.getPrice()); | ||
statement.setString(2, book.getTitle()); | ||
int updatedRows = statement.executeUpdate(); | ||
if (updatedRows < 1) { | ||
throw new RuntimeException( | ||
"expected to update at least one row, but instead 0 rows" | ||
); | ||
} | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Can't create a connection to a DB", 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 updatedRows = statement.executeUpdate(); | ||
return updatedRows > 0; | ||
} catch (SQLException e) { | ||
throw new DataProcessingException("Can't create a connection to a DB", e); | ||
} | ||
} | ||
|
||
private List<Book> mappingResSet(ResultSet resultSet) throws SQLException { | ||
List<Book> books = new ArrayList<>(); | ||
while (resultSet.next()) { | ||
long id = resultSet.getObject("id", Long.class); | ||
String title = resultSet.getString("title"); | ||
BigDecimal price = resultSet.getBigDecimal("price"); | ||
|
||
Book book = new Book(); | ||
book.setId(id); | ||
book.setTitle(title); | ||
book.setPrice(price); | ||
books.add(book); | ||
} | ||
return books; | ||
} | ||
} |
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,50 @@ | ||
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 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,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://localhost:3306/task"; | ||
|
||
private static final Properties DB_PROPERTIES; | ||
|
||
static { | ||
DB_PROPERTIES = new Properties(); | ||
DB_PROPERTIES.put("user", "root"); | ||
DB_PROPERTIES.put("password", "1987"); | ||
|
||
try { | ||
Class.forName("com.mysql.cj.jdbc.Driver"); | ||
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 save to constant |
||
} 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,22 @@ | ||
CREATE TABLE `books` ( | ||
`id` BIGINT AUTO_INCREMENT, | ||
`title` VARCHAR(255), | ||
`price` INT, | ||
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 mysql type for BigDecimal 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 |
||
PRIMARY KEY (`id`) | ||
); | ||
|
||
INSERT INTO books (title, price) VALUES( | ||
'Whats the Point of Maths?', 200 | ||
); | ||
INSERT INTO books (title, price) VALUES( | ||
'Upstream Intermediate Workbook', 350 | ||
); | ||
INSERT INTO books (title, price) VALUES( | ||
'See Inside Your Body', 400 | ||
); | ||
INSERT INTO books (title, price) VALUES( | ||
'Henry Ford: My life and My work', 250 | ||
); | ||
INSERT INTO books (title, price) VALUES( | ||
'GCSE Edexcel Mathematics for the Grade 9-1 Course', 350 | ||
); |
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.
try with resources use for read ioperations