Advertisement
mc_zefirka

class ProductsDAOImpl

Apr 7th, 2021
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.26 KB | None | 0 0
  1. package com.itstep.mvn.app.jdbc.dao;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.List;
  8.  
  9. import com.itstep.mvn.app.model.jdbc.Product;
  10. import com.itstep.mvn.app.util.DBUtils;
  11.  
  12. public class ProductDaoImpl implements ProductDao {
  13.  
  14.     @Override
  15.     public Product getProduct(String pCode) {
  16.         Connection conn = null;
  17.         Statement stmt = null;
  18.         ResultSet rs = null;
  19.         Product product = null;
  20.         String sql = "SELECT * FROM products WHERE productCode = '" + pCode + "'";
  21.  
  22.         try {
  23.             conn = DBUtils.getConnection();
  24.             stmt = conn.createStatement();
  25.             rs = stmt.executeQuery(sql);
  26.  
  27.             if (rs.next()) {
  28.                 product.setProductCode(pCode);
  29.                 product = new Product();
  30.                 product.setBuyPrice(rs.getDouble(8));
  31.                 product.setMSRP(rs.getDouble(9));
  32.                 product.setProductDescription(rs.getString(6));
  33.                 product.setProductLine(rs.getString(3));
  34.                 product.setProductName(rs.getString(2));
  35.                 product.setProductScale(rs.getString(4));
  36.                 product.setProductVendor(rs.getString(5));
  37.                 product.setQuantityInStock(rs.getInt(7));
  38.  
  39.             }else {
  40.                 System.out.println("product is not found");
  41.             }
  42.                
  43.         } catch (SQLException e) {
  44.  
  45.             e.printStackTrace();
  46.  
  47.         }
  48.         DBUtils.release(conn, stmt, rs);
  49.         return product;
  50.     }
  51.  
  52.     @Override
  53.     public boolean addProduct(Product pCode) {
  54.         String sql = "INSERT INTO `products` (`productCode`, `productName`, `productLine`, `productScale`, `productVendor`, `productDescription`, `quantityInStock`, `buyPrice`, `MSRP`) "
  55.                 + "VALUES ('"+pCode.getProductCode()+"', '"+pCode.getProductName()+"', '"+pCode.getProductLine()+"', '"+pCode.getProductScale()+"', '"+pCode.getProductVendor()+"', "
  56.                         + "'"+pCode.getProductDescription()+"', '"+pCode.getQuantityInStock()+"', '"+pCode.getBuyPrice()+"', '"+pCode.getMSRP()+"'";
  57.  
  58.         Connection conn = null;
  59.         Statement stmt = null;
  60.  
  61.         try {
  62.             conn = DBUtils.getConnection();
  63.             stmt = conn.createStatement();
  64.             int result = stmt.executeUpdate(sql);
  65.             if (result == 1) {
  66.                 System.out.println("Product '" + pCode.getProductCode() + "' was created.");
  67.             } else {
  68.                 throw new RuntimeException("Bad DESIGN!!!");
  69.             }
  70.  
  71.         } catch (SQLException e) {
  72.             e.printStackTrace();
  73.         } finally {
  74.             DBUtils.release(conn, stmt, null);
  75.         }
  76.    
  77.         return false;
  78.     }
  79.  
  80.     @Override
  81.     public boolean editProduct(Product pCode) {
  82.         String sql = "DELETE FROM classicmodel.product WHERE productCode = " + pCode;
  83.         Connection conn = null;
  84.         Statement stmt = null;
  85.         try {
  86.             conn = DBUtils.getConnection();
  87.             stmt = conn.createStatement();
  88.             int res = stmt.executeUpdate(sql);
  89.            
  90.            
  91.            
  92.         } catch (SQLException e) {
  93.             e.printStackTrace();
  94.         } finally {
  95.              DBUtils.release(conn, stmt, null);
  96.         }
  97.         return false;
  98.     }
  99.     @Override
  100.     public boolean removeProduct(String pCode) {
  101.         String sql = "DELETE FROM classicmodel.product WHERE productCode = " + pCode;
  102.         Connection conn = null;
  103.         Statement stmt = null;
  104.         try {
  105.             conn = DBUtils.getConnection();
  106.             stmt = conn.createStatement();
  107.             int res = stmt.executeUpdate(sql);
  108.             if(res == 1 ) {
  109.                 System.out.println("Product["+pCode+"] was removed");
  110.                 return true;
  111.             } else {
  112.                 System.out.println("Product["+pCode+"] not found");
  113.             }
  114.            
  115.         } catch (SQLException e) {
  116.             e.printStackTrace();
  117.         } finally {
  118.              DBUtils.release(conn, stmt, null);
  119.         }
  120.         return false;
  121.     }
  122.  
  123.     @Override
  124.     public List<Product> allProduct() {
  125.         // TODO Auto-generated method stub
  126.         return null;
  127.     }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement