swiixz | Voici l'initialisation de mon tableau à parti de mon model :
Code :
- jTtableauclient = new JTable(new Modelclient());
|
et voici mon model:
Code :
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.swing.JOptionPane;
- import javax.swing.table.AbstractTableModel;
- public class Modelclient extends AbstractTableModel {
- Connection connexion = null,connexiongescom = null;
- Statement instruction = null,instructiongescom = null;
- ResultSet resultat = null,resultatgescom = null;
- Integer nbrechamp=1,index=0,nbreenreg=0,index2=0,index3=0;
- String[] columnNames;
- Object[][] data;
- public Modelclient() {
- try
- {
- Class.forName ("com.mysql.jdbc.Driver" ); // Chargement de la classe du driver JDBC de MySQL
- connexion = DriverManager.getConnection("jdbc:mysql://localhost/bdd_utilisateur" );// Ouverture de la connexion avec MySQL sur la base utilisateur
- instruction = connexion.createStatement();
- resultat = instruction.executeQuery("select count(*)FROM param_utilisateur where affichage='1'" );//combien de colonnes à afficher?
- resultat.next();
- nbrechamp=resultat.getInt("count(*)" );
- columnNames = new String[nbrechamp];
- resultat = instruction.executeQuery("select champ FROM param_utilisateur where affichage ='1'" );//selection des champs a afficher
-
- while(index<nbrechamp){ // initialisation des colonnes
- resultat.next();
- columnNames[index]=resultat.getString("champ" );
- index++;
- }
- connexiongescom = DriverManager.getConnection("jdbc:mysql://localhost/gescom2" );// Ouverture de la connexion avec MySQL sur la base utilisateur
- instructiongescom = connexiongescom.createStatement();
- resultatgescom = instructiongescom.executeQuery("select count(*)FROM client" );// combien d'enregistrement dans la table client?
- resultatgescom.next();
- nbreenreg=resultatgescom.getInt("count(*)" );
-
- resultatgescom = instructiongescom.executeQuery("select * FROM client" );
-
-
- data = new Object[nbreenreg][nbrechamp];
- while(index2<nbreenreg){ //initialisation des données
- resultatgescom.next();
- while(index3<nbrechamp){
- data[index2][index3]=resultatgescom.getString((index3+1));
- index3++;
- }
- index3=0;
- index2++;
- }
- }
- catch (ClassNotFoundException ex)
- {
- JOptionPane.showMessageDialog(null,"Classe introuvable " + ex.getMessage ());
- }
- catch (SQLException ex)
- {
- JOptionPane.showMessageDialog(null,"Erreur JDBC : " + ex.getMessage ());
- }
- finally
- {
- try
- {
- if (resultat != null)
- resultat.close();
- if (instruction != null)
- instruction.close();
- if (connexion != null)
- connexion.close();
- }
- catch (SQLException ex)
- {
- ex.printStackTrace ();
- }
- }
- fireTableStructureChanged();
- }
- public int getColumnCount() {
- return columnNames.length;
- }
- public int getRowCount() {
- return data.length;
- }
- public String getColumnName(int col) {
- return columnNames[col];
-
- }
- public Object getValueAt(int row, int col) {
- return data[row][col];
- }
-
- }
|
Au passage je commence tout juste le java alors je sais qu'il y a des trucs que je fais horriblement mal mais mon maitre de stage ne connais pas le java du coup je me retrouve tout seul sans personne pour m'aider.
Merci d'avance. |