-
Notifications
You must be signed in to change notification settings - Fork 438
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
Requests to the DB #360
base: main
Are you sure you want to change the base?
Requests to the DB #360
Conversation
BREAKING CHANGES; *Made connection to the DB *Made initialising file to create the DB *Made methods to send requests *Made exceptions Task: https://github.com/mate-academy/jv-jdbc-intro
import java.util.Properties; | ||
import mate.academy.exceptions.DataProcessingException; | ||
|
||
public class ConnectionUtil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Util classes better to replace to util package
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++, thanks, moved it to the package
src/main/java/mate/academy/Main.java
Outdated
public static void main(String[] args) { | ||
BookDao bookDao = (BookDao) INJECTOR.getInstance(BookDao.class); | ||
Book book1 = new Book("Book of Jungles", new BigDecimal(100)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Book book1 = new Book("Book of Jungles", new BigDecimal(100)); | |
Book book = new Book("Book of Jungles", new BigDecimal(100)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++, renamed
public class BookDaoImpl implements BookDao { | ||
@Override | ||
public Book create(Book book) { | ||
String sql = "INSERT INTO books (title, price) values (?, ?)"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String sql = "INSERT INTO books (title, price) values (?, ?)"; | |
String query = "INSERT INTO books (title, price) values (?, ?)"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename in all places
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++, thanks, renamed
statement.setBigDecimal(2, book.getPrice()); | ||
int affectedRows = statement.executeUpdate(); | ||
if (affectedRows < 1) { | ||
throw new RuntimeException( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
throw new RuntimeException( | |
throw new DataProcessingException( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++, thanks, changed
PreparedStatement statement = connection.prepareStatement(sql); | ||
) { | ||
statement.setLong(1, id); | ||
int affectedRows = statement.executeUpdate(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int affectedRows = statement.executeUpdate(); | |
return statement.executeUpdate() > 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++, changed
|
||
public class Book { | ||
private Long id; | ||
private final String title; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private final String title; | |
private String title; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++, changed
public class Book { | ||
private Long id; | ||
private final String title; | ||
private final BigDecimal price; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private final BigDecimal price; | |
private BigDecimal price; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++, changed
|
||
public BigDecimal getPrice() { | ||
return price; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} | |
} | |
@Override | |
public String toString() { | |
return "Book{" | |
+ "id=" + id | |
+ ", title='" + title + '\'' | |
+ ", price=" + price | |
+ '}'; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++, added
src/main/resources/init_db.sql
Outdated
@@ -0,0 +1,5 @@ | |||
CREATE TABLE IF NOT EXISTS books ( | |||
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, | |
id BIGINT AUTO_INCREMENT PRIMARY KEY, |
Primary key already provided not null constraint
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++. thank you, changed
|
||
try { | ||
Class.forName("com.mysql.cj.jdbc.Driver"); | ||
initializeTable(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initializeTable(); |
remove this logic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++, removed this expression (i didn't get if i had to remove the logic related to create the table and invoke this function in Main class)
Thank you! Updated.
ср, 29 мая 2024 г. в 08:56, Olena Bruiako ***@***.***>:
… ***@***.**** requested changes on this pull request.
------------------------------
In src/main/java/mate/academy/ConnectionUtil.java
<#360 (comment)>
:
> @@ -0,0 +1,70 @@
+package mate.academy;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Properties;
+import mate.academy.exceptions.DataProcessingException;
+
+public class ConnectionUtil {
Util classes better to replace to util package
------------------------------
In src/main/java/mate/academy/Main.java
<#360 (comment)>
:
> public static void main(String[] args) {
+ BookDao bookDao = (BookDao) INJECTOR.getInstance(BookDao.class);
+ Book book1 = new Book("Book of Jungles", new BigDecimal(100));
⬇️ Suggested change
- Book book1 = new Book("Book of Jungles", new BigDecimal(100));
+ Book book = new Book("Book of Jungles", new BigDecimal(100));
------------------------------
In src/main/java/mate/academy/dao/BookDaoImpl.java
<#360 (comment)>
:
> +import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import mate.academy.ConnectionUtil;
+import mate.academy.exceptions.DataProcessingException;
+import mate.academy.lib.Dao;
+import mate.academy.model.Book;
+
***@***.***
+public class BookDaoImpl implements BookDao {
+ @OverRide
+ public Book create(Book book) {
+ String sql = "INSERT INTO books (title, price) values (?, ?)";
⬇️ Suggested change
- String sql = "INSERT INTO books (title, price) values (?, ?)";
+ String query = "INSERT INTO books (title, price) values (?, ?)";
------------------------------
In src/main/java/mate/academy/dao/BookDaoImpl.java
<#360 (comment)>
:
> +
***@***.***
+public class BookDaoImpl implements BookDao {
+ @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
+ );
+ ) {
+ statement.setString(1, book.getTitle());
+ statement.setBigDecimal(2, book.getPrice());
+ int affectedRows = statement.executeUpdate();
+ if (affectedRows < 1) {
+ throw new RuntimeException(
⬇️ Suggested change
- throw new RuntimeException(
+ throw new DataProcessingException(
------------------------------
In src/main/java/mate/academy/dao/BookDaoImpl.java
<#360 (comment)>
:
> + public boolean deleteById(Long id) {
+ Optional<Book> deletingBook = findById(id);
+ if (deletingBook.equals(Optional.empty())) {
+ throw new DataProcessingException(
+ "Impossible to delete the book with provided ID: "
+ + id
+ + ", because there isn't this book in the DB"
+ );
+ }
+
+ String sql = "DELETE FROM books WHERE id = ?";
+ try (Connection connection = ConnectionUtil.getConnection();
+ PreparedStatement statement = connection.prepareStatement(sql);
+ ) {
+ statement.setLong(1, id);
+ int affectedRows = statement.executeUpdate();
⬇️ Suggested change
- int affectedRows = statement.executeUpdate();
+ return statement.executeUpdate() > 0;
------------------------------
In src/main/java/mate/academy/dao/BookDaoImpl.java
<#360 (comment)>
:
> + try (Connection connection = ConnectionUtil.getConnection();
+ PreparedStatement statement = connection.prepareStatement(sql);
+ ) {
+ statement.setLong(1, id);
+ int affectedRows = statement.executeUpdate();
+ return affectedRows > 1;
+ } catch (SQLException e) {
+ throw new DataProcessingException(
+ "Can not delete the book with the provided ID: " + id
+ );
+ }
+ }
+
+ private Book extractBook(ResultSet resultSet) {
+ try {
+ if (resultSet.next()) {
⬇️ Suggested change
- if (resultSet.next()) {
+ String title = resultSet.getString("title");
+ BigDecimal price = resultSet.getBigDecimal("price");
+ Long id = (long) resultSet.getObject("id");
+ return new Book(id, title, price);
------------------------------
In src/main/java/mate/academy/dao/BookDaoImpl.java
<#360 (comment)>
:
> + }
+
+ private Book extractBook(ResultSet resultSet) {
+ try {
+ if (resultSet.next()) {
+ String title = resultSet.getString("title");
+ BigDecimal price = resultSet.getBigDecimal("price");
+ Long id = (long) resultSet.getObject("id");
+ return new Book(id, title, price);
+ }
+ } catch (SQLException e) {
+ throw new DataProcessingException(
+ "Impossible to extract the book from the ResultSet."
+ );
+ }
+ return null;
⬇️ Suggested change
- return null;
------------------------------
In src/main/java/mate/academy/model/Book.java
<#360 (comment)>
:
> @@ -0,0 +1,36 @@
+package mate.academy.model;
+
+import java.math.BigDecimal;
+
+public class Book {
+ private Long id;
+ private final String title;
⬇️ Suggested change
- private final String title;
+ private String title;
------------------------------
In src/main/java/mate/academy/model/Book.java
<#360 (comment)>
:
> @@ -0,0 +1,36 @@
+package mate.academy.model;
+
+import java.math.BigDecimal;
+
+public class Book {
+ private Long id;
+ private final String title;
+ private final BigDecimal price;
⬇️ Suggested change
- private final BigDecimal price;
+ private BigDecimal price;
------------------------------
In src/main/java/mate/academy/model/Book.java
<#360 (comment)>
:
> +
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public BigDecimal getPrice() {
+ return price;
+ }
⬇️ Suggested change
- }
+ }
+
+ @OverRide
+ public String toString() {
+ return "Book{"
+ + "id=" + id
+ + ", title='" + title + '\''
+ + ", price=" + price
+ + '}';
+ }
------------------------------
In src/main/resources/init_db.sql
<#360 (comment)>
:
> @@ -0,0 +1,5 @@
+CREATE TABLE IF NOT EXISTS books (
+ id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
⬇️ Suggested change
- id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
+ id BIGINT AUTO_INCREMENT PRIMARY KEY,
Primary key already provided not null constraint
------------------------------
In src/main/java/mate/academy/ConnectionUtil.java
<#360 (comment)>
:
> +import mate.academy.exceptions.DataProcessingException;
+
+public class ConnectionUtil {
+ private static final String DB_INIT_PATH = "src/main/resources/init_db.sql";
+ private static final String TABLE_BOOKS_NAME = "books";
+ private static final String DB_URL = "jdbc:mysql://localhost:3306/test";
+ private static final Properties DB_PROPERTIES;
+
+ static {
+ DB_PROPERTIES = new Properties();
+ DB_PROPERTIES.put("user", "artem");
+ DB_PROPERTIES.put("password", "Pssmgk_82");
+
+ try {
+ Class.forName("com.mysql.cj.jdbc.Driver");
+ initializeTable();
⬇️ Suggested change
- initializeTable();
remove this logic
------------------------------
In src/main/java/mate/academy/dao/BookDaoImpl.java
<#360 (comment)>
:
> +import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import mate.academy.ConnectionUtil;
+import mate.academy.exceptions.DataProcessingException;
+import mate.academy.lib.Dao;
+import mate.academy.model.Book;
+
***@***.***
+public class BookDaoImpl implements BookDao {
+ @OverRide
+ public Book create(Book book) {
+ String sql = "INSERT INTO books (title, price) values (?, ?)";
Rename in all places
—
Reply to this email directly, view it on GitHub
<#360 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ANMI2YEHLKML2IZBFTKTGSTZEVUYTAVCNFSM6AAAAABINXWL5GVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDAOBUGMYDOMZWGQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
src/main/java/mate/academy/Main.java
Outdated
public static void main(String[] args) { | ||
ConnectionUtil.initializeTable(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need this logic at all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++, removed
@Override | ||
public Book create(Book book) { | ||
String query = "INSERT INTO books (title, price) values (?, ?)"; | ||
try (Connection connection = ConnectionUtil.getConnection(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need changes here, try with resources is better to used only for read operation with DB
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++, thank you, changed
@Override | ||
public Book update(Book book) { | ||
String query = "UPDATE books SET title = ?, price = ? WHERE id = ?"; | ||
try (Connection connection = ConnectionUtil.getConnection(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same as comment above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++, thank you, updated
|
||
private Book extractBook(ResultSet resultSet) { | ||
try { | ||
if (resultSet.next()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You call this method inside while (resultSet.next())
so don't need if (resultSet.next()) {
here
Thank you! Updated.
чт, 30 мая 2024 г. в 08:43, Olena Bruiako ***@***.***>:
… ***@***.**** requested changes on this pull request.
------------------------------
In src/main/java/mate/academy/Main.java
<#360 (comment)>
:
> public static void main(String[] args) {
+ ConnectionUtil.initializeTable();
You don't need this logic at all
------------------------------
In src/main/java/mate/academy/dao/BookDaoImpl.java
<#360 (comment)>
:
> +import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import mate.academy.exceptions.DataProcessingException;
+import mate.academy.lib.Dao;
+import mate.academy.model.Book;
+import mate.academy.util.ConnectionUtil;
+
***@***.***
+public class BookDaoImpl implements BookDao {
+ @OverRide
+ public Book create(Book book) {
+ String query = "INSERT INTO books (title, price) values (?, ?)";
+ try (Connection connection = ConnectionUtil.getConnection();
Need changes here, try with resources is better to used only for read
operation with DB
------------------------------
In src/main/java/mate/academy/dao/BookDaoImpl.java
<#360 (comment)>
:
> + ) {
+ ResultSet resultSet = statement.executeQuery();
+ List<Book> books = new ArrayList<>();
+ while (resultSet.next()) {
+ books.add(extractBook(resultSet));
+ }
+ return books;
+ } catch (SQLException e) {
+ throw new DataProcessingException("Can not fetch books");
+ }
+ }
+
+ @OverRide
+ public Book update(Book book) {
+ String query = "UPDATE books SET title = ?, price = ? WHERE id = ?";
+ try (Connection connection = ConnectionUtil.getConnection();
The same as comment above
------------------------------
In src/main/java/mate/academy/dao/BookDaoImpl.java
<#360 (comment)>
:
> + try (Connection connection = ConnectionUtil.getConnection();
+ PreparedStatement statement = connection.prepareStatement(sql);
+ ) {
+ statement.setLong(1, id);
+ int affectedRows = statement.executeUpdate();
+ return affectedRows > 1;
+ } catch (SQLException e) {
+ throw new DataProcessingException(
+ "Can not delete the book with the provided ID: " + id
+ );
+ }
+ }
+
+ private Book extractBook(ResultSet resultSet) {
+ try {
+ if (resultSet.next()) {
You call this method inside while (resultSet.next()) so don't need if
(resultSet.next()) { here
—
Reply to this email directly, view it on GitHub
<#360 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ANMI2YHZP6CGSV5NNHGOFKDZE24AZAVCNFSM6AAAAABINXWL5GVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDAOBXGI3TGNRXGU>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
BREAKING CHANGES;
*Made connection to the DB
*Made initialising file to create the DB
*Made methods to send requests
*Made exceptions
Task: https://github.com/mate-academy/jv-jdbc-intro