-
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
jdbc-intro #399
base: main
Are you sure you want to change the base?
jdbc-intro #399
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package mate.academy; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
import java.util.Properties; | ||
|
||
public class ConnectionUtil { | ||
private static final String PROPERTIES_FILE_PATH = "src/main/resources/db.properties"; | ||
private static final String DATABASE_URL = "db.url"; | ||
private static final String DATABASE_USER_NAME = "db.username"; | ||
private static final String DATABASE_USER_PASSWORD = "db.password"; | ||
private static final Properties properties = new Properties(); | ||
|
||
static { | ||
readDatabaseFileProperties(); | ||
} | ||
|
||
private static final void readDatabaseFileProperties() { | ||
try (InputStream inputStreamProperties = new FileInputStream(PROPERTIES_FILE_PATH)) { | ||
properties.load(inputStreamProperties); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Cannot read properties file", e); | ||
} | ||
} | ||
|
||
public static Connection connectToDatabase() throws SQLException { | ||
String url = properties.getProperty(DATABASE_URL); | ||
String username = properties.getProperty(DATABASE_USER_NAME); | ||
String password = properties.getProperty(DATABASE_USER_PASSWORD); | ||
return DriverManager.getConnection(url, username, password); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,21 @@ | ||
package mate.academy; | ||
|
||
import java.math.BigDecimal; | ||
import mate.academy.dao.BookDao; | ||
import mate.academy.domain.Book; | ||
import mate.academy.lib.Injector; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
private static final Injector injector = Injector.getInstance("mate.academy.dao"); | ||
|
||
public static void main(String[] args) { | ||
BookDao bookDao = (BookDao) injector.getInstance(BookDao.class); | ||
Book book = new Book("The witcher", new BigDecimal("195.00")); | ||
bookDao.create(book); | ||
bookDao.create(book); | ||
bookDao.update(book); | ||
bookDao.deleteById(1); | ||
bookDao.findAll(); | ||
bookDao.findById(2); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,18 @@ | ||||
package mate.academy.dao; | ||||
|
||||
import java.util.List; | ||||
import java.util.Optional; | ||||
import mate.academy.domain.Book; | ||||
|
||||
public interface BookDao { | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
Book create(Book book); | ||||
|
||||
Optional<Book> findById(int id); | ||||
|
||||
List<Book> findAll(); | ||||
|
||||
Book update(Book book); | ||||
|
||||
boolean deleteById(int id); | ||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,123 @@ | ||||||||||||||||||
package mate.academy.dao; | ||||||||||||||||||
|
||||||||||||||||||
import java.math.BigDecimal; | ||||||||||||||||||
import java.sql.Connection; | ||||||||||||||||||
import java.sql.PreparedStatement; | ||||||||||||||||||
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.domain.Book; | ||||||||||||||||||
import mate.academy.lib.Dao; | ||||||||||||||||||
|
||||||||||||||||||
@Dao | ||||||||||||||||||
public class BookDaoImpl implements BookDao { | ||||||||||||||||||
private static final String CREATE_BOOK_QUERY = "INSERT INTO book (title, price) VALUES (?, ?)"; | ||||||||||||||||||
alwayswannajava marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
private static final String FIND_BOOK_BY_ID_QUERY = "SELECT * FROM book WHERE id = ?"; | ||||||||||||||||||
alwayswannajava marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
private static final String FIND_ALL_BOOK_QUERY = "SELECT * FROM book"; | ||||||||||||||||||
alwayswannajava marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
private static final String UPDATE_BOOK_QUERY = "UPDATE book SET title = ?, " | ||||||||||||||||||
+ "price = ? WHERE id = ?"; | ||||||||||||||||||
alwayswannajava marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
private static final String DELETE_BOOK_QUERY = "DELETE FROM book WHERE id = ?"; | ||||||||||||||||||
alwayswannajava marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
|
||||||||||||||||||
@Override | ||||||||||||||||||
public Book create(Book book) { | ||||||||||||||||||
Book createdBook; | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
try (Connection connection = ConnectionUtil.connectToDatabase(); | ||||||||||||||||||
PreparedStatement preparedStatement = connection.prepareStatement(CREATE_BOOK_QUERY, | ||||||||||||||||||
Statement.RETURN_GENERATED_KEYS)) { | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||||||||||||||||||
preparedStatement.setString(2, book.getTitle()); | ||||||||||||||||||
preparedStatement.setBigDecimal(3, book.getPrice()); | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The parameters for |
||||||||||||||||||
preparedStatement.execute(); | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
try (ResultSet resultSet = preparedStatement.getGeneratedKeys()) { | ||||||||||||||||||
resultSet.next(); | ||||||||||||||||||
createdBook = new Book( | ||||||||||||||||||
resultSet.getString("title"), | ||||||||||||||||||
resultSet.getBigDecimal("price")); | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
} | ||||||||||||||||||
} catch (SQLException throwables) { | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
throw new RuntimeException(throwables); | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a more informative message for the RuntimeException, such as including the action that was attempted when the exception occurred. |
||||||||||||||||||
} | ||||||||||||||||||
return createdBook; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
@Override | ||||||||||||||||||
public Optional<Book> findById(int id) { | ||||||||||||||||||
Book findBookById = null; | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
try (Connection connection = ConnectionUtil.connectToDatabase(); | ||||||||||||||||||
PreparedStatement preparedStatement = | ||||||||||||||||||
connection.prepareStatement(FIND_BOOK_BY_ID_QUERY, | ||||||||||||||||||
Statement.RETURN_GENERATED_KEYS)) { | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix comment |
||||||||||||||||||
preparedStatement.setLong(1, id); | ||||||||||||||||||
ResultSet resultSet = preparedStatement.executeQuery(); | ||||||||||||||||||
while (resultSet.next()) { | ||||||||||||||||||
Long bookId = resultSet.getLong("id"); | ||||||||||||||||||
String title = resultSet.getString("title"); | ||||||||||||||||||
BigDecimal price = resultSet.getBigDecimal("price"); | ||||||||||||||||||
findBookById = new Book(bookId, title, price); | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
} | ||||||||||||||||||
} catch (SQLException throwables) { | ||||||||||||||||||
throw new RuntimeException(throwables); | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
check all places |
||||||||||||||||||
} | ||||||||||||||||||
return Optional.ofNullable(findBookById); | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
@Override | ||||||||||||||||||
public List<Book> findAll() { | ||||||||||||||||||
List<Book> foundBooks = new ArrayList<>(); | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
try (Connection connection = ConnectionUtil.connectToDatabase(); | ||||||||||||||||||
PreparedStatement preparedStatement = | ||||||||||||||||||
connection.prepareStatement(FIND_ALL_BOOK_QUERY, | ||||||||||||||||||
Statement.RETURN_GENERATED_KEYS)) { | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix comment |
||||||||||||||||||
|
||||||||||||||||||
ResultSet resultSet = preparedStatement.executeQuery(); | ||||||||||||||||||
while (resultSet.next()) { | ||||||||||||||||||
Long bookId = resultSet.getLong("id"); | ||||||||||||||||||
String title = resultSet.getString("title"); | ||||||||||||||||||
BigDecimal price = resultSet.getBigDecimal("price"); | ||||||||||||||||||
foundBooks.add(new Book(bookId, title, price)); | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
} | ||||||||||||||||||
} catch (SQLException throwables) { | ||||||||||||||||||
throw new RuntimeException(throwables); | ||||||||||||||||||
} | ||||||||||||||||||
return foundBooks; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
@Override | ||||||||||||||||||
public Book update(Book book) { | ||||||||||||||||||
Book updatedBook = null; | ||||||||||||||||||
try (Connection connection = ConnectionUtil.connectToDatabase(); | ||||||||||||||||||
PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_BOOK_QUERY, | ||||||||||||||||||
Statement.RETURN_GENERATED_KEYS)) { | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix comment |
||||||||||||||||||
preparedStatement.setString(2, book.getTitle()); | ||||||||||||||||||
preparedStatement.setBigDecimal(3, book.getPrice()); | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The parameters for |
||||||||||||||||||
ResultSet resultSet = preparedStatement.executeQuery(); | ||||||||||||||||||
while (resultSet.next()) { | ||||||||||||||||||
Long bookId = resultSet.getLong("id"); | ||||||||||||||||||
String title = resultSet.getString("title"); | ||||||||||||||||||
BigDecimal price = resultSet.getBigDecimal("price"); | ||||||||||||||||||
updatedBook = new Book(bookId, title, price); | ||||||||||||||||||
alwayswannajava marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
} | ||||||||||||||||||
} catch (SQLException throwables) { | ||||||||||||||||||
throw new RuntimeException(throwables); | ||||||||||||||||||
alwayswannajava marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
} | ||||||||||||||||||
return updatedBook; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
@Override | ||||||||||||||||||
public boolean deleteById(int id) { | ||||||||||||||||||
boolean isDeleted; | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
try (Connection connection = ConnectionUtil.connectToDatabase(); | ||||||||||||||||||
PreparedStatement preparedStatement = connection.prepareStatement(DELETE_BOOK_QUERY, | ||||||||||||||||||
Statement.RETURN_GENERATED_KEYS)) { | ||||||||||||||||||
alwayswannajava marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
preparedStatement.setLong(1, id); | ||||||||||||||||||
isDeleted = preparedStatement.execute(); | ||||||||||||||||||
alwayswannajava marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
} catch (SQLException throwables) { | ||||||||||||||||||
throw new RuntimeException(throwables); | ||||||||||||||||||
alwayswannajava marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
} | ||||||||||||||||||
return isDeleted; | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package mate.academy.domain; | ||
|
||
import java.math.BigDecimal; | ||
|
||
public class Book { | ||
private Long id; | ||
private String title; | ||
private BigDecimal price; | ||
|
||
public Book() { | ||
} | ||
|
||
public Book(String title, BigDecimal price) { | ||
this.title = title; | ||
this.price = price; | ||
} | ||
|
||
public Book(Long id, String title, BigDecimal price) { | ||
this.id = id; | ||
this.title = title; | ||
this.price = price; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public BigDecimal getPrice() { | ||
return price; | ||
} | ||
|
||
public void setPrice(BigDecimal price) { | ||
this.price = price; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
db.url=jdbc:mysql://localhost:3307 | ||
db.username=postgres | ||
db.password=root |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,7 @@ | ||||||
create schema if not exists test; | ||||||
|
||||||
create table if not exists book ( | ||||||
book_id serial primary key, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
title varchar(20), | ||||||
price decimal | ||||||
); |
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.
replace to util package