-
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?
Conversation
src/main/java/mate/academy/Main.java
Outdated
book.setTitle("GCSE Computer Science OCR Complete Revision"); | ||
book.setPrice(BigDecimal.valueOf(400)); | ||
bookDao.create(book); | ||
System.out.println(bookDao.findById(6L)); |
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.
You don't have book with id 6 becuase you have only one book
src/main/java/mate/academy/Main.java
Outdated
book.setPrice(BigDecimal.valueOf(400)); | ||
bookDao.create(book); | ||
System.out.println(bookDao.findById(6L)); | ||
System.out.println(bookDao.deleteById(6L)); |
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.
the same
src/main/java/mate/academy/Main.java
Outdated
|
||
public static void main(String[] args) { | ||
BookDao bookDao = (BookDao) injector.getInstance(BookDao.class); | ||
Book book = new Book(); |
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.
Create several books (List) and implement daoImpl methods
|
||
@Dao | ||
public class BookDaoImpl implements BookDao { | ||
|
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.
statement.setLong(1, id); | ||
ResultSet resultSet = statement.executeQuery(); | ||
if (resultSet.next()) { | ||
String title = resultSet.getString("title"); |
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.
Create separate method for mapping resultSet to Book
@@ -0,0 +1,131 @@ | |||
package mate.academy.lib; |
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.
package mate.academy.lib; | |
package mate.academy.dao; |
@@ -0,0 +1,28 @@ | |||
package mate.academy.lib; |
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.
package mate.academy.lib; | |
package mate.academy.util; |
@@ -0,0 +1,7 @@ | |||
package mate.academy.lib; |
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.
package mate.academy.lib; | |
package mate.academy.exception; |
src/main/resources/init_db.sql
Outdated
@@ -0,0 +1,22 @@ | |||
CREATE TABLE `books` ( | |||
`id` INT NOT NULL AUTO_INCREMENT, |
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.
`id` INT NOT NULL AUTO_INCREMENT, | |
`id` BIGINT AUTO_INCREMENT, |
src/main/resources/init_db.sql
Outdated
CREATE TABLE `books` ( | ||
`id` INT NOT NULL AUTO_INCREMENT, | ||
`title` VARCHAR(255), | ||
`price` INT, |
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.
Use mysql type for BigDecimal
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.
not fixed
try (Connection connection = ConnectionUtil.getConnection(); | ||
PreparedStatement statement = | ||
connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { |
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
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 comment
The reason will be displayed to describe this comment to others. Learn more.
better save to constant
src/main/resources/init_db.sql
Outdated
CREATE TABLE `books` ( | ||
`id` INT NOT NULL AUTO_INCREMENT, | ||
`title` VARCHAR(255), | ||
`price` INT, |
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.
not fixed
No description provided.