Skip to content

Commit

Permalink
Hibernate_until
Browse files Browse the repository at this point in the history
  • Loading branch information
YaroslavKolomoiets committed Apr 30, 2024
1 parent bc1d800 commit ef645bd
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
</maven.checkstyle.plugin.configLocation>
</properties>

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/mate/academy/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package mate.academy;

public class Book {
private Long id;
private String title;
private int price;

public Long getId() {
return id;
}

public String getTitle() {
return this.title;
}

public int getPrice() {
return price;
}

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

public void setTitle(String title) {
this.title = title;
}

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

@Override
public String toString() {
return "Book{" + "id=" + id
+ ", title='" + title + '\''
+ ", price=" + price + '}';
}
}
28 changes: 28 additions & 0 deletions src/main/java/mate/academy/ConnectionUtill.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package mate.academy;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionUtill {
private static final String DB_URL = "jdbc:mysql://localhost:3306/book";
private static final Properties DB_PROPERTIES;

static {
DB_PROPERTIES = new Properties();
DB_PROPERTIES.put("user", "Admin");
DB_PROPERTIES.put("password", "admin");

try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new RuntimeException("Can not load JDBC driver",e);
}
}

public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(DB_URL, DB_PROPERTIES);
}

}
13 changes: 13 additions & 0 deletions src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
package mate.academy;

import mate.academy.dao.BookDao;
import mate.academy.dao.BookDaoImpl;

public class Main {
public static void main(String[] args) {
BookDao bookDao = new BookDaoImpl();
System.out.println(bookDao.get(3L));

Book kobzar = new Book();
kobzar.setTitle("Kobzar");
kobzar.setPrice(30);

Book kobzarUpdated = bookDao.save(kobzar);
System.out.println(kobzarUpdated);

}
}

16 changes: 16 additions & 0 deletions src/main/java/mate/academy/dao/BookDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package mate.academy.dao;

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

public interface BookDao {
Book save(Book book);

Book get(Long id);

Optional<Book> findById(Long id);

Book update(Book book);

boolean delete(Book book);
}
74 changes: 74 additions & 0 deletions src/main/java/mate/academy/dao/BookDaoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package mate.academy.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Optional;
import mate.academy.Book;
import mate.academy.ConnectionUtill;

public class BookDaoImpl implements BookDao {

@Override
public Book save(Book book) {
String sql = "INSERT INTO books (title, price) VALUES (?, ?)";
try (Connection connection = ConnectionUtill.getConnection();
PreparedStatement statement = connection.prepareStatement(sql,
Statement.RETURN_GENERATED_KEYS)) {
statement.setString(1, book.getTitle());
statement.setInt(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()) {
Long id = generatedKeys.getObject(1, Long.class);
book.setId(id);
}
} catch (SQLException e) {
throw new RuntimeException("Can't add new book: " + book, e);
}
return book;
}

@Override
public Book get(Long id) {
String sql = "SELECT * FROM books WHERE id = ?";
try (Connection connection = ConnectionUtill.getConnection();
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setLong(1, id);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
String title = resultSet.getString("title");
int price = resultSet.getObject("price", Integer.class);
Book book = new Book();
book.setId(id);
book.setPrice(price);
book.setTitle(title);
return book;
}
} catch (SQLException e) {
throw new RuntimeException("Cannot create a connection to the DB", e);
}
return null;
}

@Override
public Optional<Book> findById(Long id) {
return Optional.empty();
}

@Override
public Book update(Book book) {
return null;
}

@Override
public boolean delete(Book book) {
return false;
}
}
10 changes: 10 additions & 0 deletions src/main/resources/init_db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
SELECT * FROM books;

INSERT INTO books (id, title, price) VALUE (1,'Harry Potter', 25);


UPDATE books SET title = 'Harry Potter' WHERE id = 1;

DELETE FROM books WHERE id = 5;


0 comments on commit ef645bd

Please sign in to comment.