Ajout de l'authentification par un user (key)
This commit is contained in:
@@ -1,11 +1,35 @@
|
|||||||
|
package fr.iutfbleau.papillon;
|
||||||
|
|
||||||
|
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* La classe <code>BaseDeDonnees</code> fournit les paramètres et méthodes
|
||||||
|
* nécessaires à l’établissement d’une connexion à la base MariaDB.
|
||||||
|
*
|
||||||
|
* <p>Elle centralise les informations de connexion (URL, utilisateur et mot de passe)
|
||||||
|
* et renvoie un objet {@link java.sql.Connection} pour exécuter les requêtes SQL.</p>
|
||||||
|
*
|
||||||
|
* @version 1.0
|
||||||
|
* @author Seri-khane Yolou, Aylane SEHL, Jenson VAL
|
||||||
|
*/
|
||||||
public class BaseDeDonnees {
|
public class BaseDeDonnees {
|
||||||
|
|
||||||
private static final String URL = "jdbc:mariadb://dwarves.iut-fbleau.fr/val";
|
/** URL de connexion à la base de données MariaDB. */
|
||||||
private static final String USER = "val";
|
private static final String URL = "jdbc:mariadb://dwarves.iut-fbleau.fr/yolou";
|
||||||
private static final String PASS = "vali";
|
|
||||||
|
|
||||||
|
/** Nom d’utilisateur pour la base de données. */
|
||||||
|
private static final String USER = "yolou";
|
||||||
|
|
||||||
|
/** Mot de passe associé à l’utilisateur. */
|
||||||
|
private static final String PASS = "serikhaneyolou";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Établit une connexion avec la base de données MariaDB.
|
||||||
|
*
|
||||||
|
* @return un objet {@link java.sql.Connection} actif vers la base
|
||||||
|
* @throws SQLException si une erreur survient lors de la connexion
|
||||||
|
*/
|
||||||
public static java.sql.Connection getConnexion() throws SQLException {
|
public static java.sql.Connection getConnexion() throws SQLException {
|
||||||
try {
|
try {
|
||||||
Class.forName("org.mariadb.jdbc.Driver");
|
Class.forName("org.mariadb.jdbc.Driver");
|
||||||
|
|||||||
@@ -1,14 +1,44 @@
|
|||||||
|
/**
|
||||||
|
* La classe <code>Rappel</code> représente un rappel utilisateur,
|
||||||
|
* avec un titre, un contenu, un thème et un rang de priorité.
|
||||||
|
*
|
||||||
|
* @version 1.0
|
||||||
|
* @author Seri-khane Yolou, Aylane SEHL, Jenson VAL
|
||||||
|
*/
|
||||||
|
package fr.iutfbleau.papillon;
|
||||||
|
|
||||||
public class Rappel {
|
public class Rappel {
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
private String titre;
|
private String titre;
|
||||||
private String contenu;
|
private String contenu;
|
||||||
private String theme;
|
private String theme;
|
||||||
private int rang;
|
private int rang;
|
||||||
|
|
||||||
|
/** Constructeur vide (utilisé notamment pour la lecture SQL). */
|
||||||
|
public Rappel() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructeur d’un nouveau rappel sans identifiant.
|
||||||
|
*
|
||||||
|
* @param titre le titre du rappel
|
||||||
|
* @param contenu le contenu ou la description
|
||||||
|
* @param theme la catégorie ou le thème du rappel
|
||||||
|
* @param rang la priorité ou l’ordre d’affichage
|
||||||
|
*/
|
||||||
public Rappel(String titre, String contenu, String theme, int rang) {
|
public Rappel(String titre, String contenu, String theme, int rang) {
|
||||||
this(0, titre, contenu, theme, rang);
|
this(0, titre, contenu, theme, rang);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructeur complet avec identifiant.
|
||||||
|
*
|
||||||
|
* @param id l’identifiant du rappel
|
||||||
|
* @param titre le titre du rappel
|
||||||
|
* @param contenu le contenu
|
||||||
|
* @param theme le thème
|
||||||
|
* @param rang le rang de priorité
|
||||||
|
*/
|
||||||
public Rappel(int id, String titre, String contenu, String theme, int rang) {
|
public Rappel(int id, String titre, String contenu, String theme, int rang) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.titre = titre;
|
this.titre = titre;
|
||||||
@@ -17,51 +47,33 @@ public class Rappel {
|
|||||||
this.rang = rang;
|
this.rang = rang;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getId() {
|
/** @return l’identifiant du rappel */
|
||||||
return id;
|
public int getId() { return id; }
|
||||||
}
|
|
||||||
public void setId(int id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTitre() {
|
/** @param id définit l’identifiant du rappel */
|
||||||
return titre;
|
public void setId(int id) { this.id = id; }
|
||||||
}
|
|
||||||
public void setTitre(String titre) {
|
|
||||||
this.titre = titre;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getContenu() {
|
/** @return le titre du rappel */
|
||||||
return contenu;
|
public String getTitre() { return titre; }
|
||||||
}
|
|
||||||
public void setContenu(String contenu) {
|
|
||||||
this.contenu = contenu;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTheme() {
|
/** @param titre définit le titre du rappel */
|
||||||
return theme;
|
public void setTitre(String titre) { this.titre = titre; }
|
||||||
}
|
|
||||||
public void setTheme(String theme) {
|
|
||||||
this.theme = theme;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getRang() {
|
/** @return le contenu du rappel */
|
||||||
return rang;
|
public String getContenu() { return contenu; }
|
||||||
}
|
|
||||||
public void setRang(int rang) {
|
|
||||||
this.rang = rang;
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Override
|
/** @param contenu définit le contenu du rappel */
|
||||||
// public String toString() {
|
public void setContenu(String contenu) { this.contenu = contenu; }
|
||||||
// @Override
|
|
||||||
// public String toString() {
|
|
||||||
// String texte = "[" + id + "] " + titre + " | " + theme + " | rang=" + rang + " | ";
|
|
||||||
// if (contenu != null) {
|
|
||||||
// texte = texte + contenu;
|
|
||||||
// }
|
|
||||||
// return texte;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// }
|
/** @return le thème du rappel */
|
||||||
}
|
public String getTheme() { return theme; }
|
||||||
|
|
||||||
|
/** @param theme définit le thème du rappel */
|
||||||
|
public void setTheme(String theme) { this.theme = theme; }
|
||||||
|
|
||||||
|
/** @return le rang de priorité du rappel */
|
||||||
|
public int getRang() { return rang; }
|
||||||
|
|
||||||
|
/** @param rang définit le rang de priorité du rappel */
|
||||||
|
public void setRang(int rang) { this.rang = rang; }
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,49 +1,72 @@
|
|||||||
|
package fr.iutfbleau.papillon;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.sql.*;
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* La classe <code>RappelBD</code> gère les interactions entre le programme
|
||||||
|
* et la table <b>rappel</b> dans la base de données.
|
||||||
|
* <p>
|
||||||
|
* Elle permet d'ajouter, de modifier, de supprimer et de lister les rappels
|
||||||
|
* associés à un utilisateur spécifique à travers son identifiant (<code>utilisateur_id</code>).
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @version 1.0
|
||||||
|
* @author Seri-khane YOLOU, Aylane SEHL, Jenson VAL
|
||||||
|
*/
|
||||||
public class RappelBD {
|
public class RappelBD {
|
||||||
|
|
||||||
public static int ajouterPourUtilisateur(int utilisateurId, Rappel r) throws SQLException {
|
/**
|
||||||
|
* Ajoute un nouveau rappel dans la base de données pour un utilisateur donné.
|
||||||
String sql = "INSERT INTO rappel(titre, contenu, theme, rang, utilisateur_id) VALUES (?, ?, ?, ?, ?)";
|
*
|
||||||
|
* @param r l'objet {@link Rappel} à insérer dans la base
|
||||||
|
* @param utilisateurId l'identifiant de l'utilisateur propriétaire du rappel
|
||||||
|
* @return l'identifiant du rappel nouvellement inséré, ou -1 en cas d'erreur
|
||||||
|
* @throws SQLException si une erreur survient lors de la communication avec la base
|
||||||
|
*/
|
||||||
|
protected static int ajouter(Rappel r, int utilisateurId) throws SQLException {
|
||||||
Connection cnx = BaseDeDonnees.getConnexion();
|
Connection cnx = BaseDeDonnees.getConnexion();
|
||||||
PreparedStatement pst = cnx.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
String sql = "INSERT INTO rappel (utilisateur_id, titre, contenu, theme, rang) VALUES (?, ?, ?, ?, ?)";
|
||||||
|
|
||||||
pst.setString(1, r.getTitre());
|
PreparedStatement pst = cnx.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||||
pst.setString(2, r.getContenu());
|
pst.setInt(1, utilisateurId);
|
||||||
pst.setString(3, r.getTheme());
|
pst.setString(2, r.getTitre());
|
||||||
pst.setInt(4, r.getRang());
|
pst.setString(3, r.getContenu());
|
||||||
pst.setInt(5, utilisateurId);
|
pst.setString(4, r.getTheme());
|
||||||
|
pst.setInt(5, r.getRang());
|
||||||
pst.executeUpdate();
|
pst.executeUpdate();
|
||||||
|
|
||||||
int id = 0;
|
|
||||||
ResultSet rs = pst.getGeneratedKeys();
|
ResultSet rs = pst.getGeneratedKeys();
|
||||||
|
int id = -1;
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
id = rs.getInt(1);
|
id = rs.getInt(1);
|
||||||
r.setId(id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rs.close();
|
rs.close();
|
||||||
pst.close();
|
pst.close();
|
||||||
cnx.close();
|
cnx.close();
|
||||||
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Rappel> listerParUtilisateur(int utilisateurId) throws SQLException {
|
/**
|
||||||
|
* Récupère la liste complète des rappels appartenant à un utilisateur.
|
||||||
|
*
|
||||||
|
* @param utilisateurId l'identifiant de l'utilisateur
|
||||||
|
* @return une liste d'objets {@link Rappel} appartenant à cet utilisateur
|
||||||
|
* @throws SQLException si une erreur SQL survient lors de l'exécution
|
||||||
|
*/
|
||||||
|
protected 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";
|
||||||
|
List<Rappel> res = new ArrayList<>();
|
||||||
|
|
||||||
String sql = "SELECT id, titre, contenu, theme, rang FROM rappel WHERE utilisateur_id=? ORDER BY rang ASC, id ASC";
|
|
||||||
Connection cnx = BaseDeDonnees.getConnexion();
|
Connection cnx = BaseDeDonnees.getConnexion();
|
||||||
PreparedStatement pst = cnx.prepareStatement(sql);
|
PreparedStatement pst = cnx.prepareStatement(sql);
|
||||||
pst.setInt(1, utilisateurId);
|
pst.setInt(1, utilisateurId);
|
||||||
ResultSet rs = pst.executeQuery();
|
ResultSet rs = pst.executeQuery();
|
||||||
|
|
||||||
List<Rappel> res = new ArrayList<Rappel>();
|
|
||||||
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
|
|
||||||
Rappel r = new Rappel(
|
Rappel r = new Rappel(
|
||||||
rs.getInt("id"),
|
rs.getInt("id"),
|
||||||
rs.getString("titre"),
|
rs.getString("titre"),
|
||||||
@@ -51,44 +74,83 @@ public class RappelBD {
|
|||||||
rs.getString("theme"),
|
rs.getString("theme"),
|
||||||
rs.getInt("rang")
|
rs.getInt("rang")
|
||||||
);
|
);
|
||||||
|
|
||||||
res.add(r);
|
res.add(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
rs.close();
|
rs.close();
|
||||||
pst.close();
|
pst.close();
|
||||||
cnx.close();
|
cnx.close();
|
||||||
|
|
||||||
return res;
|
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=?";
|
* Modifie un rappel existant dans la base de données.
|
||||||
|
*
|
||||||
|
* @param id l'identifiant du rappel à modifier
|
||||||
|
* @param r l'objet {@link Rappel} contenant les nouvelles données
|
||||||
|
* @param utilisateurId l'identifiant de l'utilisateur propriétaire du rappel
|
||||||
|
* @return le nombre de lignes modifiées (0 si aucun rappel n'a été trouvé)
|
||||||
|
* @throws SQLException si une erreur SQL survient
|
||||||
|
*/
|
||||||
|
protected static int modifier(int id, Rappel r, int utilisateurId) throws SQLException {
|
||||||
Connection cnx = BaseDeDonnees.getConnexion();
|
Connection cnx = BaseDeDonnees.getConnexion();
|
||||||
|
String sql = "UPDATE rappel SET titre = ?, contenu = ?, theme = ?, rang = ? WHERE id = ? AND utilisateur_id = ?";
|
||||||
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, idRappel);
|
pst.setInt(5, id);
|
||||||
pst.setInt(6, utilisateurId);
|
pst.setInt(6, utilisateurId);
|
||||||
|
|
||||||
int rows = pst.executeUpdate();
|
int res = pst.executeUpdate();
|
||||||
|
|
||||||
pst.close();
|
pst.close();
|
||||||
cnx.close();
|
cnx.close();
|
||||||
return rows;
|
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int supprimerPourUtilisateur(int utilisateurId, int idRappel) throws SQLException {
|
/**
|
||||||
String sql = "DELETE FROM rappel WHERE id=? AND utilisateur_id=?";
|
* Supprime un rappel spécifique appartenant à un utilisateur.
|
||||||
|
*
|
||||||
|
* @param id l'identifiant du rappel à supprimer
|
||||||
|
* @param utilisateurId l'identifiant de l'utilisateur propriétaire
|
||||||
|
* @return le nombre de lignes supprimées (0 si aucun rappel correspondant)
|
||||||
|
* @throws SQLException si une erreur SQL survient
|
||||||
|
*/
|
||||||
|
protected static int supprimer(int id, int utilisateurId) throws SQLException {
|
||||||
Connection cnx = BaseDeDonnees.getConnexion();
|
Connection cnx = BaseDeDonnees.getConnexion();
|
||||||
|
String sql = "DELETE FROM rappel WHERE id = ? AND utilisateur_id = ?";
|
||||||
PreparedStatement pst = cnx.prepareStatement(sql);
|
PreparedStatement pst = cnx.prepareStatement(sql);
|
||||||
pst.setInt(1, idRappel);
|
pst.setInt(1, id);
|
||||||
pst.setInt(2, utilisateurId);
|
pst.setInt(2, utilisateurId);
|
||||||
|
|
||||||
int rows = pst.executeUpdate();
|
int res = pst.executeUpdate();
|
||||||
|
|
||||||
pst.close();
|
pst.close();
|
||||||
cnx.close();
|
cnx.close();
|
||||||
return rows;
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Supprime tous les rappels appartenant à un utilisateur spécifique.
|
||||||
|
*
|
||||||
|
* @param utilisateurId l'identifiant de l'utilisateur dont les rappels doivent être supprimés
|
||||||
|
* @return le nombre total de lignes supprimées
|
||||||
|
* @throws SQLException si une erreur SQL survient
|
||||||
|
*/
|
||||||
|
protected static int supprimerToutPourUtilisateur(int utilisateurId) throws SQLException {
|
||||||
|
Connection cnx = BaseDeDonnees.getConnexion();
|
||||||
|
String sql = "DELETE FROM rappel WHERE utilisateur_id = ?";
|
||||||
|
PreparedStatement pst = cnx.prepareStatement(sql);
|
||||||
|
pst.setInt(1, utilisateurId);
|
||||||
|
int lignes = pst.executeUpdate();
|
||||||
|
|
||||||
|
pst.close();
|
||||||
|
cnx.close();
|
||||||
|
return lignes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +1,52 @@
|
|||||||
|
/**
|
||||||
|
* La classe <code>Utilisateur</code> représente un utilisateur unique
|
||||||
|
* du logiciel Papillon, identifié par une clé locale.
|
||||||
|
*
|
||||||
|
* @version 1.0
|
||||||
|
* @author Seri-khane Yolou, Aylane SEHL, Jenson VAL
|
||||||
|
*/
|
||||||
|
package fr.iutfbleau.papillon;
|
||||||
|
|
||||||
public class Utilisateur {
|
public class Utilisateur {
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
private String nom;
|
private String cle;
|
||||||
private String cleUnique;
|
|
||||||
|
|
||||||
public Utilisateur(int id, String nom, String cleUnique) {
|
/**
|
||||||
|
* Constructeur complet d’un utilisateur.
|
||||||
|
*
|
||||||
|
* @param id l’identifiant de l’utilisateur
|
||||||
|
* @param cle la clé unique d’identification
|
||||||
|
*/
|
||||||
|
public Utilisateur(int id, String cle) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.nom = nom;
|
this.cle = cle;
|
||||||
this.cleUnique = cleUnique;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Utilisateur(String nom, String cleUnique) {
|
/**
|
||||||
this(0, nom, cleUnique);
|
* Constructeur d’un utilisateur à partir de sa clé uniquement.
|
||||||
|
*
|
||||||
|
* @param cle la clé unique d’identification
|
||||||
|
*/
|
||||||
|
public Utilisateur(String cle) {
|
||||||
|
this.cle = cle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return l’identifiant de l’utilisateur */
|
||||||
public int getId() { return id; }
|
public int getId() { return id; }
|
||||||
public String getNom() { return nom; }
|
|
||||||
public String getCleUnique() { return cleUnique; }
|
|
||||||
|
|
||||||
|
/** @param id définit l’identifiant de l’utilisateur */
|
||||||
public void setId(int id) { this.id = id; }
|
public void setId(int id) { this.id = id; }
|
||||||
public void setNom(String nom) { this.nom = nom; }
|
|
||||||
public void setCleUnique(String cleUnique) { this.cleUnique = cleUnique; }
|
|
||||||
|
|
||||||
|
/** @return la clé unique de l’utilisateur */
|
||||||
|
public String getCle() { return cle; }
|
||||||
|
|
||||||
|
/** @param cle définit la clé unique de l’utilisateur */
|
||||||
|
public void setCle(String cle) { this.cle = cle; }
|
||||||
|
|
||||||
|
/** @return une représentation textuelle de l’utilisateur */
|
||||||
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "[id=" + id + "] nom=" + nom + " cle=" + cleUnique;
|
return "Utilisateur [id=" + id + ", cle=" + cle + "]";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,51 +1,84 @@
|
|||||||
import java.sql.*;
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* La classe <code>UtilisateurBD</code> gère la table <b>utilisateur</b>
|
||||||
|
* de la base de données.
|
||||||
|
* <p>Elle permet de créer ou de récupérer un utilisateur à partir d’une clé locale,
|
||||||
|
* et de lire un utilisateur complet via sa clé.</p>
|
||||||
|
*
|
||||||
|
* @version 1.0
|
||||||
|
* @author Seri-khane Yolou, Aylane SEHL, Jenson VAL
|
||||||
|
*/
|
||||||
|
package fr.iutfbleau.papillon;
|
||||||
|
import java.sql.*;
|
||||||
public class UtilisateurBD {
|
public class UtilisateurBD {
|
||||||
|
|
||||||
public static int getUserIdByKey(String cle) throws SQLException {
|
/** Identifiant interne de l’utilisateur. */
|
||||||
|
private static int id;
|
||||||
|
|
||||||
String sql = "SELECT id FROM utilisateur WHERE cle_unique = ?";
|
/**
|
||||||
|
* Récupère ou crée un utilisateur en fonction de sa clé unique.
|
||||||
|
*
|
||||||
|
* @param cle la clé unique associée à l’utilisateur
|
||||||
|
* @return l’identifiant de l’utilisateur correspondant
|
||||||
|
* @throws SQLException si une erreur SQL survient
|
||||||
|
*/
|
||||||
|
public static int getOrCreateIdByKey(String cle) throws SQLException {
|
||||||
Connection cnx = BaseDeDonnees.getConnexion();
|
Connection cnx = BaseDeDonnees.getConnexion();
|
||||||
|
|
||||||
|
// Vérifie si un utilisateur existe déjà avec cette clé
|
||||||
|
String sqlSelect = "SELECT id FROM utilisateur WHERE cle = ?";
|
||||||
|
PreparedStatement pstSelect = cnx.prepareStatement(sqlSelect);
|
||||||
|
pstSelect.setString(1, cle);
|
||||||
|
ResultSet rs = pstSelect.executeQuery();
|
||||||
|
|
||||||
|
if (rs.next()) {
|
||||||
|
id = rs.getInt("id");
|
||||||
|
} else {
|
||||||
|
// Sinon on en crée un nouveau
|
||||||
|
String sqlInsert = "INSERT INTO utilisateur (cle) VALUES (?)";
|
||||||
|
PreparedStatement pstInsert = cnx.prepareStatement(sqlInsert, Statement.RETURN_GENERATED_KEYS);
|
||||||
|
pstInsert.setString(1, cle);
|
||||||
|
pstInsert.executeUpdate();
|
||||||
|
|
||||||
|
ResultSet keys = pstInsert.getGeneratedKeys();
|
||||||
|
if (keys.next()) {
|
||||||
|
id = keys.getInt(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
keys.close();
|
||||||
|
pstInsert.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
rs.close();
|
||||||
|
pstSelect.close();
|
||||||
|
cnx.close();
|
||||||
|
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Récupère un utilisateur complet à partir de sa clé.
|
||||||
|
*
|
||||||
|
* @param cle la clé unique de l’utilisateur
|
||||||
|
* @return un objet {@link Utilisateur} si trouvé, sinon <code>null</code>
|
||||||
|
* @throws SQLException si une erreur SQL survient
|
||||||
|
*/
|
||||||
|
public static Utilisateur findByKey(String cle) throws SQLException {
|
||||||
|
Connection cnx = BaseDeDonnees.getConnexion();
|
||||||
|
String sql = "SELECT id, cle FROM utilisateur WHERE cle = ?";
|
||||||
PreparedStatement pst = cnx.prepareStatement(sql);
|
PreparedStatement pst = cnx.prepareStatement(sql);
|
||||||
pst.setString(1, cle);
|
pst.setString(1, cle);
|
||||||
ResultSet rs = pst.executeQuery();
|
ResultSet rs = pst.executeQuery();
|
||||||
|
|
||||||
int id = -1;
|
Utilisateur u = null;
|
||||||
|
if (rs.next()) {
|
||||||
if (rs.next()){
|
u = new Utilisateur(rs.getInt("id"), rs.getString("cle"));
|
||||||
id = rs.getInt("id");
|
|
||||||
}
|
|
||||||
rs.close();
|
|
||||||
pst.close();
|
|
||||||
cnx.close();
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int creerUtilisateur(String nom, String cle) throws SQLException {
|
|
||||||
String sql = "INSERT INTO utilisateur(nom, cle_unique) VALUES (?, ?)";
|
|
||||||
Connection cnx = BaseDeDonnees.getConnexion();
|
|
||||||
PreparedStatement pst = cnx.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
|
||||||
pst.setString(1, nom);
|
|
||||||
pst.setString(2, cle);
|
|
||||||
pst.executeUpdate();
|
|
||||||
|
|
||||||
int id = -1;
|
|
||||||
ResultSet rs = pst.getGeneratedKeys();
|
|
||||||
if (rs.next()){
|
|
||||||
id = rs.getInt(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rs.close();
|
rs.close();
|
||||||
pst.close();
|
pst.close();
|
||||||
cnx.close();
|
cnx.close();
|
||||||
return id;
|
return u;
|
||||||
}
|
|
||||||
|
|
||||||
public static int getOrCreateUserId(String cle, String nomParDefaut) throws SQLException {
|
|
||||||
int id = getUserIdByKey(cle);
|
|
||||||
if (id != -1){
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
return creerUtilisateur(nomParDefaut, cle);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user