Ajouter la génération et la lecture de la clé utilisateur (.papillon_id)
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.sql.*;
|
||||
|
||||
|
||||
public class RappelBD {
|
||||
|
||||
public static int ajouterPourUtilisateur(int utilisateurId, Rappel r) throws SQLException {
|
||||
|
||||
String sql = "INSERT INTO rappel(titre, contenu, theme, rang, utilisateur_id) VALUES (?, ?, ?, ?, ?)";
|
||||
Connection cnx = BaseDeDonnees.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.setInt(5, utilisateurId);
|
||||
pst.executeUpdate();
|
||||
|
||||
int id = 0;
|
||||
ResultSet rs = pst.getGeneratedKeys();
|
||||
|
||||
if (rs.next()) {
|
||||
id = rs.getInt(1);
|
||||
r.setId(id);
|
||||
}
|
||||
|
||||
rs.close();
|
||||
pst.close();
|
||||
cnx.close();
|
||||
return id;
|
||||
}
|
||||
|
||||
public static List<Rappel> listerParUtilisateur(int utilisateurId) throws SQLException {
|
||||
|
||||
String sql = "SELECT id, titre, contenu, theme, rang FROM rappel WHERE utilisateur_id=? ORDER BY rang ASC, id ASC";
|
||||
Connection cnx = BaseDeDonnees.getConnexion();
|
||||
PreparedStatement pst = cnx.prepareStatement(sql);
|
||||
pst.setInt(1, utilisateurId);
|
||||
ResultSet rs = pst.executeQuery();
|
||||
|
||||
List<Rappel> res = new ArrayList<Rappel>();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public static int modifierPourUtilisateur(int utilisateurId, int idRappel, Rappel r) throws SQLException {
|
||||
String sql = "UPDATE rappel SET titre=?, contenu=?, theme=?, rang=? WHERE id=? AND utilisateur_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, idRappel);
|
||||
pst.setInt(6, utilisateurId);
|
||||
|
||||
int rows = pst.executeUpdate();
|
||||
pst.close();
|
||||
cnx.close();
|
||||
return rows;
|
||||
}
|
||||
|
||||
public static int supprimerPourUtilisateur(int utilisateurId, int idRappel) throws SQLException {
|
||||
String sql = "DELETE FROM rappel WHERE id=? AND utilisateur_id=?";
|
||||
Connection cnx = BaseDeDonnees.getConnexion();
|
||||
PreparedStatement pst = cnx.prepareStatement(sql);
|
||||
pst.setInt(1, idRappel);
|
||||
pst.setInt(2, utilisateurId);
|
||||
|
||||
int rows = pst.executeUpdate();
|
||||
pst.close();
|
||||
cnx.close();
|
||||
return rows;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user