bonjour tout le monde
je veux afficher le résultat d'une requéte à une base de données à une JTable.
j'ai réalisé la classe de connection à la base qui me renvoie toutes les données nécessaires au modèle.
voici son 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 package test_jdbc; import java.sql.* ; import java.util.* ; public class MySqlTest { private String driver="com.mysql.jdbc.Driver";//"org.gjt.mm.mysql.Driver"; private String bridge="jdbc:mysql://localhost:3306/"; private String name ="hicham"; private Connection con; private ResultSetMetaData rsm; private ResultSet rs; private Statement stmt; private int nbrCol; private int nbrLg; private Object valAt; private String query; private String tableName; /** Creates a new instance of MySqlTest */ public MySqlTest() { try{ Class.forName(driver); String url=bridge+name; con=DriverManager.getConnection(url,"root",""); stmt=con.createStatement(); System.out.println("connection etablie"); } catch(ClassNotFoundException e){ System.out.println("erreur lors du chargement du pilote:"+e); } catch(SQLException e){ System.out.println("la connection a échoué :"+e.getMessage()); while(e != null){ System.out.print("Message :"+e.getMessage()); System.out.print(", Etat :"+e.getSQLState()); System.out.println(", Code :"+e.getErrorCode()); e.getNextException(); } } } public int getNbCol(){ return nbrCol; } public Object getValAt(int row,int col){ valAt=null; testSelect(query,tableName); try{ for(int i=0; i<row;i++) rs.next(); valAt=rs.getObject(col); } catch(SQLException e){ System.out.println("erreur lors de la récupération de l'élément("+row+","+col+"):"+e.getMessage() ); } return valAt; } public ResultSet getResultSet(){ return rs; } public int getNbLig(){ nbrLg=0; ResultSet rs1=rs; try{ while(rs1.next()) nbrLg++; } catch(SQLException se){} return nbrLg; } public String getColumnName(int index){ String cname=null; try{ if(index>=0 && index<getNbCol()) cname=rsm.getColumnName(index); } catch(SQLException se){} return cname; } public String getColumnClassName(int index){ String cname=null; try{ if(index>=0 && index<getNbCol()) cname=rsm.getColumnClassName(index); } catch(SQLException se){} return cname; } public void testSelect(String query,String tableName){ this.query=query; this.tableName=tableName; try{ rs=stmt.executeQuery(query); //nbrLg = rs.length; rsm = rs.getMetaData(); nbrCol=rsm.getColumnCount(); } catch(Exception e){ System.out.println("erreur :"+e.getMessage()); } } public void close(){ try{ stmt.close(); rs.close(); con.close(); } catch(SQLException e){} } }
et dans mon modèle j'instancie cette classe et je définis les méthodes abstraites de AbstractTableModel :
normalement quand j'associe le modéle à la table ça devrait m'afficher mes données mais c'est pas le cas.voici la classe de JTable:
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 import java.sql.ResultSet; import java.util.Vector; import javax.swing.table.AbstractTableModel; public class DataTableModel extends AbstractTableModel { private MySqlTest mst; // private Vector data; private Vector<String> columnNames; private String tableName; private String[] headers; private ResultSet rs; private Vector<String[]> data; /** Creates a new instance of DataTableModel */ public DataTableModel() {//ajouter un paramètre ResultSet mst=new MySqlTest(); executeSelect("select * from compte","compte"); rs=mst.getResultSet(); data=new Vector<String[]>(); setData(); System.out.println(getRowCount()+","+getColumnCount()); System.out.println("l'element 2,3:"+getValueAt(2,3)); System.out.println("colonne 0:"+getColumnName(1)); System.out.println("colonne 0 de type:"+getColumnClassName(1)); for(int i=0; i<data.size(); i++){ for (int j=0; j<data.get(i).length; j++){ System.out.print(data.get(i)[j]+","); } System.out.print("\n"); } } public void setData(){ String[] row; row=new String[getColumnCount()]; for(int i=0; i< getRowCount(); i++){ for(int j=0; j<getColumnCount(); j++){ row[j] = getValueAt(i+1,j+1).toString(); } data.add(row); } } /** exécution d'une requéte select */ public void executeSelect(String query,String table){ System.out.println("execution de la requéte "+query); mst.testSelect(query,table); } /** retourne la valeur d'un champ dans une ligne */ public Object getValueAt(int rowIndex, int columnIndex) { return mst.getValAt(rowIndex,columnIndex); } /** retourne le nombre de ligne en executant un select count(*) */ public int getRowCount() { return mst.getNbLig(); } /** retourne le nombre de colonnes de la table */ public int getColumnCount() { return mst.getNbCol(); } public String getColumnName(int ind){ return mst.getColumnName(ind); } public String getColumnClassName(int ind){ return mst.getColumnClassName(ind); } public boolean isCellEditable(int row,int col){ return false; } }
est ce que vous pouvez m'indiquer l'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 public class TestJTable extends javax.swing.JFrame { DataTableModel dtm; /** Creates new form TestJTable */ public TestJTable() { dtm=new DataTableModel(); initComponents(); jTable1.setModel(new DataTableModel()); //jTable1.createDefaultColumnsFromModel(); //System.out.println(jTable1.getAutoCreateColumnsFromModel()); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc=" Generated Code "> private void initComponents() { dataTable = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTable1.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null} }, new String [] { "Title 1", "Title 2", "Title 3", "Title 4" } )); dataTable.setViewportView(jTable1); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(dataTable, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 375, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap(15, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() .addContainerGap(14, Short.MAX_VALUE) .add(dataTable, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 275, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); pack(); }// </editor-fold> /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new TestJTable().setVisible(true); }[IMG]C:\Documents and Settings\Hicham-PC\Bureau\testJTable.jpg[/IMG] }); } // Variables declaration - do not modify private javax.swing.JScrollPane dataTable; private javax.swing.JTable jTable1; // End of variables declaration }
merci d'avance.
Partager