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 95 96 97 98 99 100 101 102 103 104 105
|
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];
}
} |
Partager