How to search the database with three types of users?

0

Hi, I'm doing a college project, it's an internship site project, it has to be developed in Java WEB with database. It has three types of users, the student, the company, and the administrator. I would like to know if there is any way I can log in to the three tables at the time of logging in.

I was thinking of making a user table, which has the email, the password, the type of it (1- adm, 2 student ...) and finally a user id field in its respective table. I would then do the query in the user table, and from the type of it, I would find out the table where it is and with the id found it.

In this way I'm doing two searches. have another way to do with just one?

Thank you!

    
asked by anonymous 19.10.2016 / 14:46

2 answers

1

In your User class, set the "usertype" attribute or something, and instantiate the object to define its type by looking for the values in an Enum. More or less like:

public enum TipoUsuario {
    ADMIN, ALUNO, SISTEMA
}

When you need to check the permission type, you can check the permission type with a switch case, for example:

public static void main(String[] args) {
    Usuario usuario = new Usuario();
    usuario.setNome("Banana");
    usuario.setTipoPermissao(TipoUsuario.ADMIN);

    switch (usuario.getTipoPermissao()) {
    case ADMIN:
        System.out.println("Usuario Administrador.");
        break;
    case ALUNO:
        System.out.println("Usuario Aluno.");
        break;
    case SISTEMA:
        System.out.println("Usuario Sistema.");
        break;
    default:
        System.out.println("Quem diabos é esse cara?");
        break;
    }
}

All this without changing table structure, when you want to change the type, just call the user.setTypePermission method and change to the type you want.

    
20.10.2016 / 15:48
2

You can perform a join between the tables at the time of the query:

select *
from Usuario usu
left join Administrador adm on  adm.id   = usu.id
                            and usu.tipo = 1
left join Aluno alu on  alu.id   = usu.id
                    and usu.tipo = 2
    
19.10.2016 / 14:51