Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | None | 0 0
  1. package fbcn.model.entity.Company;
  2.  
  3. import fbcn.Values;
  4. import fbcn.model.DBConnection;
  5. import fbcn.model.OrderedList;
  6. import fbcn.model.entity.User.User;
  7.  
  8. import java.sql.ResultSet;
  9.  
  10. public class CompanyDAO implements iCompanyDAO{
  11.     private DBConnection db;
  12.     private ResultSet result;
  13.  
  14.     public CompanyDAO() throws Exception{
  15.         db = new DBConnection();
  16.     }
  17.  
  18.     @Override
  19.     public OrderedList<Company> findAll() throws Exception {
  20.         db.prepare(Values.sqlCompanyFindAll);
  21.         result = db.pstmt.executeQuery();
  22.  
  23.         return dump(result);
  24.     }
  25.  
  26.     @Override
  27.     public OrderedList<Company> findById(int id) throws Exception {
  28.         db.prepare(Values.sqlCompanyFindById);
  29.         db.pstmt.setInt(1, id);
  30.         result = db.pstmt.executeQuery();
  31.  
  32.         return dump(result);
  33.     }
  34.  
  35.     @Override
  36.     public OrderedList<Company> findByName(String name ) throws Exception{
  37.         db.prepare(Values.sqlCompanyFindByName);
  38.         db.pstmt.setString(1,name);
  39.         result = db.pstmt.executeQuery();
  40.  
  41.         return dump(result);
  42.     }
  43.  
  44.     @Override
  45.     public int findIdByName(String  name) throws Exception{
  46.         db.prepare(Values.sqlCompanyGetNameById);
  47.         db.pstmt.setString(1,name);
  48.         result = db.pstmt.executeQuery();
  49.         if (result.next()){
  50.             return  result.getInt(1);
  51.         }
  52.  
  53.         else return 0;
  54.     }
  55.     @Override
  56.     public void updateAllByDeleteId(int id) throws Exception {
  57.         db.prepare(Values.sqlCompanyUpdateAllById);
  58.         db.pstmt.setInt(1,id);
  59.  
  60.         db.pstmt.executeUpdate();
  61.         db.pstmtClose();
  62.     }
  63.  
  64.     @Override
  65.     public void insert(Company company) throws Exception {
  66.         db.prepare(Values.sqlCompanyInsert);
  67.         db.pstmt.setString(1, company.getName());
  68.         db.pstmt.setString(2, company.getType());
  69.         db.pstmt.setFloat(3, company.getCapital());
  70.  
  71.         db.pstmt.executeUpdate();
  72.         db.pstmtClose();
  73.     }
  74.  
  75.     @Override
  76.     public void update(Company company) throws Exception {
  77.         db.prepare(Values.sqlCompanyUpdate);
  78.         db.pstmt.setString(1, company.getName());
  79.         db.pstmt.setString(2, company.getType());
  80.         db.pstmt.setFloat(3, company.getCapital());
  81.         db.pstmt.setInt(4, company.getId());
  82.         db.pstmt.executeUpdate();
  83.         db.pstmtClose();
  84.     }
  85.  
  86.     @Override
  87.     public void delete(Company company) throws Exception {
  88.         db.prepare(Values.sqlCompanyDelete);
  89.         db.pstmt.setInt(1, company.getId());
  90.         db.pstmt.executeUpdate();
  91.         db.pstmtClose();
  92.     }
  93.  
  94.     @Override
  95.     public OrderedList<Company> dump(ResultSet result) throws Exception {
  96.         OrderedList<Company> list = new OrderedList<Company>();
  97.  
  98.         while(result.next()){
  99.             Company company;
  100.             company = new Company(
  101.                     result.getInt("id"),
  102.                     result.getString("name"),
  103.                     result.getString("type"),
  104.                     result.getFloat("capital")
  105.             );
  106.  
  107.             list.add(company);
  108.         }
  109.  
  110.         db.pstmtClose();
  111.  
  112.         return list;
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement