×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Israel Edet
Added: Dec 4, 2016 7:54 PM
Modified: Jan 4, 2017 10:03 PM
Views: 2196
Tags: no tags
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package inventorysystem.data;
  7.  
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11. import java.text.DateFormat;
  12. import java.text.SimpleDateFormat;
  13. import java.util.ArrayList;
  14. import java.util.Date;
  15. import java.util.logging.Logger;
  16.  
  17. /**
  18.  *
  19.  * @author Epic
  20.  */
  21. public class Suppler {
  22.     private int supplerId;
  23.     private String supplerName;
  24.     private String supplerAddress;
  25.     private String supplerPhone;
  26.     private String supplerBusinessName;
  27.  
  28.     public String getSupplerBusinessName() {
  29.         return supplerBusinessName;
  30.     }
  31.  
  32.     public void setSupplerBusinessName(String supplerBusinessName) {
  33.         this.supplerBusinessName = supplerBusinessName;
  34.     }
  35.     private static final Logger LOG = Logger.getLogger(Suppler.class.getName());
  36.     private DBConnectionClass connectionClass;
  37.  
  38.     public Suppler() {
  39.         connectionClass = new DBConnectionClass();
  40.     }
  41.     public Suppler(int supplierId, String supplierName, String supplierAddress, String supplierPhone, String supplerBusinessName) {
  42.         this.supplerId = supplierId;
  43.         this.supplerName = supplierName;
  44.         this.supplerAddress = supplierAddress;
  45.         this.supplerPhone = supplierPhone;
  46.         this.supplerBusinessName = supplerBusinessName;
  47.         connectionClass = new DBConnectionClass();
  48.     }
  49.  
  50.     /**
  51.      * @return the supplerId
  52.      */
  53.     public int getSupplerId() {
  54.         return supplerId;
  55.     }
  56.  
  57.     /**
  58.      * @param supplerId the supplerId to set
  59.      */
  60.     public void setSupplerId(int supplerId) {
  61.         this.supplerId = supplerId;
  62.     }
  63.  
  64.     /**
  65.      * @return the supplerName
  66.      */
  67.     public String getSupplerName() {
  68.         return supplerName;
  69.     }
  70.  
  71.     /**
  72.      * @param supplerName the supplerName to set
  73.      */
  74.     public void setSupplerName(String supplerName) {
  75.         this.supplerName = supplerName;
  76.     }
  77.  
  78.     /**
  79.      * @return the supplerAddress
  80.      */
  81.     public String getSupplerAddress() {
  82.         return supplerAddress;
  83.     }
  84.  
  85.     /**
  86.      * @param supplerAddress the supplerAddress to set
  87.      */
  88.     public void setSupplerAddress(String supplerAddress) {
  89.         this.supplerAddress = supplerAddress;
  90.     }
  91.  
  92.     /**
  93.      * @return the supplerPhone
  94.      */
  95.     public String getSupplerPhone() {
  96.         return supplerPhone;
  97.     }
  98.  
  99.     /**
  100.      * @param supplerPhone the supplerPhone to set
  101.      */
  102.     public void setSupplerPhone(String supplerPhone) {
  103.         this.supplerPhone = supplerPhone;
  104.     }
  105.     /**
  106.      * Add a new supplier to the database
  107.      * @return Boolean true if information is saved
  108.      * @throws SQLException
  109.      */
  110.     public Boolean addSupplier() throws SQLException{
  111.        
  112.         DateFormat df = new SimpleDateFormat("yyyy/MM/DD");
  113.         Date date = new Date();
  114.         Statement statement = this.connectionClass.getConnection().createStatement();
  115.        
  116.         return statement.execute("INSERT INTO suppliers (supplier_name,supplier_date,supplier_phone,supplier_biz_name,supplier_address)"
  117.                 + " VALUES('"+getSupplerName()+"','"+df.format(date)+"','"+getSupplerPhone()+"','"+getSupplerBusinessName()+"','"+getSupplerAddress()+"')");
  118.          
  119.        
  120.     }
  121.     /**
  122.      * Update supplier information
  123.      * @param supplierId
  124.      * @return Boolean true if information is updated
  125.      * @throws SQLException
  126.      */
  127.     public Boolean updateSupplier(int supplierId) throws SQLException{
  128.         boolean isSaved = false;
  129.        
  130.         Statement statement = this.connectionClass.getConnection().createStatement();
  131.         if(statement.execute("UPDATE suppliers SET supplier_name = '"+getSupplerName()+"',supplier_phone = '"+getSupplerPhone()+"' WHERE supplier_id = '"+supplierId+"')"))
  132.             isSaved = true;
  133.         return isSaved;
  134.     }
  135.     /**
  136.      * Delete a supplier from database
  137.      * @param supplierId
  138.      * @return
  139.      * @throws SQLException
  140.      */
  141.     public  Boolean removeSupplier(int supplierId) throws SQLException{
  142.         boolean isDeleted = false;
  143.         Statement statement = this.connectionClass.getConnection().createStatement();
  144.         if(statement.execute("DELETE FROM suppliers WHERE supplier_id = '"+supplierId+"'"))
  145.             isDeleted = true;
  146.         return  isDeleted;
  147.     }
  148.    /**
  149.      * Get Suppler Object
  150.      * @param supplierBizName  the bix name of supplier to retrieve
  151.      * @param allSupplier  boolean should retrieve all supplier
  152.      * @return A Suppler Object
  153.      * @throws SQLException
  154.      */
  155. public ArrayList<Suppler> getSupplier(String supplierBizName,boolean allSupplier) throws SQLException{
  156.     ArrayList<Suppler> supplierList = new ArrayList<>();
  157.     Suppler supplier = null;
  158.     String sql = "";
  159.     if(allSupplier){// if shud select all customers
  160.         sql = "SELECT * FROM suppliers";
  161.     }else {
  162.         sql = "SELECT * FROM suppliers where supplier_biz_name = '"+supplierBizName+"'";
  163.     }
  164.     if(this.connectionClass.getConnection() != null && !sql.isEmpty()){
  165.     //if there is a connection to db and sql query is not empty
  166.         Statement s = this.connectionClass.getConnection().createStatement();
  167.         ResultSet resultSet = s.executeQuery(sql);
  168.        
  169.         while (resultSet.next()) {
  170.             supplier = new Suppler(
  171.                     resultSet.getInt("supplier_id"),
  172.                     resultSet.getString("supplier_name"),
  173.                     resultSet.getString("supplier_address"),
  174.                     resultSet.getString("supplier_phone"),
  175.                     resultSet.getString("supplier_biz_name")
  176.              );
  177.             supplierList.add(supplier);
  178.         }
  179.     }
  180.     return supplierList;
  181. }
  182. /**
  183.  * Check if a value already exist in the database
  184.  * @param columnToSearch
  185.  * @param valueToSearchFor
  186.  * @return true if no unique
  187.  */
  188. public boolean checkIfExist(String columnToSearch,String valueToSearchFor) throws SQLException{
  189.     Statement s = this.connectionClass.getConnection().createStatement();
  190.     ResultSet resultSet = s.executeQuery("select * from suppliers where `"+columnToSearch+"` = '"+valueToSearchFor+"'");
  191.     if (resultSet.first()) {
  192.         return true;
  193.     }
  194.     return false;
  195. }
  196. }
  197.