Implementation de la bdd

This commit is contained in:
Nabil Moukhlis 2020-12-27 16:13:16 +01:00
parent 2d4b549824
commit 7f1fb4a929
4 changed files with 21 additions and 8 deletions

View file

@ -1,11 +1,11 @@
chatapp/Model/MessageHorodate.java
chatapp.Model.MessageHorodate
chatapp/Model/Utilisateur.java
chatapp.Model.Utilisateur
chatapp/Model/ListUtilisateurs.java
chatapp.Model.ListUtilisateurs
chatapp/Launcher.java
chatapp.Launcher
chatapp/Model/ListUtilisateurs.java
chatapp.Model.ListUtilisateurs
chatapp/Model/Utilisateur.java
chatapp.Model.Utilisateur
chatapp/Model/MessageHorodate.java
chatapp.Model.MessageHorodate
chatapp/Model/DataBase.java
chatapp.Model.DataBase
chatapp/Protocol/RunnerEcouteTCP.java

View file

@ -98,6 +98,7 @@ public class ChatApp implements PropertyChangeListener {
//-------Change son propre nom d'utilisateur-------//
this.getActifUsers().modifierList(this.getMe().getPseudo(), nouveau);
this.getMe().setPseudo(nouveau);
db.majPseudo("MacYAYA",nouveau);
System.out.println("Changement pseudo accepte, nouvelle liste des utilisateurs actifs:");
this.getActifUsers().afficherListeUtilisateurs();
return true;
@ -150,6 +151,10 @@ public class ChatApp implements PropertyChangeListener {
System.out.println("Connexion reussie");
//db.ajoutUtilisateurs(this.me.getId(),this.me.getPseudo());
db.majPseudo("MacYAYA",this.me.getPseudo());
db.majUtilisateursActifs(true,"MACYAYA");
//db.ajoutUtilisateurs(this.me.getId(),this.me.getPseudo());
//db.CreationTableHistorique("MACE","MACF");
//db.ajoutHistorique("MACE","MACF","Prout");
return true;
}
else
@ -183,6 +188,7 @@ public class ChatApp implements PropertyChangeListener {
// Message que l'on envoie à tous les utilisateurs actifs
String broadcastMessage = "Deconnexion\n" + this.getMe().toString() ;
UDPEchange.EnvoiBroadcast(broadcastMessage);
db.majUtilisateursActifs(false,"MACYAYA");
try {
main.stop();
@ -257,4 +263,5 @@ public class ChatApp implements PropertyChangeListener {
public Application getMain() {
return main;
}
public DataBase getDb(){return this.db;}
}

View file

@ -80,7 +80,7 @@ public class DataBase {
grand = ID1 ;
}
String nomTable = "Chat_" + petit + "_" + grand ;
String Requete = "CREATE TABLE IF NOT EXISTS`" + nomTable +"` (\n" + "`Destinataire` varchar(100) NOT NULL,\n" + " `Source` varchar(100) NOT NULL,\n" + " `Envoi` timestamp NOT NULL,\n" + " `Message` text NOT NULL\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ;";
String Requete = "CREATE TABLE IF NOT EXISTS`" + nomTable +"` (\n" + "`Destinataire` varchar(100) NOT NULL,\n" + " `Source` varchar(100) NOT NULL,\n" + " `Envoi` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n" + " `Message` text NOT NULL\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ;";
System.out.println(Requete);
PreparedStatement Ps = null ;
//Statement est utilisé pour envoyer une requete SQL à la base de donnee
@ -180,7 +180,7 @@ public class DataBase {
*/
public void ajoutUtilisateurs(String ID , String Pseudo ) {
// Verification que l'utilisateur n'existe pas
String existe= "SELECT Pseudo FROM Customers WHERE ID = " + ID;
String existe= "SELECT Pseudo FROM Utilisateurs WHERE ID = " + ID +",";
Statement stmt = null;
ResultSet rs = null ;
try {

View file

@ -11,6 +11,7 @@ import java.net.Socket;
import java.util.ArrayList;
import chatapp.Controller.ChatApp;
import chatapp.Model.DataBase;
import chatapp.Model.MessageHorodate;
import chatapp.Model.Utilisateur;
import chatapp.View.FenetreSession;
@ -23,6 +24,7 @@ public class SessionClavardage extends Thread {
private ObjectOutputStream out;
private ObjectInputStream in;
private PropertyChangeSupport pcs;
private DataBase db;
private ArrayList<MessageHorodate> derniersMsg; // on met temporairement les derniers msgs pour éviter les pb de synchro inter-threads
private int SessionID;
public SessionClavardage(Socket link, ChatApp app) {
@ -37,6 +39,7 @@ public class SessionClavardage extends Thread {
}
this.derniersMsg = new ArrayList<MessageHorodate>();
this.SessionID = 2;
this.db=DataBase.getInstance();
Platform.runLater( () ->
new FenetreSession(this)
@ -59,6 +62,7 @@ public class SessionClavardage extends Thread {
}
this.derniersMsg = new ArrayList<MessageHorodate>();
this.pcs = new PropertyChangeSupport(this);
//db.CreationTableHistorique("Doudou","Marvel");
this.start();
Platform.runLater( () ->
new FenetreSession(this)
@ -90,6 +94,7 @@ public class SessionClavardage extends Thread {
MessageHorodate msgh = new MessageHorodate(getU2(),getApp().getMe(),msg,1);
try {
getOut().writeObject(msgh.toString());
app.getDb().ajoutHistorique(app.getMe().getId(), u2.getId(),msg);
} catch (IOException e) {
e.printStackTrace();
}
@ -106,6 +111,7 @@ public class SessionClavardage extends Thread {
String plaintext = null;
MessageHorodate msg = null;
System.out.println("Session demarre");
app.getDb().CreationTableHistorique(app.getMe().getId(), u2.getId());
while(true) {
try {
plaintext = (String) getIn().readObject();