Bonjour
Bonjour
J'essaie de créer une application qui récupère des données provenant d'une base de données Mysql et les affiche dans une JTable. Sauf que je reçois ce message d'erreur:
Voici mon code:java.sql.SQLException: Column index out of range.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
at com.mysql.jdbc.ResultSetMetaData.getField(ResultSetMetaData.java:402)
at com.mysql.jdbc.ResultSetMetaData.getColumnName(ResultSetMetaData.java:253)
at gesstock.MyTableModel.getColumnName(MyTableModel.java:131)
at javax.swing.JTable.addColumn(JTable.java:2801)
at javax.swing.JTable.createDefaultColumnsFromModel(JTable.java:1289)
at javax.swing.JTable.tableChanged(JTable.java:4386)
at javax.swing.JTable.setModel(JTable.java:3688)
at javax.swing.JTable.<init>(JTable.java:634)
at javax.swing.JTable.<init>(JTable.java:575)
at gesstock.PanListerUser.actualiser(PanListerUser.java:98)
at gesstock.PanListerUser.<init>(PanListerUser.java:68)
at gesstock.Accueil.<init>(Accueil.java:29)
at gesstock.Main.main(Main.java:17)
Pourtant le bout de code suivant :
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 public class MyTableModel extends AbstractTableModel { private String url="jdbc:mysql://localhost:3306/gesentrep_db"; private String user = "root"; private String passwd = "passer"; private Connection con; private ResultSet res; private Statement state; private ResultSetMetaData resultMeta; private Vector<String> columnNames = new Vector<String>(); // Each value in the vector is a row; String[] - row data; Vector data = new Vector(); private int rowCount; private long totalTime; public MyTableModel () { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException ex) { //JOptionPane.showMessageDialog(null,ex.getMessage()); JOptionPane.showMessageDialog(null,"Impossible de charger le pilote!"+ex.getMessage()); //JOptionPane.showMessageDialog(null,"Impossible de charger le pilote!"); } try { con = (java.sql.Connection) DriverManager.getConnection(url, user, passwd); } catch (SQLException ex) { JOptionPane.showMessageDialog(null,"Impossible d'établir la connexion!"+ex.getMessage()); } try { String requete = null; requete="select * from user "; //Création d'un objet Statement long start = System.currentTimeMillis(); state = con.createStatement(); //L'objet ResultSet contient le résultat de la requête SQL res = state.executeQuery(requete); //On récupère les MetaData resultMeta = res.getMetaData(); //On récupère les meta afin de récupérer le nom des colonnes ResultSetMetaData meta = res.getMetaData(); int nbrCol = meta.getColumnCount(); System.out.println("le nombre de colonne est:" +nbrCol); //On initialise un tableau d'Object pour les en-têtes du tableau Vector column = new Vector(nbrCol); // column.addElement("Case à cocher"); for(int i = 1 ; i <= nbrCol; i++) { System.out.println("le nom de colonne est:" +resultMeta.getColumnName(i)); column.addElement(meta.getColumnName(i)); } //Petite manipulation pour obtenir le nombre de lignes res.last(); rowCount = res.getRow(); Vector<Object> vector = new Vector<Object>(); // Object[][] data1 = new Object[rowCount][nbrCol+1]; //On revient au départ res.beforeFirst(); int j = 0; //On remplit le tableau d'Object[][] while(res.next()){ for(int i = 1 ; i <= nbrCol; i++) { vector.add(res.getObject(i)); } j++; } //On ferme le tout res.close(); state.close(); totalTime = System.currentTimeMillis() - start; columnNames = column; data = vector; } catch (SQLException ex) { JOptionPane.showMessageDialog(null,"Erreur"+ex); } } public int getColumnCount() { return columnNames.size(); } public int getRowCount() { return data.size(); } public String getColumnName(int col) { try { return resultMeta.getColumnName(col); } catch (SQLException ex) { ex.printStackTrace(); } return null; } public Object getValueAt(int row, int col) { return ((Vector) data.get(row)).get(col); } /* * JTable uses this method to determine the default renderer/ * editor for each cell. If we didn't implement this method, * then the last column would contain text ("true"/"false"), * rather than a check box. */ public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } /* * Don't need to implement this method unless your table's * editable. */ public boolean isCellEditable(int row, int col) { //Note that the data/cell address is constant, //no matter where the cell appears onscreen. return true; } /* * Don't need to implement this method unless your table's * data can change. */ public void setValueAt(Object value, int row, int col) { ((Vector) data.get(row)).setElementAt(value, col); fireTableCellUpdated(row, col); } public int nbrLigne() { return rowCount; } public long temps() { return totalTime; } public void removeRow(int row) { data.removeElementAt(row); } }
M'affiche ceci:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 System.out.println("le nombre de colonne est:" +nbrCol); for(int i = 1 ; i <= nbrCol; i++) { System.out.println("le nom de colonne est:" +resultMeta.getColumnName(i)); column.addElement(meta.getColumnName(i)); }
Mercile nombre de colonne est:4
le nom de colonne est:id_user
le nom de colonne est:nom
le nom de colonne est:prenom
le nom de colonne est:age
Partager