Update src/fr/iutfbleau/papillon/model/BaseDeDonnees.java

Ajout des fermeture des connexion
This commit is contained in:
2025-10-21 19:11:17 +02:00
parent 8361921964
commit 3bdd3e9431
@@ -4,82 +4,101 @@ import java.util.List;
public class BaseDeDonnees { public class BaseDeDonnees {
private static final String URL = "jdbc:mariadb://dwarves.iut-fbleau.fr:3306/yolou"; private static final String URL = "jdbc:mariadb://dwarves.iut-fbleau.fr:3306/yolou";
private static final String USER = "yolou"; private static final String USER = "yolou";
private static final String PASS = "serikhaneyolou"; private static final String PASS = "serikhaneyolou";
/** Connexion */ /** Connexion */
public static Connection connecter() throws SQLException { public static Connection getConnexion() throws SQLException {
try { try {
Class.forName("org.mariadb.jdbc.Driver"); Class.forName("org.mariadb.jdbc.Driver");
} catch (ClassNotFoundException ignore) {} } catch (ClassNotFoundException e) {
System.out.println("Erreur : pilote MariaDB non trouvé !");
}
return DriverManager.getConnection(URL, USER, PASS); return DriverManager.getConnection(URL, USER, PASS);
} }
/** INSERT renvoie l'id généré. */ /** Ajout dun rappel (INSERT) */
public static int ajouter(Rappel r) throws SQLException { public static int ajouter(Rappel r) throws SQLException {
String sql = "INSERT INTO rappel(titre, contenu, theme, rang) VALUES (?, ?, ?, ?)"; String sql = "INSERT INTO rappel(titre, contenu, theme, rang) VALUES (?, ?, ?, ?)";
try (Connection cnx = connecter(); Connection cnx = getConnexion();
PreparedStatement pst = cnx.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { PreparedStatement pst = cnx.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
pst.setString(1, r.getTitre()); pst.setString(1, r.getTitre());
pst.setString(2, r.getContenu()); pst.setString(2, r.getContenu());
pst.setString(3, r.getTheme()); pst.setString(3, r.getTheme());
pst.setInt(4, r.getRang()); pst.setInt(4, r.getRang());
pst.executeUpdate(); pst.executeUpdate();
try (ResultSet rs = pst.getGeneratedKeys()) { ResultSet rs = pst.getGeneratedKeys();
int id = 0;
if (rs.next()) { if (rs.next()) {
int id = rs.getInt(1); id = rs.getInt(1);
r.setId(id); r.setId(id);
return id;
}
}
return 0;
}
} }
/** SELECT * ORDER BY rang, id. */ rs.close();
pst.close();
cnx.close();
return id;
}
/** Liste des rappels */
public static List<Rappel> lister() throws SQLException { public static List<Rappel> lister() throws SQLException {
String sql = "SELECT id, titre, contenu, theme, rang FROM rappel ORDER BY rang ASC, id ASC"; String sql = "SELECT id, titre, contenu, theme, rang FROM rappel ORDER BY rang ASC, id ASC";
List<Rappel> res = new ArrayList<>(); List<Rappel> res = new ArrayList<>();
try (Connection cnx = connecter();
Connection cnx = getConnexion();
PreparedStatement pst = cnx.prepareStatement(sql); PreparedStatement pst = cnx.prepareStatement(sql);
ResultSet rs = pst.executeQuery()) { ResultSet rs = pst.executeQuery();
while (rs.next()) { while (rs.next()) {
res.add(new Rappel( Rappel r = new Rappel(
rs.getInt("id"), rs.getInt("id"),
rs.getString("titre"), rs.getString("titre"),
rs.getString("contenu"), rs.getString("contenu"),
rs.getString("theme"), rs.getString("theme"),
rs.getInt("rang"))); rs.getInt("rang")
} );
res.add(r);
} }
rs.close();
pst.close();
cnx.close();
return res; return res;
} }
/** UPDATE par id. */ /** Modification dun rappel (UPDATE) */
public static int modifier(int id, Rappel r) throws SQLException { public static int modifier(int id, Rappel r) throws SQLException {
String sql = "UPDATE rappel SET titre=?, contenu=?, theme=?, rang=? WHERE id=?"; String sql = "UPDATE rappel SET titre=?, contenu=?, theme=?, rang=? WHERE id=?";
try (Connection cnx = connecter(); Connection cnx = getConnexion();
PreparedStatement pst = cnx.prepareStatement(sql)) { PreparedStatement pst = cnx.prepareStatement(sql);
pst.setString(1, r.getTitre()); pst.setString(1, r.getTitre());
pst.setString(2, r.getContenu()); pst.setString(2, r.getContenu());
pst.setString(3, r.getTheme()); pst.setString(3, r.getTheme());
pst.setInt(4, r.getRang()); pst.setInt(4, r.getRang());
pst.setInt(5, id); pst.setInt(5, id);
return pst.executeUpdate();
} int rows = pst.executeUpdate();
pst.close();
cnx.close();
return rows;
} }
/** DELETE par id. */ /** Suppression dun rappel (DELETE) */
public static int supprimer(int id) throws SQLException { public static int supprimer(int id) throws SQLException {
String sql = "DELETE FROM rappel WHERE id=?"; String sql = "DELETE FROM rappel WHERE id=?";
try (Connection cnx = connecter(); Connection cnx = getConnexion();
PreparedStatement pst = cnx.prepareStatement(sql)) { PreparedStatement pst = cnx.prepareStatement(sql);
pst.setInt(1, id); pst.setInt(1, id);
return pst.executeUpdate(); int rows = pst.executeUpdate();
}
pst.close();
cnx.close();
return rows;
} }
} }