Advertisement
xlrnxnlx

[class] DAOEmployee

Feb 24th, 2014
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. package tbanco.dao;
  2.  
  3. import java.sql.*;
  4. import tbanco.modelo.Employee;
  5.  
  6. public class DAOEmployee extends DAO implements InterfaceDAO {
  7.  
  8.     private Employee employee;
  9.    
  10.     // <editor-fold defaultstate="collapsed" desc="Insert, Delete, Update, Select">
  11.     @Override
  12.     public boolean insert(Object object) {
  13.         boolean success = false;
  14.         if (connect()) {
  15.             this.employee = (Employee)object; // cast
  16.             String query =
  17.             "insert into employee values (" + employee.getId() + ", '" + employee.getName() +"', " + employee.getPhone() + ", '" + employee.getSalary() + "')";
  18.             try {
  19.                 state.executeQuery(query);
  20.                 success = true;
  21.             } catch (SQLException e) {
  22.                 lastErrorMessage = "#N: " + e.getErrorCode() + "#Message: " + e.getMessage();
  23.                 disconnect();
  24.             }
  25.             disconnect();
  26.         }
  27.         return success;
  28.     }
  29.  
  30.     @Override
  31.     public boolean delete(Object object) {
  32.         boolean success = false;
  33.         if (connect()) {
  34.             this.employee = (Employee)object;
  35.             String query =
  36.             "delete from employee e where e.id = " + employee.getId();
  37.             try {
  38.                 state.execute(query);
  39.                 success = true;
  40.             } catch (SQLException e) {
  41.                 lastErrorMessage = "#N: " + e.getErrorCode() + "#Message: " + e.getMessage();
  42.                 disconnect();
  43.             }
  44.             disconnect();
  45.         }
  46.         return success;
  47.     }
  48.  
  49.     @Override
  50.     public boolean update(Object object) {
  51.         boolean success = false;
  52.         if (connect()) {
  53.             this.employee = (Employee)object;
  54.             String query =
  55.             "update employee e set e.id = " + employee.getId() + ", e.name = '" + employee.getName() + "', e.phone = " + employee.getPhone() + ", e.salary = '" + employee.getSalary() + "'" + "";
  56.             try {
  57.                 state.execute(query);
  58.                 success = true;
  59.             } catch (SQLException e) {
  60.                 lastErrorMessage = "#N: " + e.getErrorCode() + "#Message: " + e.getMessage();
  61.                 disconnect();
  62.             }
  63.             disconnect();
  64.         }
  65.         return success;
  66.     }
  67.  
  68.     @Override
  69.     public boolean select(Object object) {
  70.         boolean success = false;
  71.         if (connect()) {
  72.             this.employee = (Employee)object;
  73.             String query =
  74.             "select * from employee e where e.id = " + employee.getId();
  75.             try {
  76.                 result = state.executeQuery(query);
  77.                 while (result.next()) {
  78.                     employee.setId(result.getInt("id"));
  79.                     employee.setName(result.getString("name"));
  80.                     employee.setPhone(result.getInt("phone"));
  81.                     employee.setSalary(result.getDouble("salary"));
  82.                 }
  83.                 if ((employee.getName() != null) && (employee.getSalary() != null))
  84.                     success = true;
  85.             } catch (SQLException e) {
  86.                 lastErrorMessage = "#N: " + e.getErrorCode() + "#Message: " + e.getMessage();
  87.                 disconnect();
  88.             }
  89.             disconnect();
  90.         }
  91.         return success;
  92.     }
  93.     //</editor-fold>
  94.    
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement