This commit is contained in:
2025-10-22 23:18:10 +02:00
parent 1caa8d9f92
commit ab04a8daae
4 changed files with 60 additions and 119 deletions
@@ -1,104 +0,0 @@
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class BaseDeDonnees {
private static final String URL = "jdbc:mariadb://dwarves.iut-fbleau.fr:3306/yolou";
private static final String USER = "yolou";
private static final String PASS = "serikhaneyolou";
/** Connexion */
public static Connection getConnexion() throws SQLException {
try {
Class.forName("org.mariadb.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Erreur : pilote MariaDB non trouvé !");
}
return DriverManager.getConnection(URL, USER, PASS);
}
/** Ajout dun rappel (INSERT) */
public static int ajouter(Rappel r) throws SQLException {
String sql = "INSERT INTO rappel(titre, contenu, theme, rang) VALUES (?, ?, ?, ?)";
Connection cnx = getConnexion();
PreparedStatement pst = cnx.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
pst.setString(1, r.getTitre());
pst.setString(2, r.getContenu());
pst.setString(3, r.getTheme());
pst.setInt(4, r.getRang());
pst.executeUpdate();
ResultSet rs = pst.getGeneratedKeys();
int id = 0;
if (rs.next()) {
id = rs.getInt(1);
r.setId(id);
}
rs.close();
pst.close();
cnx.close();
return id;
}
/** Liste des rappels */
public static List<Rappel> lister() throws SQLException {
String sql = "SELECT id, titre, contenu, theme, rang FROM rappel ORDER BY rang ASC, id ASC";
List<Rappel> res = new ArrayList<>();
Connection cnx = getConnexion();
PreparedStatement pst = cnx.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
Rappel r = new Rappel(
rs.getInt("id"),
rs.getString("titre"),
rs.getString("contenu"),
rs.getString("theme"),
rs.getInt("rang")
);
res.add(r);
}
rs.close();
pst.close();
cnx.close();
return res;
}
/** Modification dun rappel (UPDATE) */
public static int modifier(int id, Rappel r) throws SQLException {
String sql = "UPDATE rappel SET titre=?, contenu=?, theme=?, rang=? WHERE id=?";
Connection cnx = 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);
int rows = pst.executeUpdate();
pst.close();
cnx.close();
return rows;
}
/** Suppression dun rappel (DELETE) */
public static int supprimer(int id) throws SQLException {
String sql = "DELETE FROM rappel WHERE id=?";
Connection cnx = getConnexion();
PreparedStatement pst = cnx.prepareStatement(sql);
pst.setInt(1, id);
int rows = pst.executeUpdate();
pst.close();
cnx.close();
return rows;
}
}
@@ -1,60 +0,0 @@
import java.util.List;
public class GestionRappel {
public List<Rappel> lister() throws Exception {
return BaseDeDonnees.lister();
}
public int ajouter(Rappel r) throws Exception {
return BaseDeDonnees.ajouter(r);
}
public int modifierParId(int id, Rappel r) throws Exception {
return BaseDeDonnees.modifier(id, r);
}
public int supprimerParId(int id) throws Exception {
return BaseDeDonnees.supprimer(id);
}
// === petit main pour test rapide ===
public static void main(String[] args) {
try {
GestionRappel g = new GestionRappel();
// Ajouter
Rappel r = new Rappel("Acheter du café", "avant 17h", "Urgent", 1);
int id = g.ajouter(r);
System.out.println("Ajouté : " + id);
// Lister
System.out.println("\nListe des rappels :");
for (Rappel x : g.lister()) {
System.out.println(" - " + x);
}
// Modifier
r.setTitre("Acheter du café (bio)");
r.setRang(3);
g.modifierParId(r.getId(), r);
System.out.println("\nAprès modification :");
for (Rappel x : g.lister()) {
System.out.println(" - " + x);
}
// Supprimer
// g.supprimerParId(r.getId());
// System.out.println("\nAprès suppression :");
// for (Rappel x : g.lister()) {
// System.out.println(" - " + x);
// }
} catch (Exception e) {
e.printStackTrace();
}
}
}
@@ -1,67 +0,0 @@
public class Rappel {
private int id;
private String titre;
private String contenu;
private String theme;
private int rang;
public Rappel(String titre, String contenu, String theme, int rang) {
this(0, titre, contenu, theme, rang);
}
public Rappel(int id, String titre, String contenu, String theme, int rang) {
this.id = id;
this.titre = titre;
this.contenu = contenu;
this.theme = theme;
this.rang = rang;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitre() {
return titre;
}
public void setTitre(String titre) {
this.titre = titre;
}
public String getContenu() {
return contenu;
}
public void setContenu(String contenu) {
this.contenu = contenu;
}
public String getTheme() {
return theme;
}
public void setTheme(String theme) {
this.theme = theme;
}
public int getRang() {
return rang;
}
public void setRang(int rang) {
this.rang = rang;
}
@Override
public String toString() {
@Override
public String toString() {
String texte = "[" + id + "] " + titre + " | " + theme + " | rang=" + rang + " | ";
if (contenu != null) {
texte = texte + contenu;
}
return texte;
}
}
}