Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tbanco.dao;
- import java.sql.*;
- import tbanco.modelo.Employee;
- public class DAOEmployee extends DAO implements InterfaceDAO {
- private Employee employee;
- // <editor-fold defaultstate="collapsed" desc="Insert, Delete, Update, Select">
- @Override
- public boolean insert(Object object) {
- boolean success = false;
- if (connect()) {
- this.employee = (Employee)object; // cast
- String query =
- "insert into employee values (" + employee.getId() + ", '" + employee.getName() +"', " + employee.getPhone() + ", '" + employee.getSalary() + "')";
- try {
- state.executeQuery(query);
- success = true;
- } catch (SQLException e) {
- lastErrorMessage = "#N: " + e.getErrorCode() + "#Message: " + e.getMessage();
- disconnect();
- }
- disconnect();
- }
- return success;
- }
- @Override
- public boolean delete(Object object) {
- boolean success = false;
- if (connect()) {
- this.employee = (Employee)object;
- String query =
- "delete from employee e where e.id = " + employee.getId();
- try {
- state.execute(query);
- success = true;
- } catch (SQLException e) {
- lastErrorMessage = "#N: " + e.getErrorCode() + "#Message: " + e.getMessage();
- disconnect();
- }
- disconnect();
- }
- return success;
- }
- @Override
- public boolean update(Object object) {
- boolean success = false;
- if (connect()) {
- this.employee = (Employee)object;
- String query =
- "update employee e set e.id = " + employee.getId() + ", e.name = '" + employee.getName() + "', e.phone = " + employee.getPhone() + ", e.salary = '" + employee.getSalary() + "'" + "";
- try {
- state.execute(query);
- success = true;
- } catch (SQLException e) {
- lastErrorMessage = "#N: " + e.getErrorCode() + "#Message: " + e.getMessage();
- disconnect();
- }
- disconnect();
- }
- return success;
- }
- @Override
- public boolean select(Object object) {
- boolean success = false;
- if (connect()) {
- this.employee = (Employee)object;
- String query =
- "select * from employee e where e.id = " + employee.getId();
- try {
- result = state.executeQuery(query);
- while (result.next()) {
- employee.setId(result.getInt("id"));
- employee.setName(result.getString("name"));
- employee.setPhone(result.getInt("phone"));
- employee.setSalary(result.getDouble("salary"));
- }
- if ((employee.getName() != null) && (employee.getSalary() != null))
- success = true;
- } catch (SQLException e) {
- lastErrorMessage = "#N: " + e.getErrorCode() + "#Message: " + e.getMessage();
- disconnect();
- }
- disconnect();
- }
- return success;
- }
- //</editor-fold>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement