Partage
  • Partager sur Facebook
  • Partager sur Twitter

Afficher les données d'une table MariaDB

    15 août 2017 à 18:21:09

    Bonsoir à tous, 

    Dans le cadre d'une application de gestion d'emploi du temps, j'ai créer une table "horaires" voila à quoi elle ressemble : 

    idHoraire = "H1", jourSemaine = "Lundi", Plage horaire= "08:00 - 90:30"

    idHoraire = "H2", jourSemaine = "Lundi", Plage horaire= "09:45 - 11:15"

    ....

    idHoraire = "H25", jourSemaine = "Vendredi", Plage horaire= "15:30 - 17:00"            (Toutes les données sont de type String)

    La 1ere étape serait d'affecter les classes(groupes d'éleves) aux salles de la manière suivante: chaque classe ne peut étudier que dans une seule salle et une salle peut être attribuée à deux classes. 

    J'ai donc voulu dans un premier temps créer deux tableaux d'horaires contenant les horaires répartis de la façon indiquée dans l'image(Tout en bas :p), pour pouvoir par la suite affecter les salles aux classe en utilisant les deux tableaux d'horaires.

    Voilà donc le code:  (Qui ne me renvoi que les données de la dernière ligne de ma BDD >_< ) 

    Quelqu'un pourrait me dire où est mon erreur ?

    Je vous remercies d'avance pour vos réponses :)

    public class Z_Horaire {
    	
    	static String [][] horaires_1 = new String [13][3];
    	static String [][] horaires_2 = new String [12][3];
    	
    	
    	public Z_Horaire(String jourSemaine, String horaire, String heureFin, String idHoraire) {
    		super();
    		this.jourSemaine = jourSemaine;
    		this.horaire = horaire;
    		this.idHoraire = idHoraire;}
    	
    
    	public Z_Horaire(){
    		
    				try {	          Class.forName("org.mariadb.jdbc.Driver");
    						  connex = DriverManager.getConnection("jdbc:mariadb://127.0.0.1:3306/ecole?user=root&password=0000");
    				
    						  Statement requete = connex.createStatement();
    						  ResultSet resultat = requete.executeQuery("Select idHoraire, jour, plageHoraire from horaires");
    						  
    						  while(resultat.next()){
    						  
    						  
    								  horaires_1[0][0]= resultat.getString(1);  horaires_1[0][1]= resultat.getString(2);  horaires_1[0][2]= resultat.getString(3); 
    								  horaires_1[1][0]= resultat.getString(1);  horaires_1[1][1]= resultat.getString(2);  horaires_1[1][2]= resultat.getString(3);
    								  horaires_1[2][0]= resultat.getString(1);  horaires_1[2][1]= resultat.getString(2);  horaires_1[2][2]= resultat.getString(3);
    								  
    								  horaires_2[0][0]= resultat.getString(1);  horaires_2[0][1]= resultat.getString(2);  horaires_2[0][2]= resultat.getString(3);
    								  horaires_2[1][0]= resultat.getString(1);  horaires_2[1][1]= resultat.getString(2);  horaires_2[1][2]= resultat.getString(3);
    								  horaires_2[2][0]= resultat.getString(1);  horaires_2[2][1]= resultat.getString(2);  horaires_2[2][2]= resultat.getString(3);
    								  horaires_2[3][0]= resultat.getString(1);  horaires_2[3][1]= resultat.getString(2);  horaires_2[3][2]= resultat.getString(3);
    								  horaires_2[4][0]= resultat.getString(1);  horaires_2[4][1]= resultat.getString(2);  horaires_2[4][2]= resultat.getString(3);
    						
    						
    								  horaires_1[3][0]= resultat.getString(1);  horaires_1[3][1]= resultat.getString(2);  horaires_1[3][2]= resultat.getString(3);
    								  horaires_1[4][0]= resultat.getString(1);  horaires_1[4][1]= resultat.getString(2);  horaires_1[4][2]= resultat.getString(3);
    								  horaires_1[5][0]= resultat.getString(1);  horaires_1[5][1]= resultat.getString(2);  horaires_1[5][2]= resultat.getString(3);
    								  horaires_1[6][0]= resultat.getString(1);  horaires_1[6][1]= resultat.getString(2);  horaires_1[6][2]= resultat.getString(3);
    								  horaires_1[7][0]= resultat.getString(1);  horaires_1[7][1]= resultat.getString(2);  horaires_1[7][2]= resultat.getString(3);
    						
    						
    								  horaires_2[5][0]= resultat.getString(1);  horaires_2[5][1]= resultat.getString(2);  horaires_2[5][2]= resultat.getString(3);
    								  horaires_2[6][0]= resultat.getString(1);  horaires_2[6][1]= resultat.getString(2);  horaires_2[6][2]= resultat.getString(3);
    								  horaires_2[7][0]= resultat.getString(1);  horaires_2[7][1]= resultat.getString(2);  horaires_2[7][2]= resultat.getString(3);
    								  horaires_2[8][0]= resultat.getString(1);  horaires_2[8][1]= resultat.getString(2);  horaires_2[8][2]= resultat.getString(3);
    								  horaires_2[9][0]= resultat.getString(1);  horaires_2[9][1]= resultat.getString(2);  horaires_2[9][2]= resultat.getString(3);
    						
    						
    								  horaires_1[8][0]= resultat.getString(1);  horaires_1[8][1]= resultat.getString(2);  horaires_1[8][2]= resultat.getString(3);
    								  horaires_1[9][0]= resultat.getString(1);  horaires_1[9][1]= resultat.getString(2);  horaires_1[9][2]= resultat.getString(3);
    								  horaires_1[10][0]= resultat.getString(1);  horaires_1[10][1]= resultat.getString(2);  horaires_1[10][2]= resultat.getString(3);
    								  horaires_1[11][0]= resultat.getString(1);  horaires_1[11][1]= resultat.getString(2);  horaires_1[11][2]= resultat.getString(3);
    								  horaires_1[12][0]= resultat.getString(1);  horaires_1[12][1]= resultat.getString(2);  horaires_1[12][2]= resultat.getString(3);
    						
    								  
    								  horaires_2[10][0]= resultat.getString(1);  horaires_2[10][1]= resultat.getString(2);  horaires_2[10][2]= resultat.getString(3);
    								  horaires_2[11][0]= resultat.getString(1);  horaires_2[11][1]= resultat.getString(2);  horaires_2[11][2]= resultat.getString(3);}
    			}
    				
    				
    				catch (ClassNotFoundException e) {System.out.println(" il y a une erreur");	} 
    				catch (SQLException e) { System.out.println("ERREUR BDD"); }
    	
    	
    	
    	
    	
    	}
    	
    	
    	 void afficherLesHoraires(){
    	
    		 	
    			System.out.println("Le premier tableau : "); 
    			for(int i=0; i<13 ;i++){
    				for(int j=0; j<3 ;j++){ System.out.print(horaires_1[i][j]+ " |  "); }     System.out.println(" "); System.out.println(" ");}
    			
    			
    			System.out.println("Le deuxieme tableau : "); 
    			for(int i=0; i<12 ;i++){ for(int j=0; j<3 ;j++){ System.out.print(horaires_2[i][j] + " |  "); }System.out.println(" "); System.out.println(" ");}         
    			
    	 }
    			
    	
    	
    	 
    	 public static void main(String[] args) {
    
    		 new Z_Horaire().afficherLesHoraires();
    
    		
    	}}
    



    -
    Edité par Leila_10 15 août 2017 à 18:27:41

    • Partager sur Facebook
    • Partager sur Twitter
    Anonyme
      16 août 2017 à 9:43:07

      Salut,

      Quelle est l'erreur que tu rencontres ? Code d'erreur ? Message d'erreur ? Situation anormale ?

      • Partager sur Facebook
      • Partager sur Twitter
        16 août 2017 à 10:59:07

        Ça ne serait pas la dernière ligne de ton SELECT qui est récupéré uniquement ?

        Tu écrase les valeurs de ton tableau à chaque itération dans ta boucle.

        • Partager sur Facebook
        • Partager sur Twitter
          17 août 2017 à 15:33:09

          Salut,

          Oui c'est ça, il ne m'affiche que la dernière ligne de ma table 25 fois (25 = nbr de lignes de la table )

          Comment faire pour récupérer toute les données ? 

          Qu'est ce qui fait que les valeurs de mon tableau soient écrasés à chaque fois ? 

          • Partager sur Facebook
          • Partager sur Twitter
            17 août 2017 à 15:37:27

             while(resultat.next()){
                                       
                                       
                                              horaires_1[0][0]= resultat.getString(1);  horaires_1[0][1]= resultat.getString(2);  
            
            [...]
            
                                              horaires_2[11][0]= resultat.getString(1);  horaires_2[11][1]= resultat.getString(2);  horaires_2[11][2]= resultat.getString(3);}

            quand tu fait next() sur un ResultSet il te renvoie une ligne, je te conseille fortement d'aller revoir la partie du cour qui parle de jdbc, tu n'a visiblement pas compris le fonctionnement.

            -
            Edité par florent m 17 août 2017 à 15:38:02

            • Partager sur Facebook
            • Partager sur Twitter
              19 août 2017 à 13:42:34

              D'accord je vais revoir le cours. Merci :)
              • Partager sur Facebook
              • Partager sur Twitter
                22 août 2017 à 18:14:45

                J'ai finalement divisé le tableau en deux dans ma base de données et ça à donné le résultat que je voulais. Si toute fois quelqu'un a une meilleure solution (sans avoir à créer de nouvelles tables), vos suggestions sont plus que bienvenues :)

                try {	  Class.forName("org.mariadb.jdbc.Driver");
                		  connex = DriverManager.getConnection("jdbc:mariadb://127.0.0.1:3306/ecole?user=root&password=0557");
                
                		  Statement requete = connex.createStatement();
                		  
                		  
                		  // Le tableau des horaires en entier :
                		  ResultSet resultat = requete.executeQuery("select idHoraire, jour, plageHoraire from horaires");
                		  int i=0; int j=0;
                		  while(resultat.next()){ horaires[i][j]= resultat.getString(1);  
                		  						  horaires[i][j+1]= resultat.getString(2);  
                		  						  horaires[i][j+2]= resultat.getString(3);   i++;  }
                		  
                		  
                		  //La premiere moitiée du tableau des horaires :
                		  ResultSet resultat1 = requete.executeQuery("Select idHoraire, jour, plageHoraire from horaires1");
                		  i=0; j=0;
                		  while(resultat1.next()){ horaires_1[i][j]= resultat1.getString(1);  
                		  						   horaires_1[i][j+1]= resultat1.getString(2);  
                		  						   horaires_1[i][j+2]= resultat1.getString(3);   i++;  }
                		  
                		  
                		  // La seconde moitié du tableau des horaires :
                		  ResultSet resultat2 = requete.executeQuery("select idHoraire, jour, plageHoraire from horaires2");
                		  i=0; j=0;
                		  while(resultat2.next()){ horaires_2[i][j]= resultat2.getString(1);  
                		  						   horaires_2[i][j+1]= resultat2.getString(2);  
                		  						   horaires_2[i][j+2]= resultat2.getString(3);   i++;  }
                							    
                	}
                
                
                catch (ClassNotFoundException e) {System.out.println(" il y a une erreur");	} 
                catch (SQLException e) { System.out.println("ERREUR BDD"); }



                -
                Edité par Leila_10 22 août 2017 à 18:16:57

                • Partager sur Facebook
                • Partager sur Twitter

                Afficher les données d'une table MariaDB

                × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                • Editeur
                • Markdown