Advertisement
Guest User

Untitled

a guest
May 19th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. package dao;
  2.  
  3. import entity.Order;
  4. import entity.active.ActiveOrder;
  5. import exception.UpdateFailedException;
  6.  
  7. import java.sql.*;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Optional;
  11.  
  12. public class MySqlOrderDao implements OrderDao {
  13.  
  14. private final String jdbcUrl;
  15. private final String username;
  16. private final String password;
  17.  
  18. private static final String SAVE_ORDER_QUERY = "INSERT INTO orders(description) VALUES(?)";
  19. private static final String UPDATE_ORDER_QUERY = "UPDATE orders SET description = ? WHERE id = ?";
  20. private static final String DELETE_ORDER_QUERY = "DELETE FROM orders WHERE id = ?";
  21. private static final String FINAL_ALL_ORDERS_QUERY = "SELECT * FROM orders";
  22. private static final String FINAL_BY_ID_ORDER_QUERY = "SELECT * FROM orders WHERE id = ?";
  23. private static final String DELETE_ALL_ORDERS_QUERY = "DELETE FROM orders";
  24.  
  25.  
  26. private static final String ORDER_ID_COLUMN = "id";
  27. private static final String ORDER_DESCRIPTION_COLUMN = "description";
  28.  
  29. public MySqlOrderDao(String jdbcUrl, String username, String password) {
  30. this.jdbcUrl = jdbcUrl;
  31. this.username = username;
  32. this.password = password;
  33. }
  34.  
  35. Connection getConnection() throws SQLException {
  36. return DriverManager.getConnection("jdbc:mysql://localhost:3306/products_ex", "root",
  37. "admin");
  38. }
  39.  
  40. @Override
  41. public void update(Order order) {
  42. try (Connection connection = getConnection();
  43. PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_ORDER_QUERY)) {
  44.  
  45. preparedStatement.setInt(2, order.getId());
  46. preparedStatement.setString(1, order.getDescription());
  47. int updateRecords = preparedStatement.executeUpdate();
  48. if (updateRecords != 1) {
  49. throw new UpdateFailedException(1, updateRecords);
  50. }
  51. } catch (SQLException e) {
  52. throw new RuntimeException(e);
  53. }
  54. }
  55.  
  56. @Override
  57. public Order insert(String description) {
  58.  
  59. try (Connection connection = getConnection();
  60. PreparedStatement preparedStatement = connection.prepareStatement(SAVE_ORDER_QUERY, PreparedStatement.RETURN_GENERATED_KEYS)) {
  61. preparedStatement.setString(1, description);
  62. int updateRecords = preparedStatement.executeUpdate();
  63. if (updateRecords != 1) {
  64. throw new UpdateFailedException(1, updateRecords);
  65. }
  66.  
  67. try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) {
  68. if (generatedKeys.first()) {
  69. return new Order(generatedKeys.getInt(1), description);
  70. } else {
  71. throw new SQLException("No key was generated for new ActiveOrder record");
  72. }
  73. }
  74. } catch (SQLException e) {
  75. throw new RuntimeException(e);
  76. }
  77. }
  78.  
  79. @Override
  80. public void delete(Order order) {
  81. try (Connection connection = getConnection();
  82. PreparedStatement preparedStatement = connection.prepareStatement(DELETE_ORDER_QUERY)) {
  83. preparedStatement.setInt(1, order.getId());
  84. int updateRecords = preparedStatement.executeUpdate();
  85. if (updateRecords != 1) {
  86. throw new UpdateFailedException(1, updateRecords);
  87. }
  88. } catch (SQLException e) {
  89. throw new RuntimeException(e);
  90. }
  91. }
  92.  
  93. @Override
  94. public Order findById(int id) {
  95. try (Connection connection = getConnection();
  96. PreparedStatement preparedStatement = connection.prepareStatement(FINAL_BY_ID_ORDER_QUERY)) {
  97. preparedStatement.setInt(1, id);
  98. try (ResultSet resultSet = preparedStatement.executeQuery()) {
  99. if (resultSet.first()) {
  100. return new Order(resultSet.getInt(ORDER_ID_COLUMN),
  101. resultSet.getString(ORDER_DESCRIPTION_COLUMN));
  102. } else {
  103. throw new RuntimeException("No Order found");
  104. }
  105. }
  106. } catch (SQLException e) {
  107. throw new RuntimeException(e);
  108. }
  109. }
  110.  
  111. @Override
  112. public List<Order> findAll() {
  113. try (Connection connection = getConnection();
  114. PreparedStatement preparedStatement = connection.prepareStatement(FINAL_ALL_ORDERS_QUERY);
  115. ResultSet resultSet = preparedStatement.executeQuery()) {
  116. List<Order> activeOrders = new ArrayList<>();
  117. while (resultSet.next()) {
  118. activeOrders.add(new Order(resultSet.getInt(ORDER_ID_COLUMN),
  119. resultSet.getString(ORDER_DESCRIPTION_COLUMN)));
  120. }
  121. return activeOrders;
  122. } catch (SQLException e) {
  123. throw new RuntimeException(e);
  124. }
  125. }
  126.  
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement