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.
– user28595
e.nome
is not the name of the species? How is your class species?– user28595
I added the class especie
– Felipe Portela
Does your specific class get a list of animals and Animal gets species? Your modeling is in serious trouble.
– user28595
has how I solve, to be able to select the animals and see the name of the species?
– Felipe Portela
So, you can’t quite understand the logic applied, because specie needs to receive a list of animals, and each animal gets a species?
– user28595
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?
– Felipe Portela
You want to receive an object
Especie
in the builder ofAnimal
? If so, you will need to change all your query logic, since the only species information being returned is the name.– Weslley Tavares