IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Java Discussion :

Interférences entre mise à jour du modèle d'une JTable et d'une base Derby


Sujet :

Java

  1. #1
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2006
    Messages
    958
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Janvier 2006
    Messages : 958
    Points : 213
    Points
    213
    Par défaut Interférences entre mise à jour du modèle d'une JTable et d'une base Derby
    salut

    je développe un programme de gestion d'envoi de SMS.
    j'ai un problème que j'ai résolu mais que partiellement, cad d'une manière qui ne me satisfait pas.

    je m'explique : le programme gère une liste de rendez-vous de patients, et cette liste se retrouve en 2 endroits,
    1.dans un modèle de JTable
    2.dans une base derby.

    à un certain moment je veux supprimer les rdv (1 RDV => 1 SMS) qui sont périmés.
    donc je fais une première boucle pour récolter les RDV (il y a une class RDV) qui sont trop vieux puis je fais une deuxième boucle pour supprimer les rdv les rdv récoltés.
    je dois faire deux passes car sinon je serais dans une configuration où je supprime des éléments d'une collection que je parcours, donc il y aurait un conflit.

    mais voici le code, ça sera peut-être plus clair :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    System.out.println("loop_horaire / suppression vieux RDV de AL locale");
    		/*
    		 * 1.suppression vieux RDV de ALLocale
    		 */
    		Locale locale = Locale.getDefault();
    		DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL,
    				locale);
    		Liste_rdv_minimum rdv_a_supp = new Liste_rdv_minimum();
     
    		for (RDV rdv : Fenetre.al_locale.rdvs) {
     
    			Calendar cal_rdv = Calendar.getInstance();
    			cal_rdv.set(Calendar.YEAR, (rdv.DATEXA.getYear() + 1900));
    			cal_rdv.set(Calendar.MONTH, rdv.DATEXA.getMonth());
    			cal_rdv.set(Calendar.DAY_OF_MONTH, rdv.DATEXA.getDate());
    			cal_rdv.set(Calendar.HOUR_OF_DAY, rdv.getDATEXA().getHours());
    			cal_rdv.set(Calendar.MINUTE, rdv.DATEXA.getMinutes());
     
    			Calendar cal_limite = Calendar.getInstance();
    			cal_limite.setTime(new Date());
    			cal_limite.add(Calendar.DATE, -7);
     
    			System.out.println("vieillesse : " + rdv.getNAME() + " / "
    					+ dateFormat.format(cal_rdv.getTime()));
     
    			// limite non stricte : un RDV de la même date que celle de la
    			// limite est gardé
    			if (cal_rdv.before(cal_limite)) {
    				// System.out.println("suppression pour vieillesse de " +
    				// rdv.NAME
    				// + " / " + rdv.NUMRDV + " / "
    				// + dateFormat.format(cal_rdv.getTime())
    				// + " alors que limite = "
    				// + dateFormat.format(cal_limite.getTime()));
    				rdv_a_supp.addRow(rdv);
     
    			}
     
    		}
    		for (RDV rdv2 : rdv_a_supp.rdvs) {
     
    			Fenetre.al_locale.delete_rdv_in_base(rdv2);
    			Fenetre.al_locale.removeRow(rdv2);
     
    			try {
    				Thread.sleep(500);
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    ceci ne marche PAS si j'enlève la ligne du thread.sleep.

    j'obtient alors :
    - un affichage de la JTable où une ligne manque (et il n'y a pas de décalage vers le haut des lignes restantes)
    - une erreur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    debut delete_rdv_in_base, RDV : LEM/num rdv=RHCXD24/date tel=Wed Dec 14 00:00:00 CET 2011
    delete from rdv where NUMRDV='RHCXD24'
    Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
    	at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    	at java.util.ArrayList.get(ArrayList.java:382)
    	at metier.Liste_rdv_minimum.getValueAt(Liste_rdv_minimum.java:49)
    	at javax.swing.JTable.getValueAt(JTable.java:2720)
    	at javax.swing.JTable.prepareRenderer(JTable.java:5718)
    	at org.jvnet.substance.SubstanceTableUI.paintCell(SubstanceTableUI.java:912)
    	at org.jvnet.substance.SubstanceTableUI.paintCells(SubstanceTableUI.java:661)
    	at org.jvnet.substance.SubstanceTableUI.paint(SubstanceTableUI.java:545)
    	at org.jvnet.substance.SubstanceTableUI.__org__jvnet__substance__SubstanceTableUI__update(SubstanceTableUI.java:2442)
    	at org.jvnet.substance.SubstanceTableUI.update(SubstanceTableUI.java)
    	at javax.swing.JComponent.paintComponent(JComponent.java:778)
    	at javax.swing.JComponent.paint(JComponent.java:1054)
    	at javax.swing.JComponent.paintChildren(JComponent.java:887)
    	at javax.swing.JComponent.paint(JComponent.java:1063)
    	at javax.swing.JViewport.paint(JViewport.java:725)
    	at javax.swing.JComponent.paintChildren(JComponent.java:887)
    	at javax.swing.JComponent.paint(JComponent.java:1063)
    	at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
    	at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
    	at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
    	at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
    	at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
    	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:770)
    	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
    	at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
    	at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
    	at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
    	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
    	at java.awt.EventQueue.access$000(EventQueue.java:101)
    	at java.awt.EventQueue$3.run(EventQueue.java:666)
    	at java.awt.EventQueue$3.run(EventQueue.java:664)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
    fin delete_rdv_in_base
    debut delete_rdv_in_base, RDV : DICK/num rdv=DCFF9H/date tel=Sun Dec 18 00:00:00 CET 2011
    delete from rdv where NUMRDV='DCFF9H'
    Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    	at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    	at java.util.ArrayList.get(ArrayList.java:382)
    	at metier.Liste_rdv_minimum.getValueAt(Liste_rdv_minimum.java:49)
    	at javax.swing.JTable.getValueAt(JTable.java:2720)
    	at javax.swing.JTable.prepareRenderer(JTable.java:5718)
    	at org.jvnet.substance.SubstanceTableUI.paintCell(SubstanceTableUI.java:912)
    	at org.jvnet.substance.SubstanceTableUI.paintCells(SubstanceTableUI.java:661)
    	at org.jvnet.substance.SubstanceTableUI.paint(SubstanceTableUI.java:545)
    	at org.jvnet.substance.SubstanceTableUI.__org__jvnet__substance__SubstanceTableUI__update(SubstanceTableUI.java:2442)
    	at org.jvnet.substance.SubstanceTableUI.update(SubstanceTableUI.java)
    	at javax.swing.JComponent.paintComponent(JComponent.java:778)
    	at javax.swing.JComponent.paint(JComponent.java:1054)
    	at javax.swing.JComponent.paintChildren(JComponent.java:887)
    	at javax.swing.JComponent.paint(JComponent.java:1063)
    	at javax.swing.JViewport.paint(JViewport.java:725)
    	at javax.swing.JComponent.paintChildren(JComponent.java:887)
    	at javax.swing.JComponent.paint(JComponent.java:1063)fin delete_rdv_in_base
    loop_horaire / creation ALatraiter
    loop_horaire / envoi des non envoyes deja passes
    loop_horaire / demande de nvo statut pouyr les message deja envoyes de statuyt incertain
    exiting loop_horaire
     
    	at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
    	at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
    	at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
    	at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
    	at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
    	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:770)
    	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
    	at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
    	at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
    	at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
    	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
    	at java.awt.EventQueue.access$000(EventQueue.java:101)
    	at java.awt.EventQueue$3.run(EventQueue.java:666)
    	at java.awt.EventQueue$3.run(EventQueue.java:664)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
    j'ai eu l'idée du thread.sleep en utilisant le débogueur d' eclipse, je me suis rendu compte que le programme marchait alors, et que une différence était qu'il y avait du temps entre les différentes instructions.

    le problème c'est que mon programme a pour but de traiter des milliers de lignes, et voilà donc le problème : il deviendra trop lent lorsqu'il y aura beaucoup de lignes.

    savez-vous comment résoudre ce problème, sachant que les 2 lignes en cause
    (delete_rdv_in_base et remove_row ne devraient pas à priori traiter des éléments différents?

    voici enfin les 2 méthodes :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    public void delete_rdv_in_base(RDV rdv) {
     
    		System.out.println("debut delete_rdv_in_base, RDV : " + rdv.NAME
    				+ "/num rdv=" + rdv.NUMRDV + "/date tel=" + rdv.DATTEL);
    		Statement stmt;
    		try {
     
    			String sql = "delete from rdv where NUMRDV='" + rdv.getNUMRDV()
    					+ "'";
     
    			System.out.println(sql);
    			stmt = connexion.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
    					ResultSet.CONCUR_READ_ONLY);
    			stmt.executeUpdate(sql);
     
    		} catch (SQLException e) {
    			System.out.println("e7 : " + e);
    			try {
    				// con_to_derby.close();
    			} catch (Exception ex) {
    				System.out.println("err1" + ex);
    			}
     
    		}
     
    		System.out.println("fin delete_rdv_in_base");
    	}
    et

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public void removeRow(int i) {
     
    		rdvs.remove(i);
    		// fireTableRowsDeleted(i, i);
    		fireTableDataChanged();
     
    	}
     
    	public void removeRow(RDV rdv) {
     
    		removeRow(rdvs.indexOf(rdv));
    		fireTableDataChanged();
     
    	}
    dites-moi si vous voulez plus d'infos.

    merci,

    olivier
    bonne année 2012!

  2. #2
    Membre chevronné
    Inscrit en
    Mai 2006
    Messages
    1 364
    Détails du profil
    Informations forums :
    Inscription : Mai 2006
    Messages : 1 364
    Points : 1 984
    Points
    1 984
    Par défaut
    Ca, ca sent la vilaine suppression d'une ligne hors du Thread EDT...
    Ton traitement se fait bien dans un thread à part, non ?
    Si c'est un traitement long, il y a la classe SwingWorker qui existe pour ca

    Bonne année à toi aussi

  3. #3
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2006
    Messages
    958
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Janvier 2006
    Messages : 958
    Points : 213
    Points
    213
    Par défaut
    je vais regarder ça bientôt (EDT,swingWorker)

    merci!

  4. #4
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    129
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2009
    Messages : 129
    Points : 86
    Points
    86
    Par défaut
    Essaye comme ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    public RDV removeRow(int i) {
     
    	RDV r = rdvs.remove(i);
    	fireTableDataChanged();
    	return r;
    }
     
    public RDV removeRow(RDV rdv) {
     
    	RDV r = removeRow(rdvs.indexOf(rdv));
    	fireTableDataChanged();
    	return r;
    }
    Puis dans le for :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    for (RDV rdv2 : rdv_a_supp.rdvs) {
     
            RDV r = Fenetre.al_locale.removeRow(rdv2);
    	Fenetre.al_locale.delete_rdv_in_base(r);
            ...
    }
    Car quand tu passe rdv2 à delete_rdv_in_base, tu lui passes une référence de ton objet. Or quand tu appel removeRow, tu supprimes cette objet en mémoire et donc tu perds ta référence.
    Avec ma méthode tu récupères l'objet qui vient d'être supprimé de ta liste.
    Mais l'objet est toujours en mémoire et utilisable.
    Je crois que le problème vient de là. A essayer...

    Ou alors tu supprimes Fenetre.al_locale.removeRow(rdv2); de la boucle et tu fais Fenetre.al_locale.removeAllRow(rdv_a_supp.rdvs); après la boucle :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    public void removeAllRow(Collection<RDV> rdvss) {
     
    	rdvs.removeAll(rdvss);
    	fireTableDataChanged();
    }

Discussions similaires

  1. [WD-2003] Mise à jour de modèle de styles
    Par nchristedem dans le forum Word
    Réponses: 4
    Dernier message: 16/11/2011, 21h27
  2. Problème de mise à jour du modèle
    Par Teaniel dans le forum Bases de données
    Réponses: 0
    Dernier message: 06/11/2011, 01h28
  3. diff avant et après sorting pour mise à jour du modèle
    Par Leon Ira dans le forum Composants
    Réponses: 2
    Dernier message: 30/07/2009, 21h55
  4. [VB6] Mise à jour d'un enregistrement d'un table d'une BD
    Par jfdmagic dans le forum VB 6 et antérieur
    Réponses: 7
    Dernier message: 12/05/2009, 16h20
  5. [JTable] Probleme pour afficher la mise à jour du modèle
    Par Yannick_from_31 dans le forum Composants
    Réponses: 2
    Dernier message: 02/06/2006, 19h10

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo