I have two classes. One is called Alumne and the other one is called Aula. In the class alumne i have an attribute called aulaAlumne (it’s type is Aula) which helps to connect these two classes.
These are the constructors of both classes:
Class Aula:
public class Aula {
int pis;
String porta;
int capacitat;
String SSID;
String passwdSSID;
public Aula(int pis, String porta, int capacitat, String SSID, String passwdSSID) {
this.pis = pis;
this.porta = porta;
this.capacitat = capacitat;
this.SSID = SSID;
this.passwdSSID = passwdSSID;
}
Class Alumne:
public class Alumne {
String dni;
String nom;
String email;
String telefon;
LocalDate dataNeixement;
Aula aulaAlumne;
public Alumne(String dni, String nom, String email, String telefon, LocalDate dataNeixement) {
this.dni = dni;
this.nom = nom;
this.email = email;
this.telefon = telefon;
this.dataNeixement = dataNeixement;
}
public Alumne(String dni, String nom, String email, String telefon, LocalDate dataNeixement, Aula aulaAlumne) {
this.dni = dni;
this.nom = nom;
this.email = email;
this.telefon = telefon;
this.dataNeixement = dataNeixement;
this.aulaAlumne = aulaAlumne;
}
I need to create an array list of both of them but when i try to create the Alumnes one i don’t know how to use the result set for the information from the alumne table in mysql.
I have two foreign keys in the table alumnes from the other table aules called aules_pis, aules_porta:
Table alumnes:
Table aules:
So i’m trying to get the alumnes information with this method but i don’t know how can i get the result set from the last two columns (foreign keys) because i need to put an Aula object in the constructor to create the Alumne object and finally create the arraylist of Alumne.
public ArrayList<Alumne> getAlumnes() throws AplicacioException {
ArrayList<Alumne> ret = new ArrayList<>();
try {
Statement sentencia;
sentencia = conn.getConnection().createStatement();
sentencia.executeQuery("SELECT * FROM alumnes");
ResultSet rs = sentencia.getResultSet();
while (rs.next()) {
ret.add(new Alumne(rs.getString("dniEstudiant"), rs.getString("nom"), rs.getString("email"), rs.getString("telefon"), rs.getDate("dataNeixement").toLocalDate()));
}
} catch (SQLException ex) {
throw new AplicacioException("Ha ocurrido un error al cargar los datos del alumno");
}
return ret;
}
I know it’s probably hard to understand what i’m trying to say but i tried to explain it the best i can.