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
@@ -0,0 +1,104 @@
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;
}
}