Advertisement
andersonalmada

Untitled

Jul 7th, 2022
1,273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.14 KB | None | 0 0
  1. package mandacaru3.dao;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. import mandacaru3.entities.Product;
  11.  
  12. public class ProductJDBCDAO implements ProductDAO {
  13.  
  14.     @Override
  15.     public void save(Product entity) {
  16.         Connection con = null;
  17.         try {
  18.             con = ConnectionFactory.getConnection();
  19.             String insert_sql = "insert into products (name, price) values (?, ?)";
  20.             String update_sql = "update products set name = ?, price = ? where id = ?";
  21.             PreparedStatement pst;
  22.             if (entity.getId() == 0) {
  23.                 pst = con.prepareStatement(insert_sql);
  24.             } else {
  25.                 pst = con.prepareStatement(update_sql);
  26.                 pst.setInt(3, entity.getId());
  27.             }
  28.             pst.setString(1, entity.getName());
  29.             pst.setDouble(2, entity.getPrice());
  30.             pst.executeUpdate();
  31.         } catch (SQLException e) {
  32.             e.printStackTrace();
  33.         } finally {
  34.             try {
  35.                 if (con != null)
  36.                     con.close();
  37.             } catch (SQLException e) {
  38.                 e.printStackTrace();
  39.             }
  40.         }
  41.     }
  42.  
  43.     @Override
  44.     public void delete(int id) {
  45.         Connection con = null;
  46.         try {
  47.             con = ConnectionFactory.getConnection();
  48.             String sql = "delete from products where id = ?";
  49.             PreparedStatement pst = con.prepareStatement(sql);
  50.             pst.setInt(1, id);
  51.             pst.executeUpdate();
  52.         } catch (SQLException e) {
  53.             e.printStackTrace();
  54.         } finally {
  55.             try {
  56.                 if (con != null)
  57.                     con.close();
  58.             } catch (SQLException e) {
  59.                 e.printStackTrace();
  60.             }
  61.         }
  62.     }
  63.  
  64.     @Override
  65.     public Product find(int id) {
  66.         Connection con = null;
  67.         Product p = null;
  68.         try {
  69.             con = ConnectionFactory.getConnection();
  70.             String sql = "select * from products where id = ?";
  71.             PreparedStatement pst = con.prepareStatement(sql);
  72.             pst.setInt(1, id);
  73.             ResultSet rs = pst.executeQuery();
  74.             if (rs.next()) {
  75.                 p = map(rs);
  76.             }
  77.         } catch (SQLException e) {
  78.             e.printStackTrace();
  79.         } finally {
  80.             try {
  81.                 if (con != null)
  82.                     con.close();
  83.             } catch (SQLException e) {
  84.                 e.printStackTrace();
  85.             }
  86.         }
  87.         return p;
  88.     }
  89.  
  90.     @Override
  91.     public List<Product> findAll() {
  92.         Connection con = null;
  93.         List<Product> result = null;
  94.         try {
  95.             con = ConnectionFactory.getConnection();
  96.             String sql = "select * from products";
  97.             PreparedStatement pst = con.prepareStatement(sql);
  98.             ResultSet rs = pst.executeQuery();
  99.             result = new ArrayList<Product>();
  100.             while (rs.next()) {
  101.                 Product cl = map(rs);
  102.                 result.add(cl);
  103.             }
  104.         } catch (SQLException e) {
  105.             e.printStackTrace();
  106.         } finally {
  107.             try {
  108.                 if (con != null)
  109.                     con.close();
  110.             } catch (SQLException e) {
  111.                 e.printStackTrace();
  112.             }
  113.         }
  114.         return result;
  115.     }
  116.  
  117.     @Override
  118.     public Product findByName(String str) {
  119.         Connection con = null;
  120.         Product p = null;
  121.         try {
  122.             con = ConnectionFactory.getConnection();
  123.             String sql = "select * from products where name = ?";
  124.             PreparedStatement pst = con.prepareStatement(sql);
  125.             pst.setString(1, str);
  126.             ResultSet rs = pst.executeQuery();
  127.             if (rs.next()) {
  128.                 p = map(rs);
  129.             }
  130.         } catch (SQLException e) {
  131.             e.printStackTrace();
  132.         } finally {
  133.             try {
  134.                 if (con != null)
  135.                     con.close();
  136.             } catch (SQLException e) {
  137.                 e.printStackTrace();
  138.             }
  139.         }
  140.         return p;
  141.     }
  142.  
  143.     @Override
  144.     public List<Product> findAllByName(String str) {
  145.         Connection con = null;
  146.         List<Product> result = null;
  147.         try {
  148.             con = ConnectionFactory.getConnection();
  149.             String sql = "select * from products where name = ?";
  150.             PreparedStatement pst = con.prepareStatement(sql);
  151.             pst.setString(1, str);
  152.             ResultSet rs = pst.executeQuery();
  153.             result = new ArrayList<Product>();
  154.             while (rs.next()) {
  155.                 Product cl = map(rs);
  156.                 result.add(cl);
  157.             }
  158.         } catch (SQLException e) {
  159.             e.printStackTrace();
  160.         } finally {
  161.             try {
  162.                 if (con != null)
  163.                     con.close();
  164.             } catch (SQLException e) {
  165.                 e.printStackTrace();
  166.             }
  167.         }
  168.         return result;
  169.     }
  170.  
  171.     private Product map(ResultSet rs) throws SQLException {
  172.         Product p = new Product();
  173.         p.setId(rs.getInt("id"));
  174.         p.setName(rs.getString("name"));
  175.         p.setPrice(rs.getDouble("price"));
  176.         return p;
  177.     }
  178. }
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement