Skip to content

Commit

Permalink
change some types of filds in Book class, create mapResultSetToBook m…
Browse files Browse the repository at this point in the history
…ethod, use DataProcessingException
  • Loading branch information
romazan07 committed Jun 25, 2024
1 parent 1dc44fb commit d94ee79
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 45 deletions.
10 changes: 5 additions & 5 deletions src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package mate.academy;

import java.math.BigInteger;
import java.math.BigDecimal;
import mate.academy.dao.BookDao;
import mate.academy.lib.Injector;
import mate.academy.model.Book;
Expand All @@ -12,16 +12,16 @@ public static void main(String[] args) {
// create
Book book = new Book();
book.setTitle("Java Programming");
book.setPrice(100.00);
book.setPrice(new BigDecimal(100));
System.out.println(bookDao.create(book));
// update
Book bookUpdate = new Book();
bookUpdate.setTitle("Updated title");
bookUpdate.setPrice(200.00);
bookUpdate.setId(new BigInteger("1"));
bookUpdate.setPrice(new BigDecimal(200));
bookUpdate.setId(1L);
System.out.println(bookDao.update(bookUpdate));
// findById
System.out.println(bookDao.findById(2L));
System.out.println(bookDao.findById(1L));
// findAll
System.out.println(bookDao.findAll());
// deleteById
Expand Down
35 changes: 18 additions & 17 deletions src/main/java/mate/academy/dao/BookDaoImpl.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mate.academy.dao;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand All @@ -10,10 +9,10 @@
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;
import mate.academy.util.ConnectionUtil;

@Dao
public class BookDaoImpl implements BookDao {
Expand All @@ -25,15 +24,15 @@ public Book create(Book book) {
PreparedStatement statement =
connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) {
statement.setString(1, book.getTitle());
statement.setDouble(2, book.getPrice());
statement.setBigDecimal(2, book.getPrice());
int affectedRows = statement.executeUpdate();
if (affectedRows < 1) {
throw new RuntimeException(
"Expected to insert at least one row, but inserted 0 rows.");
}
ResultSet generatedKeys = statement.getGeneratedKeys();
if (generatedKeys.next()) {
BigInteger id = generatedKeys.getObject(1, BigInteger.class);
Long id = generatedKeys.getLong(1);
book.setId(id);
}
} catch (SQLException e) {
Expand All @@ -52,14 +51,11 @@ public Optional<Book> findById(Long id) {
statement.executeQuery();
ResultSet resultSet = statement.getResultSet();
if (resultSet.next()) {
Book book = new Book();
book.setId(resultSet.getObject("id", BigInteger.class));
book.setTitle(resultSet.getObject("title", String.class));
book.setPrice(resultSet.getObject("price", Double.class));
Book book = mapResultSetToBook(resultSet);
return Optional.of(book);
}
} catch (SQLException e) {
throw new RuntimeException(e);
throw new DataProcessingException("Can't find the book by id, where id = " + id, e);
}
return Optional.empty();
}
Expand All @@ -74,15 +70,12 @@ public List<Book> findAll() {
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
do {
Book book = new Book();
book.setId(resultSet.getObject("id", BigInteger.class));
book.setTitle(resultSet.getObject("title", String.class));
book.setPrice(resultSet.getObject("price", Double.class));
Book book = mapResultSetToBook(resultSet);
books.add(book);
} while (resultSet.next());
}
} catch (SQLException e) {
throw new RuntimeException(e);
throw new DataProcessingException("Can't find all from table", e);
}
return books;
}
Expand All @@ -94,16 +87,16 @@ public Book update(Book book) {
Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, book.getTitle());
statement.setDouble(2, book.getPrice());
statement.setBigDecimal(3, new BigDecimal(book.getId()));
statement.setBigDecimal(2, book.getPrice());
statement.setLong(3, book.getId());
int affectedRows = statement.executeUpdate();
if (affectedRows < 1) {
throw new RuntimeException(
"Expected to update at least one row, but updated 0 rows.");
}
return book;
} catch (SQLException e) {
throw new RuntimeException(e);
throw new DataProcessingException("Can't update the book: " + book, e);
}
}

Expand All @@ -124,4 +117,12 @@ public boolean deleteById(Long id) {
}
return true;
}

private static Book mapResultSetToBook(ResultSet resultSet) throws SQLException {
Book book = new Book();
book.setId(resultSet.getLong("id"));
book.setTitle(resultSet.getObject("title", String.class));
book.setPrice(resultSet.getObject("price", BigDecimal.class));
return book;
}
}
14 changes: 7 additions & 7 deletions src/main/java/mate/academy/model/Book.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package mate.academy.model;

import java.math.BigInteger;
import java.math.BigDecimal;

public class Book {
private BigInteger id;
private Long id;
private String title;
private double price;
private BigDecimal price;

public BigInteger getId() {
public Long getId() {
return id;
}

public void setId(BigInteger id) {
public void setId(Long id) {
this.id = id;
}

Expand All @@ -23,11 +23,11 @@ public void setTitle(String title) {
this.title = title;
}

public double getPrice() {
public BigDecimal getPrice() {
return price;
}

public void setPrice(double price) {
public void setPrice(BigDecimal price) {
this.price = price;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package mate.academy;
package mate.academy.util;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
Expand All @@ -15,7 +12,7 @@ public class ConnectionUtil {
static {
DB_PROPERTIES = new Properties();
DB_PROPERTIES.put("user", "root");
DB_PROPERTIES.put("password", getPassword());
DB_PROPERTIES.put("password", "1111");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
Expand All @@ -26,14 +23,4 @@ public class ConnectionUtil {
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(DB_URL, DB_PROPERTIES);
}

private static String getPassword() {
String path = "src/main/resources/pswrd.txt";
try {
return Files.readString(Path.of(path));

} catch (IOException e) {
throw new RuntimeException("Can't read a file with path: " + path, e);
}
}
}
1 change: 0 additions & 1 deletion src/main/resources/pswrd.txt

This file was deleted.

0 comments on commit d94ee79

Please sign in to comment.