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

Composants Java Discussion :

jtable et base de donnée acess


Sujet :

Composants Java

  1. #1
    Futur Membre du Club
    Inscrit en
    Décembre 2006
    Messages
    9
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 9
    Points : 5
    Points
    5
    Par défaut jtable et base de donnée acess
    salut tt le monde je veu affiché mes données dans un jtable apartir de la base de donnée et g utilisé "AbstractTableModel" mai kan j'execute mon projet
    il me donne des erreur de threads si kelk1 pe m'aidé merci d'avance

    voici mon code :
    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
     
    import java.sql.*;
    import java.util.ArrayList;
    import javax.swing.table.AbstractTableModel;
    import java.util.*;
     
     
     
     
     
    public class TabModel extends AbstractTableModel{
     
    	String[] nomsColonnes = {"   nom de Compte","    Unité de mesure"};
    	ArrayList donnees ;
     
     
        public TabModel() {
     
     
        	donnees = new ArrayList();
        	initialiser();
     
        }
     
        public int getColumnCount(){return nomsColonnes.length;}
     
        public int getRowCount(){return donnees.size();}
     
        public String getColumnName(int col){return nomsColonnes[col];}
     
        public Object getValueAt(int row, int col){
     
     
    		 return ((ArrayList)donnees.get(row)).get(col);
     
        }
     
     
       public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}
     
        public void initialiser(){
        	String dataBase = "jdbc:odbc:depense";
     
       	     try
            {
            	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            	Connection dbConnection = DriverManager.getConnection(dataBase);
            	Statement stmt = dbConnection.createStatement();
            	System.out.println("connection etablie ");
            	ResultSet rslt = stmt.executeQuery("select * from tabCompte ");
     
     
     
            	while(rslt.next()){
            		donnees.add(rslt.getString(1));
            		donnees.add(rslt.getString(2));
     
     
            	}
            	        	dbConnection.close();
            	System.out.println("fermeture de connection a la base de données");
            }
            catch(Exception ex){
            	System.out.println("erreur de connection !!!!");
                }
        }	
     
     
    }

  2. #2
    Membre actif
    Profil pro
    Inscrit en
    Février 2006
    Messages
    238
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 238
    Points : 267
    Points
    267
    Par défaut
    Salut,

    peut tu nous donner ton erreur stp?

  3. #3
    Futur Membre du Club
    Inscrit en
    Décembre 2006
    Messages
    9
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 9
    Points : 5
    Points
    5
    Par défaut
    ben mon problem c quant je fai appel a cette class dans mon interface

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    TabModel tm = new TabModel() ;
      	     jTableDepense = new JTable(tm);
        	 jScrollPane = new JScrollPane(jTableDepense);
    il me donne 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
     
    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String
        at TabModel.getValueAt(TabModel.java:44)
        at TabModel.getColumnClass(TabModel.java:49)
        at javax.swing.JTable.getColumnClass(JTable.java:1833)
        at javax.swing.JTable.getCellRenderer(JTable.java:3878)
        at javax.swing.plaf.synth.SynthTableUI.paintCell(SynthTableUI.java:449)
        at javax.swing.plaf.synth.SynthTableUI.paintCells(SynthTableUI.java:347)
        at javax.swing.plaf.synth.SynthTableUI.paint(SynthTableUI.java:262)
        at javax.swing.plaf.synth.SynthTableUI.update(SynthTableUI.java:210)
        at javax.swing.JComponent.paintComponent(JComponent.java:742)
        at javax.swing.JComponent.paint(JComponent.java:1005)
        at javax.swing.JComponent.paintChildren(JComponent.java:842)
        at javax.swing.JComponent.paint(JComponent.java:1014)
        at javax.swing.JViewport.paint(JViewport.java:728)
        at javax.swing.JComponent.paintChildren(JComponent.java:842)
        at javax.swing.JComponent.paint(JComponent.java:1014)
        at javax.swing.JComponent.paintChildren(JComponent.java:842)
        at javax.swing.JComponent.paint(JComponent.java:1014)
        at javax.swing.JComponent.paintChildren(JComponent.java:842)
        at javax.swing.JComponent.paint(JComponent.java:1014)
        at javax.swing.JLayeredPane.paint(JLayeredPane.java:559)
        at javax.swing.JComponent.paintChildren(JComponent.java:842)
        at javax.swing.JComponent.paint(JComponent.java:1014)
        at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4963)
        at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4916)
        at javax.swing.JComponent._paintImmediately(JComponent.java:4859)
        at javax.swing.JComponent.paintImmediately(JComponent.java:4666)
        at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:451)
        at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:114)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String
        at TabModel.getValueAt(TabModel.java:44)
        at TabModel.getColumnClass(TabModel.java:49)
        at javax.swing.JTable.getColumnClass(JTable.java:1833)
        at javax.swing.JTable.getCellRenderer(JTable.java:3878)
        at javax.swing.plaf.synth.SynthTableUI.paintCell(SynthTableUI.java:449)
        at javax.swing.plaf.synth.SynthTableUI.paintCells(SynthTableUI.java:347)
        at javax.swing.plaf.synth.SynthTableUI.paint(SynthTableUI.java:262)
        at javax.swing.plaf.synth.SynthTableUI.update(SynthTableUI.java:210)
        at javax.swing.JComponent.paintComponent(JComponent.java:742)
        at javax.swing.JComponent.paint(JComponent.java:1005)
        at javax.swing.JComponent.paintChildren(JComponent.java:842)
        at javax.swing.JComponent.paint(JComponent.java:1014)
        at javax.swing.JViewport.paint(JViewport.java:728)
        at javax.swing.JComponent.paintChildren(JComponent.java:842)
        at javax.swing.JComponent.paint(JComponent.java:1014)
        at javax.swing.JComponent.paintChildren(JComponent.java:842)
        at javax.swing.JComponent.paint(JComponent.java:1014)
        at javax.swing.JComponent.paintChildren(JComponent.java:842)
        at javax.swing.JComponent.paint(JComponent.java:1014)
        at javax.swing.JLayeredPane.paint(JLayeredPane.java:559)
        at javax.swing.JComponent.paintChildren(JComponent.java:842)
        at javax.swing.JComponent.paint(JComponent.java:1014)
        at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4963)
        at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4916)
        at javax.swing.JComponent._paintImmediately(JComponent.java:4859)
        at javax.swing.JComponent.paintImmediately(JComponent.java:4666)
        at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:451)
        at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:114)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
    aidé moi svp et merdi d'avance

  4. #4
    Rédacteur
    Avatar de eclesia
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    2 108
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 108
    Points : 3 203
    Points
    3 203
    Par défaut
    Il n'arrive pas a definir la facon dont il doit afficher les cellules d'une des colonnes.


    Le probleme vient a mon avis d'un champ de ta table, il doit y avoir une valuer "null" dans la premiere instance .
    Quand il fait "TabModel.getColumnClass" ca plante, le "null" n'est pas une classe.


    Je regle le probleme a ma facon comme ca :

    Voici mon "format" de TableModel
    extrait de ma librairie perso
    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
     
    package jsl.table;
     
    import java.util.ArrayList;
     
    import javax.swing.table.AbstractTableModel;
     
    public class SimpleTableModel extends AbstractTableModel{
     
    private static final long serialVersionUID = 1;
    private String[] columnNames = {};
    private Class[] columnClass = {};
    private ArrayList<ArrayList<Object>> data = new ArrayList<ArrayList<Object>>();
     
     
    	public SimpleTableModel(){
    		super();
    		}
     
        public SimpleTableModel( ArrayList<ArrayList<Object>> dat, String[] cols, Class[] clas){
        	super();
        	update(dat,cols,clas);
        	}
     
        public void setClasses(Class[] cla){
        	columnClass = cla;
        	}
     
        public void empty(){
        	columnNames = new String[0];
    		data = new ArrayList<ArrayList<Object>>();
     
    		fireTableStructureChanged();
        	}
     
        public void update( ArrayList<ArrayList<Object>> dat, String[] cols, Class[] clas){
        	columnClass = clas;
        	columnNames = cols;    	    	
        	data = dat;
     
        	fireTableStructureChanged();
        	}
     
        public int getColumnCount() {
            return columnNames.length;
        	}
     
        public int getRowCount() {
            return data.size();
        	}
     
        /** use with care, only to change column names */
        public void setColumn(String[] column){
        	columnNames = column;
        	}
     
        public String getColumnName(int col) {
            return columnNames[col];
        	}
     
        public Object getValueAt(int row, int col) { 
        	ArrayList<Object> al = data.get(row);
            return al.get(col);
        	}
     
        public Class<?> getColumnClass(int c) {
            return columnClass[c];
        	}
     
        public boolean isCellEditable(int row, int col) {
        	return false;
        	}
     
        public void setValueAt(Object value, int row, int col) {
        	ArrayList<Object> al = data.get(row);
        	al.set(col, value);
        	data.set(row, al);
            fireTableCellUpdated(row, col);
        	}
     
    }
    et mon "format" de resultat de requete.
    extrait de ma librairie perso
    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
     
    package jsl.database.atome;
     
    import java.util.ArrayList;
     
    public class DBResult {
     
    public String[] columnnames = new String[0];
    public Class[] columnclass = new Class[0];
    public long nbrows = 0;
    public ArrayList<ArrayList<Object>> datas = new ArrayList<ArrayList<Object>>();
     
    	public void reset(){
    		columnnames = new String[0];
    		nbrows = 0;
    		datas = new ArrayList<ArrayList<Object>>();
    		}
     
    	public boolean compareTo(DBResult newdata){
    		boolean pareil = true;
     
    		//comparaison des donnees pour savoir si mise a jour il y a	
    		if( this != null && newdata != null){
     
    			if(columnnames.length != newdata.columnnames.length){
    				pareil = false;
    				}
     
    			else if(nbrows != newdata.nbrows){
    				pareil = false;
    				}
     
    			else if(columnnames.length > 0 && newdata.columnnames.length > 0){
     
    				for(int I=0; I<nbrows; I++){
    					ArrayList<Object> dataAL = datas.get(I);
    					ArrayList<Object> newdataAL = newdata.datas.get(I);
     
    					for(int T=0; T<columnnames.length; T++){
    						if(dataAL.get(T) == null && newdataAL.get(T) == null){}
    						else if (dataAL.get(T) == null || newdataAL.get(T) == null){
    							pareil = false;
    							break;
    							}						
    						else if( ! dataAL.get(T).equals(newdataAL.get(T))){
    							pareil = false;
    							break;
    							}
    						}
     
    					if(!pareil) break;
     
    					}
     
    				}
    			}
     
    		return pareil;		
    		}
     
    }
    Et voici comment je génére mon objet DBResult :
    extrait de ma librairie perso
    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
     
    DBResult dbrs = new DBResult();
     
    		int T = 0;
    		int nbcolonne = 0;
     
    		if( stmt != null ){
    			ResultSet rs = stmt.executeQuery(str);
     
    			// on remplit la liste des noms de colonnes
    			nbcolonne = rs.getMetaData().getColumnCount();
    			dbrs.columnnames = new String[nbcolonne];
    			dbrs.columnclass = new Class[nbcolonne];
     
     
    			for( int I = 0 ; I < rs.getMetaData().getColumnCount() ; I++ ){
    				dbrs.columnnames[I] = rs.getMetaData().getColumnName(I + 1);
    				dbrs.columnclass[I] = Class.forName(rs.getMetaData().getColumnClassName(I + 1));
    			}
     
    			while ( rs.next() ){
    				dbrs.nbrows++;
    				ArrayList<Object> rec = new ArrayList<Object>();
     
    				while ( T < nbcolonne ){
    					rec.add(rs.getObject(T + 1));
    					T++;
    				}
     
    				dbrs.datas.add(rec);
    				T = 0;
    			}
     
    		}

    ps : vous voulez ma librairie? je la donnerai au monde libre un jour, si j'estime qu'elle au vaut la peine.
    Systèmes d'Informations Géographiques
    - Projets : Unlicense.science - Apache.SIS

    Pour un monde sans BigBrother IxQuick ni censure RSF et Les moutons

  5. #5
    Futur Membre du Club
    Inscrit en
    Décembre 2006
    Messages
    9
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 9
    Points : 5
    Points
    5
    Par défaut
    merci mai moi j'utilise une base de donnée access
    et je ve seulement afficher mes données dans un jtable apartir de la BD

  6. #6
    Rédacteur
    Avatar de eclesia
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    2 108
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 108
    Points : 3 203
    Points
    3 203
    Par défaut
    Si tu travail toujours sur la meme table, alors tu modifis la méthode :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}
    en quelques chose du genre :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    public Class getColumnClass(int c) {
     
    switch (c) {
      case 0 : return Integer.class; break;
      case 1 : return String.class; break;
      ...
      }
     
    }
    ca devrait faire l'affaire, je pense.
    Systèmes d'Informations Géographiques
    - Projets : Unlicense.science - Apache.SIS

    Pour un monde sans BigBrother IxQuick ni censure RSF et Les moutons

  7. #7
    Futur Membre du Club
    Inscrit en
    Décembre 2006
    Messages
    9
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 9
    Points : 5
    Points
    5
    Par défaut
    merci mai ca ne ve pa marché

  8. #8
    Rédacteur
    Avatar de eclesia
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    2 108
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 108
    Points : 3 203
    Points
    3 203
    Par défaut
    ... Je viens de trouver cette erreur la :

    dans ta methode initializer tu fais :

    donnees.add(rslt.getString(1));
    donnees.add(rslt.getString(2));

    et pour recuperer la classe tu fais :

    return ((ArrayList)donnees.get(row)).get(col);


    C'est pour ca que ca ne marche pas.
    quand tu fais "donnees.get(row)" ca te retourne un String
    ensuite tu essai de le "convertir" en en Arraylist.
    Ca ne marche pas.

    Essai :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    while(rslt.next()){
     
       ArrayList ar = new ArrayList();
       ar.add(rslt.getString(1));
       ar.add(rslt.getString(2));
     
       donnees.add(ar);
     
            	}
    Systèmes d'Informations Géographiques
    - Projets : Unlicense.science - Apache.SIS

    Pour un monde sans BigBrother IxQuick ni censure RSF et Les moutons

  9. #9
    Futur Membre du Club
    Inscrit en
    Décembre 2006
    Messages
    9
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 9
    Points : 5
    Points
    5
    Par défaut
    merci beaucoup mon frére

Discussions similaires

  1. Insertion jTable et Base de données problème
    Par Ercan67000 dans le forum Composants
    Réponses: 3
    Dernier message: 24/01/2012, 14h30
  2. Réponses: 4
    Dernier message: 04/05/2010, 15h55
  3. Réponses: 5
    Dernier message: 03/08/2009, 15h40
  4. jtable et base de données?
    Par 7rouz dans le forum Composants
    Réponses: 2
    Dernier message: 27/07/2009, 09h41
  5. JTable avec base de donnée
    Par grospatapouf dans le forum Composants
    Réponses: 4
    Dernier message: 06/09/2008, 18h10

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