Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dao;
- import entity.Order;
- import entity.active.ActiveOrder;
- import exception.UpdateFailedException;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Optional;
- public class MySqlOrderDao implements OrderDao {
- private final String jdbcUrl;
- private final String username;
- private final String password;
- private static final String SAVE_ORDER_QUERY = "INSERT INTO orders(description) VALUES(?)";
- private static final String UPDATE_ORDER_QUERY = "UPDATE orders SET description = ? WHERE id = ?";
- private static final String DELETE_ORDER_QUERY = "DELETE FROM orders WHERE id = ?";
- private static final String FINAL_ALL_ORDERS_QUERY = "SELECT * FROM orders";
- private static final String FINAL_BY_ID_ORDER_QUERY = "SELECT * FROM orders WHERE id = ?";
- private static final String DELETE_ALL_ORDERS_QUERY = "DELETE FROM orders";
- private static final String ORDER_ID_COLUMN = "id";
- private static final String ORDER_DESCRIPTION_COLUMN = "description";
- public MySqlOrderDao(String jdbcUrl, String username, String password) {
- this.jdbcUrl = jdbcUrl;
- this.username = username;
- this.password = password;
- }
- Connection getConnection() throws SQLException {
- return DriverManager.getConnection("jdbc:mysql://localhost:3306/products_ex", "root",
- "admin");
- }
- @Override
- public void update(Order order) {
- try (Connection connection = getConnection();
- PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_ORDER_QUERY)) {
- preparedStatement.setInt(2, order.getId());
- preparedStatement.setString(1, order.getDescription());
- int updateRecords = preparedStatement.executeUpdate();
- if (updateRecords != 1) {
- throw new UpdateFailedException(1, updateRecords);
- }
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }
- @Override
- public Order insert(String description) {
- try (Connection connection = getConnection();
- PreparedStatement preparedStatement = connection.prepareStatement(SAVE_ORDER_QUERY, PreparedStatement.RETURN_GENERATED_KEYS)) {
- preparedStatement.setString(1, description);
- int updateRecords = preparedStatement.executeUpdate();
- if (updateRecords != 1) {
- throw new UpdateFailedException(1, updateRecords);
- }
- try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) {
- if (generatedKeys.first()) {
- return new Order(generatedKeys.getInt(1), description);
- } else {
- throw new SQLException("No key was generated for new ActiveOrder record");
- }
- }
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }
- @Override
- public void delete(Order order) {
- try (Connection connection = getConnection();
- PreparedStatement preparedStatement = connection.prepareStatement(DELETE_ORDER_QUERY)) {
- preparedStatement.setInt(1, order.getId());
- int updateRecords = preparedStatement.executeUpdate();
- if (updateRecords != 1) {
- throw new UpdateFailedException(1, updateRecords);
- }
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }
- @Override
- public Order findById(int id) {
- try (Connection connection = getConnection();
- PreparedStatement preparedStatement = connection.prepareStatement(FINAL_BY_ID_ORDER_QUERY)) {
- preparedStatement.setInt(1, id);
- try (ResultSet resultSet = preparedStatement.executeQuery()) {
- if (resultSet.first()) {
- return new Order(resultSet.getInt(ORDER_ID_COLUMN),
- resultSet.getString(ORDER_DESCRIPTION_COLUMN));
- } else {
- throw new RuntimeException("No Order found");
- }
- }
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }
- @Override
- public List<Order> findAll() {
- try (Connection connection = getConnection();
- PreparedStatement preparedStatement = connection.prepareStatement(FINAL_ALL_ORDERS_QUERY);
- ResultSet resultSet = preparedStatement.executeQuery()) {
- List<Order> activeOrders = new ArrayList<>();
- while (resultSet.next()) {
- activeOrders.add(new Order(resultSet.getInt(ORDER_ID_COLUMN),
- resultSet.getString(ORDER_DESCRIPTION_COLUMN)));
- }
- return activeOrders;
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement