-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc1d800
commit ef645bd
Showing
7 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + '}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|