Select from 2 Java tables

Asked

Viewed 45 times

0

Hello guys I am having problems to bring data from 2 different tables of HSQLDB database my DAO class this so:

@Override
public List<Animal> listar() throws Exception {
    List<Animal> lista = new ArrayList<>();
    try {
        statement = con.createStatement();
        String sql = "SELECT a.id,a.nome,a.nascimento,e.nome FROM animal a INNER JOIN especie e ON (a.especie_id = e.id)";
        rs = statement.executeQuery(sql);
        while (rs.next()) {
            int id = rs.getInt(1);
            String nome = rs.getString(2);
            Date nascimento = rs.getDate(3);
            String especie = rs.getString(4);
            Animal tempAnimal = new Animal(id,nome,nascimento);
            lista.add(tempAnimal);
            con.close();
        }
        return lista;
    }catch (Exception e){           
        con.close();
    }finally{
        con.close();
    }
    return null;
}

and my Animal Model class is like this:

private Especie especie;
private int id;
private String Nome;
private Date DataDeNascimento;



public Animal() {

}


public Animal(int id, String nome, Date dataDeNascimento, Especie especie) {
    super();
    this.especie = especie;
    this.id = id;
    this.Nome = nome;
    this.DataDeNascimento = dataDeNascimento;
}

model class Specie

private Tipo_animal tipoanimal;

private int id;
private String nome;
private String Descricao;
private List<Animal> animais;


public Especie() {


}

public Especie(Tipo_animal tipoanimal, int id, String nome, String descricao, List<Animal> animais) {
    super();
    this.tipoanimal = tipoanimal;
    this.id = id;
    this.nome = nome;
    Descricao = descricao;
    this.animais = animais;
}

database structure:

CREATE TABLE ANIMAL
(
ID             BIGINT IDENTITY NOT NULL ,
NOME           VARCHAR (50) NOT NULL ,
NASCIMENTO     DATE ,
ESPECIE_ID BIGINT NOT NULL ,
constraint ANIMAL_PK primary key (ID)
);

CREATE TABLE ESPECIE
(
ID                   BIGINT IDENTITY NOT NULL ,
NOME                 VARCHAR (50) NOT NULL ,
DESCRICAO            VARCHAR (100) ,
TIPO_ANIMAL_ACRONIMO CHAR (3) NOT NULL,
CONSTRAINT ESPECIE_PK PRIMARY KEY (ID)
);

ALTER TABLE ESPECIE ADD CONSTRAINT ESPECIE_NOME UNIQUE ( NOME ) ;

ALTER TABLE ANIMAL ADD CONSTRAINT ANIMAL_ESPECIE_FK FOREIGN KEY ( ESPECIE_ID ) REFERENCES ESPECIE ( ID ) ;

I need to get the name of the animal species and I’m not getting it, I can just bring the animal data like the id, name and date of birth, when I place species in my constructor method I can’t bring data from it along with animal data if anyone can help me would be grateful.

  • Felipe, if possible, add in textual form, not everyone can have access to images.

  • e.nome is not the name of the species? How is your class species?

  • I added the class especie

  • Does your specific class get a list of animals and Animal gets species? Your modeling is in serious trouble.

  • has how I solve, to be able to select the animals and see the name of the species?

  • So, you can’t quite understand the logic applied, because specie needs to receive a list of animals, and each animal gets a species?

  • I will try to explain what I need to do, I have 3 tables in my bank have Animal , Specie and Tipo_animal, each animal registered in the bank has its type ex: totó belongs to the breed dog, for me to add a new animal in the bank I need to tell which species it belongs to if not I can’t register. now I have registered animals in the bank via hsqldb and I need to pull them from the bank so that until now I have managed to pull only the data of the animal ex: id name and birth, I can’t pull the species to which it belongs, sera q I modeled wrong so it’s not working out right?

  • You want to receive an object Especie in the builder of Animal? If so, you will need to change all your query logic, since the only species information being returned is the name.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.