debut changement

This commit is contained in:
2022-11-29 15:09:41 +01:00
parent c215672e38
commit 1351345df5
9 changed files with 218 additions and 27 deletions

View File

@@ -1,5 +1,13 @@
package fr.iutfbleau.projetIHM2022FI2.MNP;
import fr.iutfbleau.projetIHM2022FI2.API.*;
import java.sql.Connection;
import org.mariadb.jdbc.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.util.*;
/**
* Usine abstraite gérant l'ensemble des changements.
@@ -10,14 +18,15 @@ public class AbstractChangementFactoryNP implements AbstractChangementFactory {
// l'usine à groupe travaillant en tandem avec cette usine.
private AbstractGroupeFactory agf;
private JFrame fenetre;
// On utilise une table de hachage pour retrouver facilement un changement (à partir de son id).
// Si il y a beaucoup de changements c'est plus rapide que de parcourir toute une liste.
private HashMap<Integer,Changement> brain;
public AbstractChangementFactoryNP(AbstractGroupeFactory agf){
public AbstractChangementFactoryNP(AbstractGroupeFactory agf, JFrame fenetre){
Objects.requireNonNull(agf,"On ne peut pas créer une usine à changement dont l'usine à groupe parternaire est null");
this.agf=agf;
this.fenetre=fenetre;
this.brain=new HashMap<Integer,Changement>();
}
@@ -96,9 +105,71 @@ public class AbstractChangementFactoryNP implements AbstractChangementFactory {
Objects.requireNonNull(A,"Le groupe d'origine ne peut pas être null");
Objects.requireNonNull(B,"Le groupe d'arrivée ne peut pas être null");
Objects.requireNonNull(e,"L'étudiant ne peut pas être null");
Changement c = new ChangementNP(A,e,B);
this.brain.put(Integer.valueOf(c.getId()),c);
this.brain.put(Integer.valueOf(c.getId()),c);
Connection cnx=this.cnx();
try{
PreparedStatement pst=cnx.prepareStatement("INSERT INTO `Changement` (`idGroupeA`, `idGroupeB`, `idEtudiant`) VALUES (?, ?, ?);");
pst.setInt(1, A.getId());
pst.setInt(2, B.getId());
pst.setInt(3, e.getId());
pst.executeUpdate();
pst.close();
}catch(SQLException er){
if(this.erreurSQL()){
this.createChangement(A, e, B);
}else{
return;
}
}
this.close(cnx);
}
// **********************
// FONCTION POUR SIMPLIFIER LES Modification BD
// ***********************
private Connection cnx(){
//On se Connecte a la BD
try{
Class.forName("org.mariadb.jdbc.Driver");
Connection cnx = DriverManager.getConnection(
"jdbc:mariadb://dwarves.iut-fbleau.fr/chaignea",
"chaignea", "Chaigneauphpmyadmin");
return cnx;
}catch(Exception e){
if(this.erreurCO()==true){
return this.cnx();
}
}
return null;
}
private boolean erreurCO(){
if(JOptionPane.showConfirmDialog(this.fenetre, "erreur connection a la BD reassayer?", "erreur connection", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){
return true;
}else{
this.fenetre.dispose();
return false;
}
}
private boolean erreurSQL(){
if(JOptionPane.showConfirmDialog(this.fenetre, "erreur lors de la modification, reasssayer?", "erreur SQL", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){
return true;
}else{
return false;
}
}
private void close(AutoCloseable clos){
try{
clos.close();
}catch(Exception e){
if(this.erreurCO()==true)
this.close(clos);
}
}
}

View File

@@ -375,13 +375,13 @@ public class AbstractGroupeFactoryNP implements AbstractGroupeFactory {
}
private void close(AutoCloseable clos){
try{
clos.close();
}catch(Exception e){
if(this.erreurCO()==true)
this.close(clos);
try{
clos.close();
}catch(Exception e){
if(this.erreurCO()==true)
this.close(clos);
}
}
}
private boolean saveEtu(Etudiant etudiant, Groupe g){