diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..d8e9561 --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..cbbf455 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..69ace3f --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..46cd2fb --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml new file mode 100644 index 0000000..0aeb349 --- /dev/null +++ b/.idea/sqldialects.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/TP_Base.iml b/TP_Base.iml new file mode 100644 index 0000000..88ffcb6 --- /dev/null +++ b/TP_Base.iml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/libs/mysql-connector-java-5.0.7-bin.jar b/libs/mysql-connector-java-5.0.7-bin.jar new file mode 100644 index 0000000..6764101 Binary files /dev/null and b/libs/mysql-connector-java-5.0.7-bin.jar differ diff --git a/src/ExercicesTP/CRUD/FormationDAO.java b/src/ExercicesTP/CRUD/FormationDAO.java new file mode 100644 index 0000000..f468755 --- /dev/null +++ b/src/ExercicesTP/CRUD/FormationDAO.java @@ -0,0 +1,99 @@ +package ExercicesTP.CRUD; + +import ExercicesTP.Formation; +import ExercicesTP.Config; +import TP_Base.MyConnexion; +import ExercicesTP.interfaces.FormationDaoCRUD; +import java.sql.*; + +public class FormationDAO implements FormationDaoCRUD { + + Connection con = null; + Statement st = null; + public FormationDAO() { + con = MyConnexion.getConnection(Config.URL, Config.USERNAME, Config.PASSWORD); + try { + st = con.createStatement(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + public void addFormation(Formation formation) { + PreparedStatement ps = null; + try { + ps = con.prepareStatement("INSERT INTO formation (idF, titre, datef, lieu, certif) VALUES (?,?,?,?,?)"); + ps.setInt(1, formation.getId()); + ps.setString(2, formation.getTitle()); + ps.setDate(3, formation.getDateF()); + ps.setString(4, formation.getLieu()); + ps.setBoolean(5, formation.getCertification()); + ps.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public void deleteFormation(int id) { + try { + st.executeUpdate("DELETE FROM formation WHERE Id = " + id); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public void updateFormation(Formation formation) { + PreparedStatement ps = null; + try { + ps = con.prepareStatement("UPDATE formation SET titre = ?, datef = ?, lieu = ?, certification = ? WHERE Id = ?"); + ps.setString(1, formation.getTitle()); + ps.setDate(2, formation.getDateF()); + ps.setString(3, formation.getLieu()); + ps.setBoolean(4, formation.getCertification()); + ps.setInt(5, formation.getId()); + ps.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } + + public Formation getFormation(int id) { + try { + PreparedStatement ps = con.prepareStatement("SELECT * FROM formation WHERE IdF = ?"); + ps.setInt(1, id); + ResultSet rs = ps.executeQuery(); + if (rs.next()) { + return new Formation(rs.getInt(1), rs.getString(2), rs.getDate(4), rs.getString(3), rs.getBoolean(5)); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + return null; + } + + public Formation[] getAllFormations() { + try { + ResultSet rs = st.executeQuery("SELECT * FROM formation"); + Formation[] formations = new Formation[getRowCount(rs)]; + int i = 0; + while (rs.next()) { + formations[i] = new Formation(rs.getInt(1), rs.getString(2), rs.getDate(3), rs.getString(4), rs.getBoolean(5)); + i++; + } + return formations; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + private int getRowCount(ResultSet rs) { + try { + int rowCount = 0; + while(rs.next()) + rowCount++; + return rowCount; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/ExercicesTP/Config.java b/src/ExercicesTP/Config.java new file mode 100644 index 0000000..2dfb33b --- /dev/null +++ b/src/ExercicesTP/Config.java @@ -0,0 +1,7 @@ +package ExercicesTP; + +public class Config { + public static final String USERNAME = "root"; + public static final String PASSWORD = ""; + public static final String URL = "jdbc:mysql://127.0.0.1/GFormations"; +} diff --git a/src/ExercicesTP/Enseignant.java b/src/ExercicesTP/Enseignant.java new file mode 100644 index 0000000..b9b1cc9 --- /dev/null +++ b/src/ExercicesTP/Enseignant.java @@ -0,0 +1,27 @@ +package ExercicesTP; + +public class Enseignant extends Personne{ + String specialite; + String grade; + public Enseignant(int Id, String nom, String prenom, String specialite, String grade) { + super(Id, nom, prenom); + this.specialite = specialite; + this.grade = grade; + } + + public String getGrade() { + return grade; + } + + public void setGrade(String grade) { + this.grade = grade; + } + + public String getSpecialite() { + return specialite; + } + + public void setSpecialite(String specialite) { + this.specialite = specialite; + } +} diff --git a/src/ExercicesTP/Etudiant.java b/src/ExercicesTP/Etudiant.java new file mode 100644 index 0000000..4806e68 --- /dev/null +++ b/src/ExercicesTP/Etudiant.java @@ -0,0 +1,37 @@ +package ExercicesTP; + +public class Etudiant extends Personne{ + String filiere; + int niveau; + int groupe; + public Etudiant(int Id, String nom, String prenom, String filiere, int niveau, int groupe) { + super(Id, nom, prenom); + this.filiere = filiere; + this.niveau = niveau; + this.groupe = groupe; + } + + public String getFiliere() { + return filiere; + } + + public void setFiliere(String filiere) { + this.filiere = filiere; + } + + public int getNiveau() { + return niveau; + } + + public void setNiveau(int niveau) { + this.niveau = niveau; + } + + public int getGroupe() { + return groupe; + } + + public void setGroupe(int groupe) { + this.groupe = groupe; + } +} diff --git a/src/ExercicesTP/Exercice1.java b/src/ExercicesTP/Exercice1.java new file mode 100644 index 0000000..97e4410 --- /dev/null +++ b/src/ExercicesTP/Exercice1.java @@ -0,0 +1,33 @@ +package ExercicesTP; +import TP_Base.MyConnexion; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class Exercice1 { + public static void main(String[] args) { + Statement st = null; + Connection con = MyConnexion.getConnection(Config.URL, Config.USERNAME, Config.PASSWORD); + try { + st = con.createStatement(); + ResultSet res = st.executeQuery("SELECT * FROM ETUDIANT;"); + String line = "------------------------"; + System.out.println(line); + System.out.println("| Nom | Prenom | Moyenne |"); + System.out.println(line); + while (res.next()){ + System.out.println(" | "+res.getString(2) + " | " + res.getString(3) + " | " + res.getDouble(4)+" | "); + } + System.out.println(line); + + + } catch (SQLException e) { + throw new RuntimeException(e); + } + + + + } +} diff --git a/src/ExercicesTP/Exercice2.java b/src/ExercicesTP/Exercice2.java new file mode 100644 index 0000000..3fe21e7 --- /dev/null +++ b/src/ExercicesTP/Exercice2.java @@ -0,0 +1,4 @@ +package ExercicesTP; + +public class Exercice2 { +} diff --git a/src/ExercicesTP/Formation.java b/src/ExercicesTP/Formation.java new file mode 100644 index 0000000..0dee8f1 --- /dev/null +++ b/src/ExercicesTP/Formation.java @@ -0,0 +1,60 @@ +package ExercicesTP; + + +import java.sql.Date; + +public class Formation { + public int Id; + public String Title; + public Date DateF; + public String lieu; + public Boolean certification; + public Formation(int Id, String Title, Date DateF, String lieu, Boolean certification){ + this.Id = Id; + this.Title = Title; + this.DateF = DateF; + this.lieu = lieu; + this.certification = certification; + } + + public Boolean getCertification() { + return certification; + } + + public Date getDateF() { + return DateF; + } + + public int getId() { + return Id; + } + + public String getLieu() { + return lieu; + } + + public String getTitle() { + return Title; + } + + public void setCertification(Boolean certification) { + this.certification = certification; + } + + public void setDateF(Date dateF) { + DateF = dateF; + } + + public void setId(int id) { + Id = id; + } + + public void setTitle(String title) { + Title = title; + } + + public void setLieu(String lieu) { + this.lieu = lieu; + } + +} diff --git a/src/ExercicesTP/IHM/IHMAjoutFormation.java b/src/ExercicesTP/IHM/IHMAjoutFormation.java new file mode 100644 index 0000000..8717ae2 --- /dev/null +++ b/src/ExercicesTP/IHM/IHMAjoutFormation.java @@ -0,0 +1,120 @@ +package ExercicesTP.IHM; + +import javax.swing.*; +import java.awt.*; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; + +import ExercicesTP.CRUD.FormationDAO; +import ExercicesTP.Formation; + +public class IHMAjoutFormation extends JFrame { + + private JLabel referenceLabel, titleLabel, dateLabel, lieuLabel, certificationLabel; + private JTextField referenceField, titleField; + private JTextField dateTextField, lieuTextField; + private JCheckBox certificationCheckbox; + private JButton addButton, cancelButton; + public FormationDAO formationDAO; + public IHMAjoutFormation() { + super("Ajout d'une formation"); + initializeComponents(); + createLayout(); + addEventListeners(); + formationDAO = new FormationDAO(); + this.setVisible(true); + } + + private void initializeComponents() { + referenceLabel = new JLabel("Référence :"); + titleLabel = new JLabel("Titre :"); + dateLabel = new JLabel("Date :"); + lieuLabel = new JLabel("Lieu :"); + certificationLabel = new JLabel("Certification :"); + + referenceField = new JTextField(15); + titleField = new JTextField(20); + + // Sample data for comboboxes (replace with actual data source) + dateTextField = new JTextField(15); + lieuTextField = new JTextField(15); + + certificationCheckbox = new JCheckBox(); + + addButton = new JButton("Ajouter"); + cancelButton = new JButton("Annuler"); + } + + private void createLayout() { + JPanel panel = new JPanel(new GridBagLayout()); + GridBagConstraints c = new GridBagConstraints(); + + // Add labels and corresponding components + c.fill = GridBagConstraints.HORIZONTAL; + addComponent(panel, referenceLabel, c, 0, 0, 1, 1); + addComponent(panel, referenceField, c, 1, 0, 2, 1); + addComponent(panel, titleLabel, c, 0, 1, 1, 1); + addComponent(panel, titleField, c, 1, 1, 2, 1); + addComponent(panel, dateLabel, c, 0, 2, 1, 1); + addComponent(panel, dateTextField, c, 1, 2, 2, 1); + addComponent(panel, lieuLabel, c, 0, 3, 1, 1); + addComponent(panel, lieuTextField, c, 1, 3, 2, 1); + addComponent(panel, certificationLabel, c, 0, 4, 1, 1); + addComponent(panel, certificationCheckbox, c, 1, 4, 1, 1); + + // Add buttons + JPanel buttonPanel = new JPanel(); + buttonPanel.add(addButton); + buttonPanel.add(cancelButton); + c.fill = GridBagConstraints.NONE; + c.gridy = 5; + c.gridwidth = 3; + addComponent(panel, buttonPanel, c, 0, 5, 3, 1); + + getContentPane().add(panel); + setSize(500,250); + setLocationRelativeTo(null); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + } + + private void addComponent(JPanel panel, JComponent component, GridBagConstraints c, int x, int y, int width, int height) { + c.gridx = x; + c.gridy = y; + c.gridwidth = width; + c.gridheight = height; + panel.add(component, c); + } + + private void addEventListeners() { + addButton.addActionListener(e -> { + //change reference to int + + int reference = Integer.parseInt(referenceField.getText()); + String title = titleField.getText(); + + DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + String startDate = dateTextField.getText(); + + java.util.Date date = null; + try { + date = format.parse(startDate); + } catch (ParseException ex) { + throw new RuntimeException(ex); + } + java.sql.Date sqlStartDate = new java.sql.Date(date.getTime()); + String lieu = lieuTextField.getText(); + boolean certification = certificationCheckbox.isSelected(); + formationDAO.addFormation(new Formation(reference, title, sqlStartDate, lieu, certification)); + dispose(); + }); + + cancelButton.addActionListener(e -> dispose()); + } + + public static void main(String[] args) { + new IHMAjoutFormation(); + } + +} diff --git a/src/ExercicesTP/IHM/IHMRechercheFormation.java b/src/ExercicesTP/IHM/IHMRechercheFormation.java new file mode 100644 index 0000000..071ea67 --- /dev/null +++ b/src/ExercicesTP/IHM/IHMRechercheFormation.java @@ -0,0 +1,103 @@ +package ExercicesTP.IHM; + +import javax.swing.*; +import java.awt.*; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; + +import ExercicesTP.CRUD.FormationDAO; +import ExercicesTP.Formation; + +public class IHMRechercheFormation extends JFrame { + + private JLabel referenceLabel, titleLabel, dateLabel, lieuLabel, certificationLabel; + private JTextField referenceField, titleField; + private JTextField dateTextField, lieuTextField; + private JCheckBox certificationCheckbox; + private JButton rechercheButton, cancelButton, modifierButton, supprimerButton; + public FormationDAO formationDAO; + public IHMRechercheFormation() { + super("Ajout d'une formation"); + initializeComponents(); + createLayout(); + addEventListeners(); + formationDAO = new FormationDAO(); + this.setVisible(true); + } + + private void initializeComponents() { + referenceLabel = new JLabel("Référence :"); + titleLabel = new JLabel("Titre :"); + dateLabel = new JLabel("Date :"); + lieuLabel = new JLabel("Lieu :"); + certificationLabel = new JLabel("Certification :"); + + referenceField = new JTextField(15); + titleField = new JTextField(20); + + // Sample data for comboboxes (replace with actual data source) + dateTextField = new JTextField(15); + lieuTextField = new JTextField(15); + + certificationCheckbox = new JCheckBox(); + + rechercheButton = new JButton("Rechercher"); + modifierButton = new JButton("Modifier"); + supprimerButton = new JButton("Supprimer"); + cancelButton = new JButton("Annuler"); + } + + private void createLayout() { + JPanel panel = new JPanel(new GridBagLayout()); + GridBagConstraints c = new GridBagConstraints(); + + // Add labels and corresponding components + c.fill = GridBagConstraints.HORIZONTAL; + addComponent(panel, referenceLabel, c, 0, 0, 1, 1); + addComponent(panel, referenceField, c, 1, 0, 2, 1); + addComponent(panel, rechercheButton, c, 5, 1, 1, 1); + addComponent(panel, titleLabel, c, 0, 1, 1, 1); + addComponent(panel, titleField, c, 1, 1, 2, 1); + addComponent(panel, dateLabel, c, 0, 2, 1, 1); + addComponent(panel, dateTextField, c, 1, 2, 2, 1); + addComponent(panel, lieuLabel, c, 0, 3, 1, 1); + addComponent(panel, lieuTextField, c, 1, 3, 2, 1); + addComponent(panel, certificationLabel, c, 0, 4, 1, 1); + addComponent(panel, certificationCheckbox, c, 1, 4, 1, 1); + + + getContentPane().add(panel); + setSize(500,250); + setLocationRelativeTo(null); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + } + + private void addComponent(JPanel panel, JComponent component, GridBagConstraints c, int x, int y, int width, int height) { + c.gridx = x; + c.gridy = y; + c.gridwidth = width; + c.gridheight = height; + panel.add(component, c); + } + + private void addEventListeners() { + rechercheButton.addActionListener(e -> { + Formation formation = formationDAO.getFormation(Integer.parseInt(referenceField.getText())); + if (formation != null) { + titleField.setText(formation.getTitle()); + dateTextField.setText(formation.getDateF().toString()); + lieuTextField.setText(formation.getLieu()); + certificationCheckbox.setSelected(formation.getCertification()); + } else { + JOptionPane.showMessageDialog(this, "Formation non trouvée"); + } + + }); + + cancelButton.addActionListener(e -> dispose()); + } + + +} diff --git a/src/ExercicesTP/IHM/Main.java b/src/ExercicesTP/IHM/Main.java new file mode 100644 index 0000000..3d9e88c --- /dev/null +++ b/src/ExercicesTP/IHM/Main.java @@ -0,0 +1,7 @@ +package ExercicesTP.IHM; + +public class Main { + public static void main(String[] args) { + Principale principale = new Principale(); + } +} diff --git a/src/ExercicesTP/IHM/Principale.java b/src/ExercicesTP/IHM/Principale.java new file mode 100644 index 0000000..9789c85 --- /dev/null +++ b/src/ExercicesTP/IHM/Principale.java @@ -0,0 +1,99 @@ +package ExercicesTP.IHM; + +import TP_Base.Config; +import TP_Base.MyConnexion; + +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.sql.Connection; + + +public class Principale extends JFrame { + JMenuBar menuBar; + JMenu menuFormation; + JMenu menuEtudiant; + JMenu menuEnseignant; + + JMenuItem menuItemAjouteurEtudiant; + JMenuItem menuItemRechercherEtudiant; + JMenuItem menuItemAfficherEtudiant; + + JMenuItem menuItemAjouteurEnseignant; + JMenuItem menuItemRechercherEnseignant; + JMenuItem menuItemAfficherEnseignant; + + JMenuItem menuItemAjouteurFormation; + JMenuItem menuItemRechercherFormation; + JMenuItem menuItemAfficherFormation; + JDesktopPane desktop; + Connection con = null; + Principale() { + con = MyConnexion.getConnection(Config.URL, Config.USERNAME, Config.PASSWORD); + menuBar = new JMenuBar(); + this.setTitle("Gestion des formation"); + this.setSize(1000, 900); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + desktop = new JDesktopPane(); + this.add(desktop); + + menuFormation = new JMenu("Formation"); + menuEtudiant = new JMenu("Etudiant"); + menuEnseignant = new JMenu("Enseignant"); + + menuItemAjouteurFormation = new JMenuItem("Ajouteur"); + menuItemRechercherFormation = new JMenuItem("Rechercher"); + menuItemAfficherFormation = new JMenuItem("Afficher"); + + menuFormation.add(menuItemAjouteurFormation); + menuFormation.add(menuItemRechercherFormation); + menuFormation.add(menuItemAfficherFormation); + + menuItemAjouteurEtudiant = new JMenuItem("Ajouteur"); + menuItemRechercherEtudiant = new JMenuItem("Rechercher"); + menuItemAfficherEtudiant = new JMenuItem("Afficher"); + menuEtudiant.add(menuItemAjouteurEtudiant); + menuEtudiant.add(menuItemRechercherEtudiant); + menuEtudiant.add(menuItemAfficherEtudiant); + + menuItemAjouteurEnseignant = new JMenuItem("Ajouteur"); + menuItemRechercherEnseignant = new JMenuItem("Rechercher"); + menuItemAfficherEnseignant = new JMenuItem("Afficher"); + + menuEnseignant.add(menuItemAjouteurEnseignant); + menuEnseignant.add(menuItemRechercherEnseignant); + menuEnseignant.add(menuItemAfficherEnseignant); + + + menuBar.add(menuFormation); + menuBar.add(menuEtudiant); + menuBar.add(menuEnseignant); + this.setJMenuBar(menuBar); + this.setVisible(true); + + // listenners + menuItemAjouteurFormation.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + IHMAjoutFormation ihmAjout = new IHMAjoutFormation(); + desktop.add(ihmAjout); + } + }); + menuItemRechercherFormation.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + IHMRechercheFormation ihmRecherche = new IHMRechercheFormation(); + desktop.add(ihmRecherche); + } + }); + + + + + + + } + + +} \ No newline at end of file diff --git a/src/ExercicesTP/Personne.java b/src/ExercicesTP/Personne.java new file mode 100644 index 0000000..f66ddf9 --- /dev/null +++ b/src/ExercicesTP/Personne.java @@ -0,0 +1,37 @@ +package ExercicesTP; + +abstract public class Personne { + public int Id; + public String nom; + public String prenom; + + public Personne(int Id, String nom, String prenom) { + this.Id = Id; + this.nom = nom; + this.prenom = prenom; + } + + public int getId() { + return Id; + } + + public void setId(int id) { + Id = id; + } + + public String getNom() { + return nom; + } + + public void setNom(String nom) { + this.nom = nom; + } + + public String getPrenom() { + return prenom; + } + + public void setPrenom(String prenom) { + this.prenom = prenom; + } +} diff --git a/src/ExercicesTP/interfaces/EnseignantDaoCRUD.java b/src/ExercicesTP/interfaces/EnseignantDaoCRUD.java new file mode 100644 index 0000000..2920d18 --- /dev/null +++ b/src/ExercicesTP/interfaces/EnseignantDaoCRUD.java @@ -0,0 +1,4 @@ +package ExercicesTP.interfaces; + +public interface EnseignantDaoCRUD { +} diff --git a/src/ExercicesTP/interfaces/EtudiantDaoCRUD.java b/src/ExercicesTP/interfaces/EtudiantDaoCRUD.java new file mode 100644 index 0000000..1417110 --- /dev/null +++ b/src/ExercicesTP/interfaces/EtudiantDaoCRUD.java @@ -0,0 +1,11 @@ +package ExercicesTP.interfaces; + +import ExercicesTP.Etudiant; + +public interface EtudiantDaoCRUD { + public void addEtudiant(Etudiant etudiant); + public void deleteEtudiant(int id); + public void updateEtudiant(Etudiant etudiant); + public Etudiant getEtudiant(int id); + public Etudiant[] getAllEtudiants(); +} diff --git a/src/ExercicesTP/interfaces/FormationDaoCRUD.java b/src/ExercicesTP/interfaces/FormationDaoCRUD.java new file mode 100644 index 0000000..379ddd0 --- /dev/null +++ b/src/ExercicesTP/interfaces/FormationDaoCRUD.java @@ -0,0 +1,10 @@ +package ExercicesTP.interfaces; +import ExercicesTP.Formation; + +public interface FormationDaoCRUD { + public void addFormation(Formation formation); + public void deleteFormation(int id); + public void updateFormation(Formation formation); + public Formation getFormation(int id); + public Formation[] getAllFormations(); +} diff --git a/src/TP_Base/Config.java b/src/TP_Base/Config.java new file mode 100644 index 0000000..7642369 --- /dev/null +++ b/src/TP_Base/Config.java @@ -0,0 +1,7 @@ +package TP_Base; + +public class Config { + public static final String USERNAME = "root"; + public static final String PASSWORD = ""; + public static final String URL = "jdbc:mysql://127.0.0.1/db_tp_java"; +} diff --git a/src/TP_Base/DB_Main.java b/src/TP_Base/DB_Main.java new file mode 100644 index 0000000..4f95e29 --- /dev/null +++ b/src/TP_Base/DB_Main.java @@ -0,0 +1,17 @@ +package TP_Base; + + +public class DB_Main { + public static void main(String[] args) { + String url = "jdbc:mysql://127.0.0.1/db_tp_java"; + String username = "root"; + String password = ""; + //EtudiantDao dao = new EtudiantDao(url, username, password); + GestionEtudiant gestionEtudiant = new GestionEtudiant(); + + + //dao.afficheAll("SELECT * FROM ETUDIANT;"); + //dao.modfierEtudiant("cc","cc2",11212325, 16.00); + //dao.afficheAll("SELECT * FROM ETUDIANT;"); + } +} \ No newline at end of file diff --git a/src/TP_Base/EtudiantDao.java b/src/TP_Base/EtudiantDao.java new file mode 100644 index 0000000..ea07349 --- /dev/null +++ b/src/TP_Base/EtudiantDao.java @@ -0,0 +1,93 @@ +package TP_Base; + +import javax.xml.transform.Result; +import java.sql.*; + +public class EtudiantDao implements EtudiantDaoCRUD{ + + Connection con = null; + Statement st = null; + EtudiantDao(String url, String username, String password){ + this.con = MyConnexion.getConnection(url, username, password); + try { + st = con.createStatement(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + @Override + public int insertEtudiant(String nom, String prenom, int cin, double moyenne) { + String sql1 = "INSERT INTO ETUDIANT VALUES (?, ?, ?, ?)"; + PreparedStatement ps = null; + try { + ps = con.prepareStatement(sql1); + ps.setString(1, nom); + ps.setString(2, prenom); + ps.setInt(3, cin); + ps.setDouble(4, moyenne); + return ps.executeUpdate(); + } catch (SQLException e) { + System.out.println("erreur sql "+ e.getMessage()); + return 0; + } + + } + + @Override + public int supprimerEtudiant(int cin) { + String sql1 = "DELETE FROM ETUDIANT WHERE (cin=?)"; + PreparedStatement ps = null; + try { + ps = con.prepareStatement(sql1); + ps.setInt(1,cin); + return ps.executeUpdate(); + } catch (SQLException e) { + System.out.println("Error sql + "+ e.getMessage()); + return 0; + } + } + + @Override + public int modfierEtudiant(String nom, String prenom, int cin, double moyenne) { + String sql = "UPDATE ETUDIANT SET nom=?,prenom=?,moyenne=? WHERE cin=?;"; + PreparedStatement ps = null; + try { + ps = con.prepareStatement(sql); + ps.setString(1, nom); + ps.setString(2, prenom); + ps.setDouble(3, moyenne); + ps.setInt(4, cin); + return ps.executeUpdate(); + } catch (SQLException e) { + System.out.println("error sql : "+e.getMessage()); + return 0; + } + } + + ResultSet selection(String req){ + try{ + PreparedStatement ps = con.prepareStatement(req); + return ps.executeQuery(); + } catch (SQLException e) { + System.out.println("Erreur sql + "+ e.getMessage()); + return null; + } + } + void afficheResultSet(ResultSet rs){ + try { + while(rs.next()){ + String nom = rs.getString(1); + String prenom = rs.getString(2); + int cin = rs.getInt(3); + double moyenne = rs.getDouble(4); + System.out.println("Nom: "+nom+" Prénom: "+prenom+ " cin: "+cin+ " moyenne: "+ moyenne); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + @Override + public void afficheAll(String req) { + afficheResultSet(selection(req)); + } +} diff --git a/src/TP_Base/EtudiantDaoCRUD.java b/src/TP_Base/EtudiantDaoCRUD.java new file mode 100644 index 0000000..78fa010 --- /dev/null +++ b/src/TP_Base/EtudiantDaoCRUD.java @@ -0,0 +1,8 @@ +package TP_Base; + +public interface EtudiantDaoCRUD { + public int insertEtudiant(String nom, String prenom, int cin, double moyenne); + public int supprimerEtudiant(int cin); + public int modfierEtudiant(String nom, String prenom, int cin, double moyenne); + public void afficheAll(String req); +} diff --git a/src/TP_Base/GestionEtudiant.java b/src/TP_Base/GestionEtudiant.java new file mode 100644 index 0000000..64581b5 --- /dev/null +++ b/src/TP_Base/GestionEtudiant.java @@ -0,0 +1,99 @@ +package TP_Base; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.sql.ResultSet; + +import static java.lang.Integer.parseInt; + +public class GestionEtudiant extends JFrame { + JPanel northPannel; + JLabel nomField; + JLabel prenomField; + JLabel cinField; + JLabel moyenneField; + JTextField nom; + JTextField prenom; + JTextField cin; + JTextField moyenne; + JButton valider; + JTable jt_Etud; + MyTableModel model; + + GestionEtudiant(){ + this.setTitle("Gestion de l'etudiant"); + this.setSize(700,500); + this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + this.setResizable(true); + nomField = new JLabel("Nom"); + prenomField = new JLabel("Prenom"); + cinField = new JLabel("Cin"); + moyenneField = new JLabel("Moyenne"); + nom = new JTextField(5); + prenom = new JTextField(5); + cin = new JTextField(5); + moyenne = new JTextField(5); + northPannel = new JPanel(); + valider = new JButton("Valider"); + + + northPannel.add(nomField); + northPannel.add(nom); + northPannel.add(prenomField); + northPannel.add(prenom); + northPannel.add(cinField); + northPannel.add(cin); + northPannel.add(moyenneField); + northPannel.add(moyenne); + northPannel.add(valider); + northPannel.setBackground(Color.LIGHT_GRAY); + + + jt_Etud = new JTable(); + String rq = "SELECT * FROM ETUDIANT;"; + EtudiantDao dao = new EtudiantDao(Config.URL, Config.USERNAME, Config.PASSWORD); + ResultSet rs = dao.selection(rq); + model = new MyTableModel(rs, dao); + jt_Etud.setModel(model); + + + this.add(new JScrollPane(jt_Etud)); + this.add(northPannel, BorderLayout.NORTH); + this.setVisible(true); + + + + // ecouteur d'evts + + + valider.addActionListener((ae)->{ + String nomTextValue = nom.getText(); + String prenomTextValue = prenom.getText(); + int cinTextValue = Integer.parseInt(cin.getText()); + double moyenneTextValue = Double.parseDouble(moyenne.getText()); + model.insertEtudiant(nomTextValue,prenomTextValue, cinTextValue, moyenneTextValue); + + }); + + jt_Etud.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + if(e.getButton()==MouseEvent.BUTTON3){ + int row = jt_Etud.rowAtPoint(e.getPoint()); + //jt_Etud.setRowSelectionInterval(row, row); + JPopupMenu jpm = new JPopupMenu(); + JMenuItem supprimer = new JMenuItem("Supprimer"); + jpm.add(supprimer); + jpm.show(jt_Etud, e.getX(), e.getY()); + supprimer.addActionListener((ae)->{ + int cin = (int) model.getValueAt(row, 2); + model.supprimerEtudiant(cin); + }); + + } + } + }); + } +} diff --git a/src/TP_Base/MyConnexion.java b/src/TP_Base/MyConnexion.java new file mode 100644 index 0000000..18f56ca --- /dev/null +++ b/src/TP_Base/MyConnexion.java @@ -0,0 +1,31 @@ +package TP_Base; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +public class MyConnexion { + public static Connection getConnection(String url, String username, String password){ + String nomDriver = "com.mysql.jdbc.Driver"; + try { + Class.forName(nomDriver); + } catch (ClassNotFoundException e) { + System.out.println("Erreur connection : "+ e.getMessage()); + return null; + } + System.out.println("Driver Chargé"); + // connection a la base de donnée + + Connection con = null; + Statement st = null; + try{ + con = DriverManager.getConnection(url,username,password); + System.out.println("Conncted to db.."); + } catch (SQLException e) { + System.out.println("Erreur sql : "+ e.getMessage()); + return null; + } + return con; + } +} diff --git a/src/TP_Base/MyTableModel.java b/src/TP_Base/MyTableModel.java new file mode 100644 index 0000000..92da9eb --- /dev/null +++ b/src/TP_Base/MyTableModel.java @@ -0,0 +1,129 @@ +package TP_Base; + +import com.mysql.jdbc.log.NullLogger; + +import javax.swing.*; +import javax.swing.table.AbstractTableModel; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.util.ArrayList; + +public class MyTableModel extends AbstractTableModel { + ArrayList data; + ResultSetMetaData rsmd; + EtudiantDao dao; + MyTableModel(ResultSet rs, EtudiantDao dao){ + data = new ArrayList(); + this.dao = new EtudiantDao(Config.URL,Config.USERNAME,Config.PASSWORD); + try { + rsmd = rs.getMetaData(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + try { + while (rs.next()) { + Object[] ligne = new Object[rsmd.getColumnCount()]; + for(int i = 0;i 0) { + data.add(new Object[]{nom, prenom, cin, moyenne}); + fireTableDataChanged(); + JOptionPane.showMessageDialog(null, "done"); + + }else { + JOptionPane.showMessageDialog(null, "Not inserted.."); + } + } + public void supprimerEtudiant(int cin){ + int option = JOptionPane.showConfirmDialog(null, "est ce que vous etes sure ?"); + if(option == JOptionPane.NO_OPTION || option == JOptionPane.CANCEL_OPTION){ + JOptionPane.showMessageDialog(null, "annulé"); + return; + } + + if(this.dao.supprimerEtudiant(cin) > 0){ + for(int i = 0; i < data.size(); i++){ + if((int)data.get(i)[colmunNameToIndex("cin")] == cin){ + data.remove(i); + fireTableDataChanged(); + JOptionPane.showMessageDialog(null, "done"); + return; + } + } + }else { + JOptionPane.showMessageDialog(null, "Not deleted.."); + } + } +}