Advertisement
andersonalmada

Untitled

Jul 29th, 2022
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. package br.ufc.mandacaru5.dao;
  2.  
  3. import java.util.List;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.jdbc.core.JdbcTemplate;
  7. import org.springframework.stereotype.Component;
  8.  
  9. import br.ufc.mandacaru5.model.Product;
  10.  
  11. @Component
  12. public class ProductJDBCDAO implements ProductDAO {
  13.  
  14.     @Autowired
  15.     JdbcTemplate jdbcTemplate;
  16.  
  17.     @Override
  18.     public void save(Product entity) {
  19.         String insert_sql = "insert into products (name, price) values (?, ?)";
  20.         String update_sql = "update products set name = ?, price = ? where id = ?";
  21.  
  22.         if (entity.getId() == 0) {
  23.             jdbcTemplate.update(insert_sql, entity.getName(), entity.getPrice());
  24.  
  25.         } else {
  26.             jdbcTemplate.update(update_sql, entity.getName(), entity.getPrice(), entity.getId());
  27.  
  28.         }
  29.     }
  30.  
  31.     @Override
  32.     public void delete(int id) {
  33.         String sql = "delete from products where id = ?";
  34.         jdbcTemplate.update(sql, id);
  35.     }
  36.  
  37.     @Override
  38.     public Product find(int id) {
  39.         String sql = "select * from products where id = ?";
  40.  
  41.         try {
  42.             return jdbcTemplate.queryForObject(sql,
  43.                     (rs, rowNum) -> new Product(rs.getInt("id"), rs.getString("name"), rs.getDouble("price")), id);
  44.         } catch (Exception e) {
  45.             return null;
  46.         }
  47.     }
  48.  
  49.     @Override
  50.     public List<Product> findAll() {
  51.         String sql = "select * from products";
  52.  
  53.         return jdbcTemplate.query(sql,
  54.                 (rs, rowNum) -> new Product(rs.getInt("id"), rs.getString("name"), rs.getDouble("price")));
  55.  
  56.     }
  57.  
  58.     @Override
  59.     public Product findByName(String str) {
  60.         String sql = "select * from products where name = ? LIMIT 1";
  61.  
  62.         try {
  63.             return jdbcTemplate.queryForObject(sql,
  64.                     (rs, rowNum) -> new Product(rs.getInt("id"), rs.getString("name"), rs.getDouble("price")), str);
  65.         } catch (Exception e) {
  66.             return null;
  67.         }
  68.     }
  69.  
  70.     @Override
  71.     public List<Product> findAllByName(String str) {
  72.         String sql = "select * from products where name = ?";
  73.  
  74.         try {
  75.             return jdbcTemplate.query(sql,
  76.                     (rs, rowNum) -> new Product(rs.getInt("id"), rs.getString("name"), rs.getDouble("price")), str);
  77.         } catch (Exception e) {
  78.             return null;
  79.         }
  80.     }
  81.  
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement