MAJ modele user

This commit is contained in:
2025-10-23 23:50:43 +02:00
parent 8e25bb44eb
commit 749ecc3021
2 changed files with 11 additions and 9 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ public class BaseDeDonnees {
private static final String USER = "val"; private static final String USER = "val";
private static final String PASS = "vali"; private static final String PASS = "vali";
public static java.sql.Connection getConnexion() throws java.sql.SQLException { public static java.sql.Connection getConnexion() throws SQLException {
try { try {
Class.forName("org.mariadb.jdbc.Driver"); Class.forName("org.mariadb.jdbc.Driver");
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
+9 -7
View File
@@ -2,7 +2,7 @@ import java.sql.*;
public class UtilisateurBD { public class UtilisateurBD {
public static int getUserIdByKey(String cle) throws java.sql.SQLException { public static int getUserIdByKey(String cle) throws SQLException {
String sql = "SELECT id FROM utilisateur WHERE cle_unique = ?"; String sql = "SELECT id FROM utilisateur WHERE cle_unique = ?";
Connection cnx = BaseDeDonnees.getConnexion(); Connection cnx = BaseDeDonnees.getConnexion();
@@ -21,17 +21,19 @@ public class UtilisateurBD {
return id; return id;
} }
public static int creerUtilisateur(String nom, String cle) throws java.sql.SQLException { public static int creerUtilisateur(String nom, String cle) throws SQLException {
String sql = "INSERT INTO utilisateur(nom, cle_unique) VALUES (?, ?)"; String sql = "INSERT INTO utilisateur(nom, cle_unique) VALUES (?, ?)";
java.sql.Connection cnx = BaseDeDonnees.getConnexion(); Connection cnx = BaseDeDonnees.getConnexion();
java.sql.PreparedStatement pst = cnx.prepareStatement(sql, java.sql.Statement.RETURN_GENERATED_KEYS); PreparedStatement pst = cnx.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
pst.setString(1, nom); pst.setString(1, nom);
pst.setString(2, cle); pst.setString(2, cle);
pst.executeUpdate(); pst.executeUpdate();
int id = -1; int id = -1;
java.sql.ResultSet rs = pst.getGeneratedKeys(); ResultSet rs = pst.getGeneratedKeys();
if (rs.next()) id = rs.getInt(1); if (rs.next()){
id = rs.getInt(1);
}
rs.close(); rs.close();
pst.close(); pst.close();
@@ -39,7 +41,7 @@ public class UtilisateurBD {
return id; return id;
} }
public static int getOrCreateUserId(String cle, String nomParDefaut) throws java.sql.SQLException { public static int getOrCreateUserId(String cle, String nomParDefaut) throws SQLException {
int id = getUserIdByKey(cle); int id = getUserIdByKey(cle);
if (id != -1){ if (id != -1){
return id; return id;