Populate a JTable from a ResultSet
Eagle_de_nord
Posted messages
46
Status
Member
-
oumeiima Posted messages 3 Status Member -
oumeiima Posted messages 3 Status Member -
Hello,
Here I’d like to have an idea about filling a JTable from a SQL SELECT query and of course by using a ResultSet() ...
I’ve seen on the net that you need to create a TableModel but I didn’t understand the principle...
thanks for guiding me ...
thanks in advance for your help
<config>Windows 7 Firefox 12.0</config>
Here I’d like to have an idea about filling a JTable from a SQL SELECT query and of course by using a ResultSet() ...
I’ve seen on the net that you need to create a TableModel but I didn’t understand the principle...
thanks for guiding me ...
thanks in advance for your help
<config>Windows 7 Firefox 12.0</config>
2 answers
Hello,
I’ll try to summarize the code in steps:
1- in your method you declare objects in which you return the records from the database (one object for each field) e.g.:
Col1 = new String[100];
Col2 = new String[100];
....
2- you declare an object data of type Object as follows:
data = new Object[200][N]; // N being the number of columns of your JTable 200 rows
3- you open your connection to the database ... (Class.forName.. and the accompanying chatter)
4- the query
5- you retrieve the query result into a ResultSet
// stmt being the preparedStatement
ResultSet rs=stmt.executeQuery();
while (rs.next()) {
Col1 [j] = rs.getString("Champ1");
Col2[j] = rs.getString("Champ2");
... // for as many columns as you want to return
data[j][i]= Col1 [j];
data[j][i+1]= Col2[j];
....
j++;
}
// the column headers :
String title[] = {"La colonne 1", "La colonne 2",....};
JTable table = new JTable(data, title);
JPanel tableau = new JPanel (new BorderLayout ());
// add the headers to the north of the container
tableau.add (table.getTableHeader (), BorderLayout.NORTH);
tableau.add (table, BorderLayout.CENTER);
JSplitPane split3 = new JSplitPane (JSplitPane.VERTICAL_SPLIT, true, tableau, new JTextArea (" Avec entête "));
TableColumn column = null;
for(int i=0;i<4 ; i++){
column= table.getColumnModel().getColumn(i);
column.setPreferredWidth(180);
}
P.add(tableau); // P being the panel of the window
// a scrollPane if needed
JScrollPane scrollpane = new JScrollPane(P,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
this.getContentPane().add(scrollpane);
6- you finish with a catch ....
There isn’t anything simpler :-)
I’ll try to summarize the code in steps:
1- in your method you declare objects in which you return the records from the database (one object for each field) e.g.:
Col1 = new String[100];
Col2 = new String[100];
....
2- you declare an object data of type Object as follows:
data = new Object[200][N]; // N being the number of columns of your JTable 200 rows
3- you open your connection to the database ... (Class.forName.. and the accompanying chatter)
4- the query
5- you retrieve the query result into a ResultSet
// stmt being the preparedStatement
ResultSet rs=stmt.executeQuery();
while (rs.next()) {
Col1 [j] = rs.getString("Champ1");
Col2[j] = rs.getString("Champ2");
... // for as many columns as you want to return
data[j][i]= Col1 [j];
data[j][i+1]= Col2[j];
....
j++;
}
// the column headers :
String title[] = {"La colonne 1", "La colonne 2",....};
JTable table = new JTable(data, title);
JPanel tableau = new JPanel (new BorderLayout ());
// add the headers to the north of the container
tableau.add (table.getTableHeader (), BorderLayout.NORTH);
tableau.add (table, BorderLayout.CENTER);
JSplitPane split3 = new JSplitPane (JSplitPane.VERTICAL_SPLIT, true, tableau, new JTextArea (" Avec entête "));
TableColumn column = null;
for(int i=0;i<4 ; i++){
column= table.getColumnModel().getColumn(i);
column.setPreferredWidth(180);
}
P.add(tableau); // P being the panel of the window
// a scrollPane if needed
JScrollPane scrollpane = new JScrollPane(P,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
this.getContentPane().add(scrollpane);
6- you finish with a catch ....
There isn’t anything simpler :-)
Hi :) I followed what you wrote but it shows the following error!!
java:219: error: cannot find symbol
TableColumn column = null;
^
could someone help :) thank you
java:219: error: cannot find symbol
TableColumn column = null;
^
could someone help :) thank you
Yes, that is totally normal! you have to create the JTable inside the try just after the while! and it is only after that you add the catch!
So move all the code from the creation of the JTable up to the addition of the ScrollPane into the try. Then chain with the catch ...
And normally it should work ;)
Good luck
so I fixed what you had pointed out,
but despite that it still doesn’t work .. yet with System.out.println(data [0][0] + " " + data [0][1] ); it prints the result of the query correctly ...
i do not see the declaration of the string tilte (attribute of JTable in your code) ! check it, it may be that the error comes from there ;)