I’ve prepared a method to print the content of a table named “etudiants” which has a foreing key “filiere” into another table “filieres”, and my code was :

public ArrayList<Etudiant> afficher() {

    ArrayList list = new ArrayList<Etudiant>();
    con = Connexion.getCon();

    String select = "SELECT * FROM etudiants";
    try {
        st = con.prepareStatement(select);;
        rs = st.executeQuery();
        
        String select2 = "";
        PreparedStatement stat = null;
        ResultSet rslt = null ;
        
        while (rs.next()) {
            Etudiant etu = new Etudiant();
            etu.setIdE(rs.getInt("id"));
            etu.setNomE(rs.getString("nom"));
            etu.setPrenomE(rs.getString("prenom"));
            etu.setDateNE(rs.getDate("date_naissance"));
            etu.setSexeE(rs.getString("sexe"));
            etu.setNiveauE(rs.getInt("niveau"));
            
            select2 = "select nom from filieres where id=?";
            
            //on prepare une autre statment pour selectionner de la table filieres le nom 
            //correspondant à l'id selectionné dans le table etudiants
            stat = con.prepareStatement(select2);
            stat.setInt(1, rs.getInt("filiere"));
            rslt = stat.executeQuery(select2);
            etu.setNomFiliereE(rslt.getString("nom"));


            list.add(etu);
        }
    } catch (SQLException ex) {
        Logger.getLogger(GestionEtudiant.class.getName()).log(Level.SEVERE, null, ex);
    }

    return list;
}

it seems that SQL statements are right, however it gives me this error :

java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual tha

By admin