From 4553efe2018243f0379b09a9a370a532f2af847d Mon Sep 17 00:00:00 2001 From: "thomas.millot" Date: Mon, 18 Oct 2021 16:52:21 +0200 Subject: [PATCH] Merge origin/master Conflicts: web/WEB-INF/AuthentificationJSP.jsp --- src/java/bdd/ClientMysql.java | 22 ---- src/java/bdd/PompierMysql.java | 158 +++++++++++++++++++++++++++ src/java/com/test/beans/Pompier.java | 8 ++ src/java/com/test/forms/MD5.java | 35 ++++++ 4 files changed, 201 insertions(+), 22 deletions(-) delete mode 100644 src/java/bdd/ClientMysql.java create mode 100644 src/java/bdd/PompierMysql.java create mode 100644 src/java/com/test/forms/MD5.java diff --git a/src/java/bdd/ClientMysql.java b/src/java/bdd/ClientMysql.java deleted file mode 100644 index e5a5cfb..0000000 --- a/src/java/bdd/ClientMysql.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package bdd; - -import java.sql.Connection; - -/** - * - * @author thomas.millot - */ -public class ClientMysql { - - private Connection theConnection; - - public ClientMysql() - { - theConnection = Connexion.getConnect("localhost", "sdis29", "admin", "minda"); - } -} diff --git a/src/java/bdd/PompierMysql.java b/src/java/bdd/PompierMysql.java new file mode 100644 index 0000000..88e039d --- /dev/null +++ b/src/java/bdd/PompierMysql.java @@ -0,0 +1,158 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package bdd; + +import com.mysql.cj.xdevapi.Client; +import com.test.beans.Pompier; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; + +/** + * + * @author thomas.millot + */ +public class PompierMysql { + + private Connection theConnection; + private Pompier unPompier; + + public PompierMysql() + { + theConnection = Connexion.getConnect("localhost", "sdis29", "admin", "minda"); + } + + public ArrayList readAll() { + ArrayList lesPompiers = new ArrayList<>(); + + try { + Statement stmt = theConnection.createStatement(); + ResultSet resultQ = null; + resultQ = stmt.executeQuery("SELECT * FROM client"); + while (resultQ.next()) { + unPompier = new Pompier(resultQ.getInt("id"), + resultQ.getString("nom"), + resultQ.getString("prenom"), + resultQ.getString("statut"), + resultQ.getString("typePers"), + resultQ.getString("mail"), + resultQ.getString("login"), + resultQ.getString("mdp"), + resultQ.getString("adresse"), + resultQ.getInt("cp"), + resultQ.getString("ville"), + resultQ.getInt("bip"), + resultQ.getInt("nbGardes"), + resultQ.getInt("grade"), + resultQ.getString("commentaire"), + resultQ.getString("dateEnreg"), + resultQ.getString("dateModif")); + lesPompiers.add(unPompier); + } + resultQ.close(); + stmt.close(); + //theConnection.close(); + } catch (SQLException ex) { + System.out.println("SQLException : " + ex.getMessage()); + System.out.println("SQLState : " + ex.getSQLState()); + System.out.println("Code erreur : " + ex.getErrorCode()); + } + + return lesPompiers; + } + + /** + * Creation du client passé en paramètre dans la table client + * Requête non préparée + * @param c objet de type Client (sans identifiant) + * @return int : id du client créé + */ + public int create(Pompier p) { + int id = -1; + try { + Statement stmt = theConnection.createStatement(); + int status = stmt.executeUpdate( + "INSERT INTO pompier (nom, prenom, statut, mail, login, mdp, adresse, cp, ville, bip, nbGardes, grade, commentaire, dateEnreg, dateModif) " + + "VALUES ('" + p.getNom() + "', '" + + p.getPrenom() + "', '" + + p.getStatut() + "', " + + p.getMail() + ", '" + + p.getLogin() + "', '" + + p.getMdp() + "', '" + + p.getAdresse() + "', '" + + p.getVille()+ "', '" + + p.getBip()+ "', '" + + p.getNbGardes()+ "', '" + + p.getGrade()+ "', '" + + p.getCommentaire()+ "', '" + + p.getDateEnreg()+ "', '" + + p.getDateModif() + "');", + Statement.RETURN_GENERATED_KEYS); + + // Recherche de l'identifiant du client créé + if (status > 0) { + ResultSet result = stmt.getGeneratedKeys(); + if (result.first()) { + id = result.getInt(1); + } + } + } catch (SQLException ex) { + System.out.println("SQLException : " + ex.getMessage()); + System.out.println("SQLState : " + ex.getSQLState()); + System.out.println("Code erreur : " + ex.getErrorCode()); + } + return id; + } + + /** + * Creation du client passé en paramètre dans la table client + * Requête préparée + * @param c objet de type Client (sans identifiant) + * @return int : id du client créé + */ + public int createRP(Pompier p) { + int id = -1; + try { + PreparedStatement stmt = null; + String sql = "INSERT INTO pompier (nom, prenom, statut, mail, login, mdp, adresse, cp, ville, bip, nbGardes, grade, commentaire, dateEnreg, dateModif) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; + stmt = theConnection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); + stmt.setString(1, p.getNom()); + stmt.setString(2, p.getPrenom()); + stmt.setString(3, p.getStatut()); + stmt.setString(4, p.getMail()); + stmt.setString(5, p.getLogin()); + stmt.setString(6, p.getMdp()); + stmt.setString(7, p.getAdresse()); + stmt.setString(8, p.getVille()); + stmt.setInt(9, p.getBip()); + stmt.setInt(10, p.getNbGardes()); + stmt.setInt(11, p.getGrade()); + stmt.setString(12, p.getCommentaire()); + stmt.setString(13, p.getDateEnreg()); + stmt.setString(14, p.getDateModif()); + System.out.println("Requête : " + stmt.toString()); + int status = stmt.executeUpdate(); + + + // Recherche de l'identifiant du client créé + if (status > 0) { + ResultSet result = stmt.getGeneratedKeys(); + if (result.first()) { + id = result.getInt(1); + } + } + } catch (SQLException ex) { + System.out.println("SQLException : " + ex.getMessage()); + System.out.println("SQLState : " + ex.getSQLState()); + System.out.println("Code erreur : " + ex.getErrorCode()); + } + return id; + } +} diff --git a/src/java/com/test/beans/Pompier.java b/src/java/com/test/beans/Pompier.java index fbc1698..ba83c7b 100644 --- a/src/java/com/test/beans/Pompier.java +++ b/src/java/com/test/beans/Pompier.java @@ -52,6 +52,14 @@ public class Pompier { this.dateModif = dateModif; } + public Pompier(int aInt, String string, String string0, String string1, String string2, int aInt0, String string3, String string4, String string5, String string6, String string7, String string8) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + public Pompier(int aInt, String string, String string0, String string1, String string2, String string3, String string4, String string5, String string6, int aInt0, String string7, int aInt1, int aInt2, int aInt3, String string8, String string9, String string10) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + public int getId() { return id; } diff --git a/src/java/com/test/forms/MD5.java b/src/java/com/test/forms/MD5.java new file mode 100644 index 0000000..9b023ae --- /dev/null +++ b/src/java/com/test/forms/MD5.java @@ -0,0 +1,35 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.test.forms; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.xml.bind.DatatypeConverter; + +/** + * + * @author Dominique_2 + */ +public abstract class MD5 { + + public static String encode(String uneChaine){ + MessageDigest md = null; + String myHash = null; + try { + md = MessageDigest.getInstance("MD5"); + } catch (NoSuchAlgorithmException ex) { + Logger.getLogger(AuthentifForm.class.getName()).log(Level.SEVERE, null, ex); + } + md.update(uneChaine.getBytes()); + byte[] digest = md.digest(); + myHash = DatatypeConverter.printHexBinary(digest).toLowerCase(); + + return myHash; + } + +}