Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

artemklishch
Copy link

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

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 {

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

Copy link
Author

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

public static void main(String[] args) {
BookDao bookDao = (BookDao) INJECTOR.getInstance(BookDao.class);
Book book1 = new Book("Book of Jungles", new BigDecimal(100));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Book book1 = new Book("Book of Jungles", new BigDecimal(100));
Book book = new Book("Book of Jungles", new BigDecimal(100));

Copy link
Author

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 (?, ?)";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
String sql = "INSERT INTO books (title, price) values (?, ?)";
String query = "INSERT INTO books (title, price) values (?, ?)";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename in all places

Copy link
Author

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(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new RuntimeException(
throw new DataProcessingException(

Copy link
Author

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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int affectedRows = statement.executeUpdate();
return statement.executeUpdate() > 0;

Copy link
Author

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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private final String title;
private String title;

Copy link
Author

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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private final BigDecimal price;
private BigDecimal price;

Copy link
Author

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;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}
@Override
public String toString() {
return "Book{"
+ "id=" + id
+ ", title='" + title + '\''
+ ", price=" + price
+ '}';
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++, added

@@ -0,0 +1,5 @@
CREATE TABLE IF NOT EXISTS books (
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
id BIGINT AUTO_INCREMENT PRIMARY KEY,

Primary key already provided not null constraint

Copy link
Author

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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
initializeTable();

remove this logic

Copy link
Author

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)

@artemklishch
Copy link
Author

artemklishch commented May 29, 2024 via email

public static void main(String[] args) {
ConnectionUtil.initializeTable();

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

Copy link
Author

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();

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

Copy link
Author

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();

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

Copy link
Author

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()) {

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

@artemklishch
Copy link
Author

artemklishch commented May 30, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants