Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

MaksimSokur
Copy link

  1. Establish connection to my DB
  • using community version of Idea - download plugin DB Browser (танці з бубном трохи) - it is work;
  • create new schema - books;
  1. Create init_db.sql file and put the script.
  2. Create Book model
  3. Create DAO layer for Book model.
    • create();
    • findAll();
    • findById();
    • update();
    • deleteById();
  4. Used Dao annotation;
  5. Return Optional findById;
  6. Call CRUD methods in main.
  7. 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

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 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public interface BookDaoInt {
public interface BookDao {


@Dao
public interface BookDaoInt {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

import java.math.BigDecimal;

public class Book {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

import java.util.Optional;
import mate.academy.model.Book;

public class BookDaoImpl implements BookDaoInt {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class BookDaoImpl implements BookDaoInt {
@Dao
public class BookDaoImpl implements BookDaoInt {

return books;
}

private Book mapBookFromResultSet(ResultSet resultSet) {

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);

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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new RuntimeException("Sql error while finding book",e);
throw new DataProcessingException("Can't find a book by id: " + id, e);

Check all places

Copy link
Author

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

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

import java.util.Optional;
import mate.academy.dao.BookDaoImpl;
import mate.academy.model.Book;

public class Main {
public static void main(String[] args) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static void main(String[] args) {
private static final Injector injector = Injector.getInstance("mate.academy");
public static void main(String[] args) {

public class Main {
public static void main(String[] args) {
final BookDaoImpl bookDao = new BookDaoImpl();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
final BookDaoImpl bookDao = new BookDaoImpl();
BookDao bookDao = (BookDao) injector.getInstance(BookDao.class);

corrected
Copy link

@Elena-Bruyako Elena-Bruyako left a 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@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)) {

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 =

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) {

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));

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();

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);
}
}

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants