I need to save the result of my query in a List and then send it to an xhtml

1

How do I send these data from two tables to a List and how do I print them in the xhtml?

What I need is that you bring me this information Ex:

User Table Name: Juan IdCargo: 1

Cargo Tables IdLoad: 1 Name: Administrator

Result: User.Name Cargo.Name - > Juan Administrator

The query would be:

  

SELECT User.Name, Position.Name   FROM User inner join Cargo   on User.IdCargo = Cargo.IdCargo

I have the following code:

public List<Objeto> getUsuarios(){
    List Usuarios<Objeto> = new ArrayList<>();

    try 
    {
        Connection conexion=this.getConection();

        String Query="SELECT Usuario.IdUsuario,Usuario.Nombre,Cargo.Nombre FROM Usuario inner join Cargo on Usuario.IdCargo = cargo.IdCargo"; 
        Statement sentenciaSQL = conexion.createStatement();
        ResultSet cdr = sentenciaSQL.executeQuery(Query);

        while(cdr.next())
        {
            BeanUsuario dUsu = new BeanUsuario(); 
            dUsu.setIdUsuario(cdr.getInt("IdUsuario"));
            dUsu.setNombre(cdr.getString("Nombre")); 
            Usuarios.add(dUsu); 

            BeanCargo dCar = new BeanCargo(); 
            dCar.setNombre(cdr.getString("Nombre")); 
            Usuarios.add(dCar);
        }

        cdr.close();
        conexion.close(); 

    }
    catch (Exception e) 
    {

    } 
    finally
    {
        return Usuarios;
    }    
}
    
asked by Max 03.03.2017 в 21:49
source

1 answer

1

Good is about relationships between classes. The relations between classes and the way in which they relate to each other.

If you want to access the data of the user with the user's data, it is very simple:

public class BeanUsuario{// Yo no la llamaría así dado que no sigue la denominacion Java Usuario es mejor nombre
  private int idUsuario; //Te recomiendo trabajar con long
  private String nombre;
  private BeanCargo cargo;

/*
constructores metodos y getters y setters.
*/
}

public class BeanCargo{//tambien te recomiendo cambiar de nombre
  private int idCargo;//lo mismo que lo anterior
  private String nombre;

/*
constructores metodos y getters y setters.
*/
}

   while(cdr.next())
        {
            BeanUsuario dUsu = new BeanUsuario(); 
            dUsu.setIdUsuario(cdr.getInt("IdUsuario"));
            dUsu.setNombre(cdr.getString("Nombre")); 
            usuarios.add(dUsu); //el nombre de los objetos siempre en minuscula

            BeanCargo dCar = new BeanCargo(); 
            dCar.setNombre(cdr.getString("Nombre")); 

            dUsu.setCargo(dCar);

             usuarios.add(dCar);
        }

This way whenever you want you will have the data of the BeanCargo class. By the way, I recommend you return the code of the position in the query since this way you open the possibility of taking actions from that view and it does not mean an increase of resources and implements in a more logical way the Object Oriented Programming and not the structured programming.

Relations between classes can be expressed in many ways. In your case it is the association.

Association: The relationship between classes known as Association allows you to associate objects that collaborate with each other. It should be noted that it is not a strong relationship, that is, the lifetime of one object does not depend on the other.

Example:

A customer can have many Purchase Orders associated with it, instead a purchase order can only have one customer associated with it.

    
answered by 04.03.2017 в 08:24