Populate a JTable from a ResultSet

Eagle_de_nord Posted messages 46 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>

2 answers

LaDiva432
 
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 :-)
0
Eagle_de_nord Posted messages 46 Status Member
 
thank you very much I will apply it right away and I will report the result to you...
0
Eagle_de_nord Posted messages 46 Status Member
 
Hello so here is what I did: 1) I created a class Jforme 2) the class contains an "OK" button to call the method in question (okActionPerformed) i.e. when I click OK it will display the result: as follows ... [then includes a lot of code in French mixed with HTML and Java] Thank you, the problem is that nothing shows in the table when I click the "OK" button... and I have no error message on NetBeans I only have "run: BUILD SUCCESSFUL (total time: 4 seconds)" thanks for your help
0
LaDiva432
 
Good evening,
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
0
Eagle_de_nord Posted messages 46 Status Member > LaDiva432
 
thank you LaDiva432 for your efforts :)
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 ...
0
LaDiva432 > LaDiva432
 
please! :)
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 ;)
0
guns65 Posted messages 38 Status Member
 
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
0
Eagle_de_nord Posted messages 46 Status Member
 
saam

i'm not sure about the answer, but well... it may be useful for something.

try to check if :

1) the classes are in the same package (otherwise do an import....).
2) the declaration of the tables and other variables ...
3) the instantiation of the variables...
0
KX Posted messages 19031 Status Moderator 3 020
 
Indeed an import should help:
import javax.swing.table.TableColumn;
0
oumeiima Posted messages 3 Status Member
 
LaDiva432, I followed your advice well, because I really needed to learn how to display the results of a query in a JTABLE
but I ran into a problem, that in the display, only the last row of my SQL table is shown, the other rows are not displayed!
0