From a6a89ca83653e5dfc7af6c8ddf304f9ac954950c Mon Sep 17 00:00:00 2001 From: saaya-code Date: Tue, 26 Mar 2024 14:42:05 +0100 Subject: [PATCH] made updates to ui --- src/ExercicesTP/CRUD/DemandeDAO.java | 4 --- src/ExercicesTP/CRUD/EtudiantDAO.java | 36 +++++++++++++++++++ src/ExercicesTP/CRUD/FormationDAO.java | 3 ++ ...elDemande.java => TableModelEtudiant.java} | 18 +++++----- .../Helpers/TableModelFormation.java | 1 + src/ExercicesTP/IHM/IHMRechercheEtudiant.java | 23 ++++++++---- 6 files changed, 66 insertions(+), 19 deletions(-) delete mode 100644 src/ExercicesTP/CRUD/DemandeDAO.java rename src/ExercicesTP/Helpers/{TableModelDemande.java => TableModelEtudiant.java} (89%) diff --git a/src/ExercicesTP/CRUD/DemandeDAO.java b/src/ExercicesTP/CRUD/DemandeDAO.java deleted file mode 100644 index 7c4b342..0000000 --- a/src/ExercicesTP/CRUD/DemandeDAO.java +++ /dev/null @@ -1,4 +0,0 @@ -package ExercicesTP.CRUD; - -public class DemandeDAO { -} diff --git a/src/ExercicesTP/CRUD/EtudiantDAO.java b/src/ExercicesTP/CRUD/EtudiantDAO.java index 969934d..5309c50 100644 --- a/src/ExercicesTP/CRUD/EtudiantDAO.java +++ b/src/ExercicesTP/CRUD/EtudiantDAO.java @@ -112,4 +112,40 @@ private int getRowCount(ResultSet rs) { throw new RuntimeException(e); } } + + public Object[] fetchFormationData(int idFormation, int idEtudiant){ + PreparedStatement ps = null; + try { + ps = con.prepareStatement("SELECT f.titre, f.lieu, f.datef FROM formation f,Etudiant e,demandeetd d WHERE f.IdF=d.IdFormation and e.id=d.IdEtudiant and f.IdF = ? and e.id = ?"); + ps.setInt(1, idFormation); + ps.setInt(2, idEtudiant); + ResultSet rs = ps.executeQuery(); + if (rs.next()) { + return new Object[]{rs.getString(2), rs.getDate(3), rs.getString(4)}; + } + } catch (SQLException e) { + return null; + } + return null; + } + + public void insertDemande(int idFormation, int idEtudiant){ + PreparedStatement ps = null; + try { + ps = con.prepareStatement("INSERT INTO demandeetd (IdFormation, IdEtudiant) VALUES (?,?)"); + ps.setInt(1, idFormation); + ps.setInt(2, idEtudiant); + ps.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public ResultSet selection(String rq){ + try { + return st.executeQuery(rq); + } catch (SQLException e) { + return null; + } + } } diff --git a/src/ExercicesTP/CRUD/FormationDAO.java b/src/ExercicesTP/CRUD/FormationDAO.java index fd862f9..3067656 100644 --- a/src/ExercicesTP/CRUD/FormationDAO.java +++ b/src/ExercicesTP/CRUD/FormationDAO.java @@ -39,6 +39,9 @@ public void deleteFormation(int id) { } } + + + public ResultSet selection(String req){ try{ PreparedStatement ps = con.prepareStatement(req); diff --git a/src/ExercicesTP/Helpers/TableModelDemande.java b/src/ExercicesTP/Helpers/TableModelEtudiant.java similarity index 89% rename from src/ExercicesTP/Helpers/TableModelDemande.java rename to src/ExercicesTP/Helpers/TableModelEtudiant.java index e8249e4..68a6d7c 100644 --- a/src/ExercicesTP/Helpers/TableModelDemande.java +++ b/src/ExercicesTP/Helpers/TableModelEtudiant.java @@ -1,7 +1,7 @@ package ExercicesTP.Helpers; import ExercicesTP.CRUD.EtudiantDAO; - +import ExercicesTP.Etudiant; import javax.swing.*; import javax.swing.table.AbstractTableModel; @@ -11,12 +11,13 @@ import java.sql.SQLException; import java.util.ArrayList; -public class TableModelDemande extends AbstractTableModel { + +public class TableModelEtudiant extends AbstractTableModel { ArrayList data; ResultSetMetaData rsmd; EtudiantDAO dao; - public TableModelDemande(ResultSet rs, EtudiantDAO dao){ + public TableModelEtudiant(ResultSet rs, EtudiantDAO dao){ data = new ArrayList(); this.dao = dao; try { @@ -41,6 +42,7 @@ public TableModelDemande(ResultSet rs, EtudiantDAO dao){ } + public void updateTableWithNewResultSet(ResultSet rs){ data = new ArrayList(); try { @@ -112,9 +114,10 @@ public void setValueAt(Object aValue, int rowIndex, int columnIndex) { } - public void insertFormation(String nom, String lieu, Date date){ - this.dao.addEtudiant(new Formation(id, titre, dateF, lieu,certification)); - data.add(new Object[]{id, titre, dateF, lieu, certification}); + public void insertDemande(int idFormation, int idEtudiant){ + this.dao.insertDemande(idFormation, idEtudiant); + Object[] ligne = this.dao.fetchFormationData(idFormation, idEtudiant); + data.add(ligne); fireTableDataChanged(); JOptionPane.showMessageDialog(null, "done"); } @@ -127,8 +130,7 @@ public void supprimerFormation(int id){ JOptionPane.showMessageDialog(null, "annulé"); return; } - - this.dao.deleteFormation(id); + this.dao.deleteEtudiant(id); for(int i = 0; i < data.size(); i++){ if((int)data.get(i)[colmunNameToIndex("id")] == id){ data.remove(i); diff --git a/src/ExercicesTP/Helpers/TableModelFormation.java b/src/ExercicesTP/Helpers/TableModelFormation.java index ec1511f..e209fd7 100644 --- a/src/ExercicesTP/Helpers/TableModelFormation.java +++ b/src/ExercicesTP/Helpers/TableModelFormation.java @@ -1,5 +1,6 @@ package ExercicesTP.Helpers; +import ExercicesTP.CRUD.EtudiantDAO; import ExercicesTP.CRUD.FormationDAO; import ExercicesTP.Formation; diff --git a/src/ExercicesTP/IHM/IHMRechercheEtudiant.java b/src/ExercicesTP/IHM/IHMRechercheEtudiant.java index 56a8501..ec77d66 100644 --- a/src/ExercicesTP/IHM/IHMRechercheEtudiant.java +++ b/src/ExercicesTP/IHM/IHMRechercheEtudiant.java @@ -4,6 +4,7 @@ import java.awt.*; import ExercicesTP.CRUD.EtudiantDAO; +import ExercicesTP.Helpers.TableModelEtudiant; import ExercicesTP.Helpers.TableModelFormation; public class IHMRechercheEtudiant extends JInternalFrame { @@ -13,16 +14,17 @@ public class IHMRechercheEtudiant extends JInternalFrame { private JComboBox filiereComboBox,demandesBox; private JComboBox niveauComboBox, groupeComboBox; private JButton ajouterDemandeButton, supprimerDemandeButton, rechercheButton, cancelButton, modifierButton, supprimerButton; - public EtudiantDAO etudiantDAO; - public TableModelFormation model; + public EtudiantDAO dao; + public TableModelEtudiant model; JTable jt_Etudiant; public IHMRechercheEtudiant(EtudiantDAO dao) { - super("Recherche d'une formation"); + + super("Recherche d'un etudiant"); + this.dao = dao; initializeComponents(); createLayout(); addEventListeners(); - this.etudiantDAO = dao; this.setVisible(true); } @@ -55,7 +57,7 @@ private void initializeComponents() { listeDemandesLabel = new JLabel("Liste des demandes :"); jt_Etudiant = new JTable(); String rq = "SELECT titre,lieu,datef FROM FORMATION f,demandeetd d,ETUDIANT e WHERE (e.id=d.idEtudiant) and (f.idF = d.idFormation) ;"; - model = new TableModelFormation(etudiantDAO.selection(rq), etudiantDAO); + model = new TableModelEtudiant(dao.selection(rq), dao); jt_Etudiant.setModel(model); } @@ -90,7 +92,9 @@ private void createLayout() { addComponent(panel, demandesBox, c, 2,7,1, 1); addComponent(panel, listeDemandesLabel, c, 0, 10, 1, 1); - addComponent(panel, jt_Etudiant, c, 2, 10, 6, 1); + JScrollPane scrollPane = new JScrollPane(jt_Etudiant); + + addComponent(panel, scrollPane, c, 2, 10, 6, 1); // Add buttons getContentPane().add(panel); @@ -98,7 +102,7 @@ private void createLayout() { getContentPane().setLayout(new FlowLayout()); modifierButton.setEnabled(false); supprimerButton.setEnabled(false); - rechercheButton.setEnabled(false); + //rechercheButton.setEnabled(false); @@ -121,6 +125,11 @@ private void clearInputs() { private void addEventListeners() { //TODO implement + rechercheButton.addActionListener(e -> { + String rq = "SELECT titre,lieu,datef FROM FORMATION f,demandeetd d,ETUDIANT e WHERE (e.id=d.idEtudiant) and (f.idF = d.idFormation) ;"; + model.updateTableWithNewResultSet(dao.selection(rq)); + + }); }