diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/ExercicesTP/CRUD/FormationDAO.java b/src/ExercicesTP/CRUD/FormationDAO.java index f468755..7a51306 100644 --- a/src/ExercicesTP/CRUD/FormationDAO.java +++ b/src/ExercicesTP/CRUD/FormationDAO.java @@ -35,7 +35,7 @@ public void addFormation(Formation formation) { public void deleteFormation(int id) { try { - st.executeUpdate("DELETE FROM formation WHERE Id = " + id); + st.executeUpdate("DELETE FROM formation WHERE IdF = " + id); } catch (SQLException e) { throw new RuntimeException(e); } @@ -44,7 +44,7 @@ public void deleteFormation(int id) { public void updateFormation(Formation formation) { PreparedStatement ps = null; try { - ps = con.prepareStatement("UPDATE formation SET titre = ?, datef = ?, lieu = ?, certification = ? WHERE Id = ?"); + ps = con.prepareStatement("UPDATE formation SET titre = ?, datef = ?, lieu = ?, certif = ? WHERE IdF = ?"); ps.setString(1, formation.getTitle()); ps.setDate(2, formation.getDateF()); ps.setString(3, formation.getLieu()); diff --git a/src/ExercicesTP/Exercice2.java b/src/ExercicesTP/Exercice2.java deleted file mode 100644 index 3fe21e7..0000000 --- a/src/ExercicesTP/Exercice2.java +++ /dev/null @@ -1,4 +0,0 @@ -package ExercicesTP; - -public class Exercice2 { -} diff --git a/src/ExercicesTP/Formation.java b/src/ExercicesTP/Formation.java index 0dee8f1..fe2b8f3 100644 --- a/src/ExercicesTP/Formation.java +++ b/src/ExercicesTP/Formation.java @@ -2,6 +2,8 @@ import java.sql.Date; +import java.text.ParseException; +import java.text.SimpleDateFormat; public class Formation { public int Id; @@ -57,4 +59,13 @@ public void setLieu(String lieu) { this.lieu = lieu; } + public static java.sql.Date parseDateToSqlDate(String date) { + java.util.Date dateUtil = null; + try { + dateUtil = new SimpleDateFormat("yyyy-MM-dd").parse(date); + } catch (ParseException e) { + throw new RuntimeException(e); + } + return new java.sql.Date(dateUtil.getTime()); + } } diff --git a/src/ExercicesTP/IHM/IHMRechercheFormation.java b/src/ExercicesTP/IHM/IHMRechercheFormation.java index 071ea67..0b82b16 100644 --- a/src/ExercicesTP/IHM/IHMRechercheFormation.java +++ b/src/ExercicesTP/IHM/IHMRechercheFormation.java @@ -51,12 +51,12 @@ private void initializeComponents() { private void createLayout() { JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); + JPanel panel2 = new JPanel(new GridBagLayout()); // 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); @@ -65,9 +65,16 @@ private void createLayout() { addComponent(panel, lieuTextField, c, 1, 3, 2, 1); addComponent(panel, certificationLabel, c, 0, 4, 1, 1); addComponent(panel, certificationCheckbox, c, 1, 4, 1, 1); + addComponent(panel2, rechercheButton, c, 5, 1, 1, 1); + addComponent(panel2, modifierButton, c, 5, 2, 1, 1); + addComponent(panel2, supprimerButton, c, 5, 3, 1, 1); + addComponent(panel2, cancelButton, c, 5, 4, 1, 1); - + // Add buttons getContentPane().add(panel); + getContentPane().add(panel2); + getContentPane().setLayout(new FlowLayout()); + setSize(500,250); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -82,6 +89,13 @@ private void addComponent(JPanel panel, JComponent component, GridBagConstraints panel.add(component, c); } + private void clearInputs() { + referenceField.setText(""); + titleField.setText(""); + dateTextField.setText(""); + lieuTextField.setText(""); + certificationCheckbox.setSelected(false); + } private void addEventListeners() { rechercheButton.addActionListener(e -> { Formation formation = formationDAO.getFormation(Integer.parseInt(referenceField.getText())); @@ -91,12 +105,26 @@ private void addEventListeners() { lieuTextField.setText(formation.getLieu()); certificationCheckbox.setSelected(formation.getCertification()); } else { + clearInputs(); JOptionPane.showMessageDialog(this, "Formation non trouvée"); } }); cancelButton.addActionListener(e -> dispose()); + + modifierButton.addActionListener(e -> { + Formation formation = new Formation(Integer.parseInt(referenceField.getText()), titleField.getText(), Formation.parseDateToSqlDate(dateTextField.getText()), lieuTextField.getText(), certificationCheckbox.isSelected()); + formationDAO.updateFormation(formation); + JOptionPane.showMessageDialog(this, "Formation modifiée"); + }); + supprimerButton.addActionListener(e -> { + formationDAO.deleteFormation(Integer.parseInt(referenceField.getText())); + JOptionPane.showMessageDialog(this, "Formation supprimée"); + clearInputs(); + + }); + }