Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. package pl.poznan.put.cie.coffee;
  2.  
  3. import org.springframework.dao.DataAccessException;
  4. import org.springframework.dao.EmptyResultDataAccessException;
  5. import org.springframework.jdbc.core.ResultSetExtractor;
  6. import org.springframework.jdbc.core.RowMapper;
  7. import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
  8. import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
  9.  
  10. import java.math.BigDecimal;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.util.ArrayList;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import java.util.Map;
  17.  
  18. public class CoffeeDao {
  19.  
  20. private final NamedParameterJdbcTemplate jdbc ;
  21.  
  22. public CoffeeDao() {
  23. this.jdbc = new NamedParameterJdbcTemplate(DbUtilities.getSQLiteDataSource("jdbc:sqlite:database.sqlite"));
  24. }
  25.  
  26. /**
  27. * Returns a coffee with given name.
  28. *
  29. * @param name coffee name
  30. * @return coffee object or null
  31. */
  32. public Coffee get(final String name) {
  33. String sql = "SELECT sup_id, price, sales, total FROM coffees "
  34. + "WHERE cof_name = :cof_name";
  35. MapSqlParameterSource params = new MapSqlParameterSource("cof_name", name);
  36. return jdbc.query(sql, params, new ResultSetExtractor<Coffee>() {
  37.  
  38. @Override
  39. public Coffee extractData(ResultSet rs) throws SQLException, DataAccessException {
  40. int supplierId= rs.getInt("sup_id");
  41. BigDecimal price = new BigDecimal(rs.getInt("price"));
  42. int sales = rs.getInt("sales");
  43. int total = rs.getInt("total");
  44. return new Coffee(name, supplierId, price, sales, total);
  45. }
  46. });
  47. }
  48.  
  49. /**
  50. * Returns a list of all coffees.
  51. *
  52. * @return list of all coffees
  53. */
  54. public List<Coffee> getAll() {
  55. String sql = "SELECT cof_name, sup_id, price, sales, total FROM coffees";
  56. try {
  57. return jdbc.query(sql, new RowMapper<Coffee>() {
  58.  
  59. @Override
  60. public Coffee mapRow(ResultSet rs, int i) throws SQLException{
  61. return new Coffee(rs.getString("cof_name"),
  62. rs.getInt("sup_id"),
  63. new BigDecimal(rs.getInt("price")),
  64. rs.getInt("sales"),
  65. rs.getInt("total")
  66. );
  67. }
  68. });
  69. } catch (EmptyResultDataAccessException ex) {
  70. return new ArrayList<>();
  71. }
  72. }
  73.  
  74. public void update(Coffee c) {
  75. String sql = "UPDATE coffees "
  76. + "SET price = :price, sales = :sales, total = :total "
  77. + "WHERE cof_name = :cof_name AND sup_id = :sup_id";
  78. Map<String, Object> parameters = new HashMap<>();
  79. parameters.put("price", c.getPrice());
  80. parameters.put("sales", c.getSales());
  81. parameters.put("total", c.getTotal());
  82. parameters.put("cof_name", c.getName());
  83. parameters.put("sup_id", c.getSupplierId());
  84. jdbc.update(sql, parameters);
  85. }
  86.  
  87. public void delete(String name, int supplierId) {
  88. String sql = " DELETE FROM coffees WHERE cof_name = :cof_name AND sup_id =:sup_id";
  89. MapSqlParameterSource params = new MapSqlParameterSource("cof_name", name)
  90. .addValue("sup_id", supplierId);
  91. jdbc.update(sql, params);
  92. }
  93.  
  94. public void create(Coffee c) {
  95. String sql = "INSERT INTO coffees(cof_name, sup_id, price, sales, total) " +
  96. "VALUES(:cof_name, :sup_id, :price, :sales, :total)";
  97. MapSqlParameterSource params = new MapSqlParameterSource("cof_name", c.getName())
  98. .addValue("sup_id", c.getSupplierId())
  99. .addValue("price", c.getPrice())
  100. .addValue("sales", c.getSales())
  101. .addValue("total", c.getTotal());
  102. jdbc.update(sql, params);
  103. }
  104.  
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement