diff --git a/lib/mariadb-java-client-3.5.6.jar b/lib/mariadb-java-client-3.5.6.jar new file mode 100644 index 0000000..447793c Binary files /dev/null and b/lib/mariadb-java-client-3.5.6.jar differ diff --git a/src/fr/iutfbleau/papillon/controller/GestionRappel.java b/src/fr/iutfbleau/papillon/controller/GestionRappel.java new file mode 100644 index 0000000..73970d9 --- /dev/null +++ b/src/fr/iutfbleau/papillon/controller/GestionRappel.java @@ -0,0 +1,59 @@ +package fr.iutfbleau.papillon.controller; + +import fr.iutfbleau.papillon.model.BaseDeDonnees; +import fr.iutfbleau.papillon.model.Rappel; + +import java.sql.*; +import java.util.ArrayList; +import java.util.List; + +public class GestionRappel { + + public void ajouter(Rappel r) throws SQLException { + String sql = "INSERT INTO rappel (titre, contenu, theme, rang) VALUES (?, ?, ?, ?)"; + Connection cnx = BaseDeDonnees.getConnexion(); + PreparedStatement pst = cnx.prepareStatement(sql); + pst.setString(1, r.getTitre()); + pst.setString(2, r.getContenu()); + pst.setString(3, r.getTheme()); + pst.setInt(4, r.getRang()); + pst.executeUpdate(); + pst.close(); + } + + public List lister() throws SQLException { + String sql = "SELECT id, titre, theme, rang FROM rappel ORDER BY id DESC"; + List res = new ArrayList<>(); + Connection cnx = BaseDeDonnees.getConnexion(); + PreparedStatement pst = cnx.prepareStatement(sql); + ResultSet rs = pst.executeQuery(); + while (rs.next()) { + res.add(rs.getInt("id") + " | " + rs.getString("titre") + " | " + rs.getString("theme") + " | " + rs.getInt("rang")); + } + rs.close(); + pst.close(); + return res; + } + + public void supprimerParId(int id) throws SQLException { + String sql = "DELETE FROM rappel WHERE id = ?"; + Connection cnx = BaseDeDonnees.getConnexion(); + PreparedStatement pst = cnx.prepareStatement(sql); + pst.setInt(1, id); + pst.executeUpdate(); + pst.close(); + } + + public void modifierParId(int id, Rappel r) throws SQLException { + String sql = "UPDATE rappel SET titre = ?, contenu = ?, theme = ?, rang = ? WHERE id = ?"; + Connection cnx = BaseDeDonnees.getConnexion(); + PreparedStatement pst = cnx.prepareStatement(sql); + pst.setString(1, r.getTitre()); + pst.setString(2, r.getContenu()); + pst.setString(3, r.getTheme()); + pst.setInt(4, r.getRang()); + pst.setInt(5, id); + pst.executeUpdate(); + pst.close(); + } +} diff --git a/src/fr/iutfbleau/papillon/model/BaseDeDonnees.java b/src/fr/iutfbleau/papillon/model/BaseDeDonnees.java new file mode 100644 index 0000000..ddcfdad --- /dev/null +++ b/src/fr/iutfbleau/papillon/model/BaseDeDonnees.java @@ -0,0 +1,38 @@ +package fr.iutfbleau.papillon.model; +import org.mariadb.jdbc.MariaDbDataSource; + +import java.sql.Connection; +import java.sql.SQLException; + +public class BaseDeDonnees { + + private static Connection connexion; + + public static Connection getConnexion() { + if (connexion == null) { + try { + MariaDbDataSource dataSource = new MariaDbDataSource(); + dataSource.setUrl("jdbc:mariadb://localhost:3307/papillon"); + dataSource.setUser("root"); + dataSource.setPassword("mdp"); + + connexion = dataSource.getConnection(); + System.out.println(" Connexion réussie !"); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return connexion; + } + + public static void fermer() { + try { + if (connexion != null && !connexion.isClosed()) { + connexion.close(); + System.out.println(" Connexion fermée."); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/fr/iutfbleau/papillon/model/Rappel.java b/src/fr/iutfbleau/papillon/model/Rappel.java new file mode 100644 index 0000000..4efb062 --- /dev/null +++ b/src/fr/iutfbleau/papillon/model/Rappel.java @@ -0,0 +1,35 @@ +package fr.iutfbleau.papillon.model; + +public class Rappel { + private int id; + private String titre; + private String contenu; + private LocalDateTime date; + + public Rappel(int id, String titre, String contenu, LocalDateTime date) { + this.id = id; + this.titre = titre; + this.contenu = contenu; + this.date = date; + } + public Rappel(String titre, String contenu, String theme, int rang){ + this(0,titre,contenu,theme,rang); + } + + public int getId(){ + return id; + } + public String getTitre(){ + return titre; + } + public String getContenu(){ + return contenu; + } + public String getTheme(){ + return theme; + } + public int getRang(){ + return rang; + } + +}