How to implement MVC in Java?

1

I am studying about MVC, however I have doubts about its correct implementation since I see everywhere that it is done differently.

Now, what I do is the following:

1- I create the DTO that represent my tables in the BD.

2- I create the corresponding DAO for each DTO.

3- I create a Controller for each DTO ** (I do not know if this is correct)

4- I create the View (Using Window Builder in NetBeans).

5- In the code of the Button I instate the DTO and through the setters I modify its attributes. (I do not know if this is correct)

6- I invoke the Controller, who passed the DTO object as parameters.

In the exercises that I have seen, I observe that the Vista does not call the Controller, its not that the Controller calls the View.

A fragment of my application:

MODEL: EmployeeDTO:

package Modelo;
/**
 *
 * @author Julian Lopez
 */
public class Empleado {

    private int empleadoId;
    private String empleadoNombre;
    private String empleadoApellido;
    private String empleadoContacto;


    public Empleado(int empleadoId, String empleadoNombre, String empleadoApellido, String empleadoContacto) {
        this.empleadoId = empleadoId;
        this.empleadoNombre = empleadoNombre;
        this.empleadoApellido = empleadoApellido;
        this.empleadoContacto = empleadoContacto;

    }

    public Empleado() {
        this.empleadoId = 0;
        this.empleadoNombre = "";
        this.empleadoApellido = "";
        this.empleadoContacto = "";
    }

    public int getEmpleadoId() {
        return empleadoId;
    }

    public void setEmpleadoId(int empleadoId) {
        this.empleadoId = empleadoId;
    }

    public String getEmpleadoNombre() {
        return empleadoNombre;
    }

    public void setEmpleadoNombre(String empleadoNombre) {
        this.empleadoNombre = empleadoNombre;
    }

    public String getEmpleadoApellido() {
        return empleadoApellido;
    }

    public void setEmpleadoApellido(String empleadoApellido) {
        this.empleadoApellido = empleadoApellido;
    }

    public String getEmpleadoContacto() {
        return empleadoContacto;
    }

    public void setEmpleadoContacto(String empleadoContacto) {
        this.empleadoContacto = empleadoContacto;
    }

}

MODEL: EmpleadoDAO

public class EmpleadoDAO {
    public ArrayList<Empleado> listarEmpleado(int empleadoId) {
        Connection con = null;
        PreparedStatement prepared = null;
        ResultSet result = null;
        ArrayList<Empleado> listado = new ArrayList<>();
        try {
            con = Conexion.getConection();
            String selectSQL = "";

            if (empleadoId == 0) {
                selectSQL = "SELECT * FROM tb_empleado";
            } else {
                selectSQL = "SELECT * FROM tb_empleado WHERE Empleado_ID = ?";
            }
            prepared = con.prepareStatement(selectSQL);

            if (empleadoId != 0) {
                prepared.setInt(1, empleadoId);
            }

            result = prepared.executeQuery();
            Empleado empleado = null;

            while (result.next()) {
                empleado = new Empleado();
                empleado.setEmpleadoId(result.getInt("Empleado_ID"));
                empleado.setEmpleadoNombre(result.getString("Empleado_Nombre"));
                empleado.setEmpleadoApellido(result.getString("Empleado_Apellido"));
                empleado.setEmpleadoContacto(result.getString("Empleado_Contacto"));
                listado.add(empleado);
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Código : "
                    + ex.getErrorCode() + "\nError :" + ex.getMessage());
        }
        return listado;
    }

    public int grabarEmpleado(Empleado empleado) {
        Connection con = null;
        PreparedStatement prepared = null;
        int resultado = 0;

        try {
            con = Conexion.getConection();
            String insertSQL = "INSERT INTO tb_empleado VALUES ( ?, ?, ?, ?)";
            prepared = con.prepareStatement(insertSQL);
            prepared.setInt(1, empleado.getEmpleadoId());
            prepared.setString(2, empleado.getEmpleadoNombre());
            prepared.setString(3, empleado.getEmpleadoApellido());
            prepared.setString(4, empleado.getEmpleadoContacto());
            resultado = prepared.executeUpdate();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Código : "
                    + ex.getErrorCode() + "\nError :" + ex.getMessage());
        }
        return resultado;
    }

DRIVER : DriverEmployee:

public class ControladorEmpleado {
    public static ArrayList<Empleado> listarEmpleado(int empleadoId) {
        ArrayList<Empleado> listaEmpleado; //= new ArrayList<>();
        EmpleadoDAO empleadoDAO = new EmpleadoDAO();
        listaEmpleado = empleadoDAO.listarEmpleado(empleadoId);
        return listaEmpleado;

    }

    public static int grabarEmpleado(Empleado empleado) {
        EmpleadoDAO empleadoDAO = new EmpleadoDAO();
        int resultado = empleadoDAO.grabarEmpleado(empleado);
        return resultado;
    }

VIEW: Add Employee (Here I urge the EmployeeDTO and modify its attributes through the setters, later I invoke the corresponding Controller and I pass the object EmployeeDTO as parameter Is it correct to call the EmployeeDTO being this part of the MODEL?

private void btnGrabarActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        if (txtEmpleadoID.getText().equals("") || txtEmpleadoNombre.getText().equals("")
                || txtEmpleadoApellido.getText().equals("") || txtEmpleadoContacto.getText().equals("")
                || txtEmpleadoDireccion.getText().equals("") || txtEmpleadoSalario.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Por favor diligencie todos los datos", "Falta Informacion", JOptionPane.INFORMATION_MESSAGE);
        } else {

            Empleado miEmpleado = new Empleado();
            CargoEmpleado cargosEmpleado = new CargoEmpleado();

            cargosEmpleado = (CargoEmpleado) cboCargo.getSelectedItem();

            int empleadoId = Integer.parseInt(txtEmpleadoID.getText());
            String empleadoNombre = txtEmpleadoNombre.getText();
            String empleadoApellido = txtEmpleadoApellido.getText();
            String empleadoContacto = txtEmpleadoContacto.getText();
            String empleadoDireccion = txtEmpleadoDireccion.getText();
            int empleadoSalario = Integer.parseInt(txtEmpleadoSalario.getText());

            miEmpleado.setEmpleadoId(empleadoId);
            miEmpleado.setEmpleadoNombre(empleadoNombre);
            miEmpleado.setEmpleadoApellido(empleadoApellido);
            miEmpleado.setEmpleadoContacto(empleadoContacto);

            int resultado = ControladorEmpleado.grabarEmpleado(miEmpleado);

            if (resultado == 1) {
                JOptionPane.showMessageDialog(null, "Nuevo empleado registrado.");
            } else {
                JOptionPane.showMessageDialog(null, "No se pudo realizar el registro.", "Oopps... ", JOptionPane.ERROR_MESSAGE);
            }

        }
    }

PS: I am aware that being a bit more demanding, validations of the fields should have been done in a separate Class and not as I have it here, where it is valid in the code of the button. I do not use any specific Framework.

    
asked by Julian David 30.01.2018 в 01:38
source

0 answers