Skip to content

Commit

Permalink
fixed all mistake.
Browse files Browse the repository at this point in the history
  • Loading branch information
SviatoslavTataryn committed Apr 29, 2024
1 parent a154fb0 commit 0b20f37
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 38 deletions.
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>jv-jdbc-intro</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<jdk.version>17</jdk.version>
<maven.compiler.source>17</maven.compiler.source>
Expand All @@ -16,7 +14,6 @@
<maven.checkstyle.plugin.configLocation>
https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml
</maven.checkstyle.plugin.configLocation>

</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
Expand All @@ -25,10 +22,7 @@
<artifactId>mysql-connector-j</artifactId>
<version>8.3.0</version>
</dependency>


</dependencies>

<build>
<plugins>
<plugin>
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ public static void main(String[] args) {
BookDao bookDao = (BookDao) injector.getInstance(BookDao.class);
Book book = new Book();
book.setPrice(BigDecimal.valueOf(548));
book.setTitle("The Witcher. 1. The Last Wish");
book.setTitle("The Witcher.");
bookDao.create(book);
System.out.println(bookDao.findById(6L).orElseThrow(()
-> new RuntimeException("Can`t find book with id " + 1L)));
List<Book> books = bookDao.findAll();
books.forEach(System.out::println);
Optional<Book> newBook = bookDao.findById(6L);
Optional<Book> newBook = bookDao.findById(5L);
Book newBookForChange = newBook.get();
newBookForChange.setPrice(BigDecimal.valueOf(250));
newBookForChange.setTitle("Harry Potter");
System.out.println(bookDao.update(newBook.get()));
System.out.println(bookDao.deleteById(7L));
System.out.println(bookDao.deleteById(5L));
}
}
38 changes: 17 additions & 21 deletions src/main/java/mate/academy/dao/BookDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,38 @@
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 {
private static void checkAffectedRows(int affectedRows) {
if (affectedRows < 1) {
throw new RuntimeException("Expected to update at least one row");
}
}

@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)) {
PreparedStatement statement = connection
.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
statement.setString(1, book.getTitle());
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.");
}
checkAffectedRows(affectedRows);
ResultSet generatedKeys = statement.getGeneratedKeys();
if (generatedKeys.next()) {
Long id = generatedKeys.getObject(1, Long.class);
book.setId(id);
}
return book;
} catch (SQLException e) {
throw new DataProcessingException("Can`t add new car: " + book, e);
throw new DataProcessingException("Can't add new book: " + book, e);
}
return book;
}

@Override
Expand All @@ -53,7 +55,8 @@ public Optional<Book> findById(Long id) {
return book;
}
} catch (SQLException e) {
throw new DataProcessingException("Can not create connection to the DB", e);
throw new DataProcessingException("Failed to establish a database connection. "
+ "Please check the database configuration and network connectivity.", e);
}
return Optional.empty();
}
Expand All @@ -63,7 +66,7 @@ public List<Book> findAll() {
String sql = "SELECT * FROM books";
try (Connection connection = ConnectionUtil.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql)) {
ResultSet resultSet = statement.executeQuery(sql)) {
List<Book> books = new ArrayList<>();
while (resultSet.next()) {
books.add(parseResultSet(resultSet));
Expand All @@ -80,7 +83,7 @@ public Book update(Book book) {
String sql = "UPDATE books SET title = ?, price = ? WHERE id = ?";
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement =
connection.prepareStatement(sql)) {
connection.prepareStatement(sql)) {
statement.setString(1, book.getTitle());
statement.setBigDecimal(2, book.getPrice());
statement.setLong(3, book.getId());
Expand All @@ -97,8 +100,7 @@ public Book update(Book book) {
public boolean deleteById(Long id) {
String sql = "DELETE FROM books WHERE id = ?";
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement
= connection.prepareStatement(sql)) {
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setLong(1, id);
int affectedRows = statement.executeUpdate();
checkAffectedRows(affectedRows);
Expand All @@ -122,10 +124,4 @@ private Book parseResultSet(ResultSet resultSet) {
throw new DataProcessingException("Result set is empty", e);
}
}

private static void checkAffectedRows(int affectedRows) {
if (affectedRows < 1) {
throw new RuntimeException("Expected to update at least one row");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package mate.academy;
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/test";
private static final String DB_URL = "jdbc:mysql://localhost:3306/book_store";
private static final Properties DB_PROPERTIES;
private static final String USER = "root";
private static final String PASSWORD = "1234";
private static final String USER = "****";
private static final String PASSWORD = "*******";

static {
DB_PROPERTIES = new Properties();
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/init_db.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CREATE TABLE books (
id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(255),
price DECIMAL,
id BIGINT NOT NULL AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
PRIMARY KEY (id)
)
);

0 comments on commit 0b20f37

Please sign in to comment.