-
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
jdbc-intro-v1 #349
base: main
Are you sure you want to change the base?
jdbc-intro-v1 #349
Conversation
MaksimSokur
commented
May 11, 2024
- Establish connection to my DB
- using community version of Idea - download plugin DB Browser (танці з бубном трохи) - it is work;
- create new schema - books;
- Create init_db.sql file and put the script.
- Create Book model
- Create DAO layer for Book model.
- create();
- findAll();
- findById();
- update();
- deleteById();
- Used Dao annotation;
- Return Optional findById;
- Call CRUD methods in main.
- Using custom exception
1. Establish connection to my DB - using community version of Idea - download plugin DB Browser (танці з бубном трохи) - it is work; - create new schema - books; 2. Create init_db.sql file and put the script. 3. Create Book model 4. Create DAO layer for Book model. - create(); - findAll(); - findById(); - update(); - deleteById(); 5. Used Dao annotation; 6. Return Optional findById; 7. Call CRUD methods in main. 8. Using custom exception
import mate.academy.lib.Dao; | ||
import mate.academy.model.Book; | ||
|
||
@Dao |
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.
This annotation should be there
import mate.academy.model.Book; | ||
|
||
@Dao | ||
public interface BookDaoInt { |
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.
public interface BookDaoInt { | |
public interface BookDao { |
|
||
@Dao | ||
public interface BookDaoInt { | ||
|
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.
import java.math.BigDecimal; | ||
|
||
public class 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.
import java.util.Optional; | ||
import mate.academy.model.Book; | ||
|
||
public class BookDaoImpl implements BookDaoInt { |
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.
public class BookDaoImpl implements BookDaoInt { | |
@Dao | |
public class BookDaoImpl implements BookDaoInt { |
return books; | ||
} | ||
|
||
private Book mapBookFromResultSet(ResultSet resultSet) { |
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.
private methods should be placed after all public and protected methods
book.setPrice(resultSet.getBigDecimal("price")); | ||
return book; | ||
} catch (SQLException e) { | ||
throw new RuntimeException("SQL error db book.", e); |
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.
unclear message
return Optional.of(mapBookFromResultSet(resultSet)); | ||
} | ||
} catch (SQLException e) { | ||
throw new RuntimeException("Sql error while finding book",e); |
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.
throw new RuntimeException("Sql error while finding book",e); | |
throw new DataProcessingException("Can't find a book by id: " + id, e); |
Check all places
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.
I Have a problem - cannot find symbol class DataProcessingExcepton
File -> Invalidate Caches/Restart - doing
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.
DataProcessingExcepton is custom exception, that you can create. It' better to use instead of RuntimeException
src/main/java/mate/academy/Main.java
Outdated
import java.util.Optional; | ||
import mate.academy.dao.BookDaoImpl; | ||
import mate.academy.model.Book; | ||
|
||
public class Main { | ||
public static void main(String[] args) { |
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.
public static void main(String[] args) { | |
private static final Injector injector = Injector.getInstance("mate.academy"); | |
public static void main(String[] args) { |
src/main/java/mate/academy/Main.java
Outdated
public class Main { | ||
public static void main(String[] args) { | ||
final BookDaoImpl bookDao = new BookDaoImpl(); |
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.
final BookDaoImpl bookDao = new BookDaoImpl(); | |
BookDao bookDao = (BookDao) injector.getInstance(BookDao.class); |
corrected
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.
Please make sure your build was successful before submitting a review request for your PR.
import mate.academy.lib.Dao; | ||
import mate.academy.model.Book; | ||
|
||
@Dao |
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.
@Dao |
This annotation shouldn't be here
@Override | ||
public Book create(Book book) { | ||
String query = "INSERT INTO books (title, price) VALUES (?, ?)"; | ||
try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) { |
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.
Please, pay attention to the comment
public Book create(Book book) { | ||
String query = "INSERT INTO books (title, price) VALUES (?, ?)"; | ||
try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) { | ||
PreparedStatement preparedStatement = |
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.
Please, pay attention to the comment
preparedStatement.setString(1, book.getTitle()); | ||
preparedStatement.setBigDecimal(2, book.getPrice()); | ||
int updatedRows = preparedStatement.executeUpdate(); | ||
if (updatedRows == 0) { |
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
} | ||
ResultSet generatedKeys = preparedStatement.getGeneratedKeys(); | ||
if (generatedKeys.next()) { | ||
book.setId(generatedKeys.getLong(1)); |
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
List<Book> books = new ArrayList<>(); | ||
String query = "SELECT * FROM books"; | ||
try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) { | ||
Statement statement = connection.createStatement(); |
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
Create costom exeption DataProcessingException and using it Create Conction util and using it fixed the mistakes corrected
return DriverManager.getConnection(DB_URL, DB_PROPERTIES); | ||
} | ||
} | ||
|
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 to not leave useless blank lines