File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTable won't display records from database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable won Watch "JTable won New topic
Author

JTable won't display records from database

Michael Monchu
Greenhorn

Joined: Oct 20, 2010
Posts: 16
Hi

I am trying to display data from the database on the table, using the Vector approach. I have verified that the Vector is loaded by printing the data on the console. I am not sure why the table won't display the records.
Below is the code:


I would appreciate any form help or advice to a link where I can get help.

Regards
Michael
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Moved to the Swing forum. Please UseCodeTags when posting code. You can edit your post with the button.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
Once a TableModel has been added to the table all updates to the model should be done through the TableModel.

So your code should be restructured to look something like:



Your populateTable() method would need to change to create and then return the DefaultTableModel.
Michael Monchu
Greenhorn

Joined: Oct 20, 2010
Posts: 16
Hi Rob

I have modified the code the way you suggested, and it still doesn't display.



I don't know what is it that I am doing wrong. Please help.

Regards
Michael
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
See Table From Database.

The first part you don't need to worry about (unless you want to try that more complicated solution).

However, the second part contains the "Table From Database Example", which does exactly what you are trying to do. All you need to do is customize the SQL and it should work. Once you get that example working you can compare that code with your code to see what the different is.

If you need more help then you need to post a SSCCE. Of course in your SSCCE you can't include the database code, so you would replace that method with hard coded data that creates the DefaultTableModel. Once you get it working with hard coded data you should be able to replace that code with the SQL code.
gorgoro rogers
Greenhorn

Joined: Feb 10, 2011
Posts: 3
Michael Monchu wrote:Hi

I am trying to display data from the database on the table, using the Vector approach. I have verified that the Vector is loaded by printing the data on the console. I am not sure why the table won't display the records.
Below is the code:


I would appreciate any form help or advice to a link where I can get help.

Regards
Michael


replace this and try
public void populateTable(){
ResultSet res;

String select = "Select * from LIBRARY.TABLE";<-------------------------------------------String select
try{
PreparedStatement rtvStmt = conn.prepareStatement(select);
res = rtvStmt.executeQuery(select);<-----------------------------------------------Put here String select
ResultSetMetaData md = res.getMetaData();
int columnCount = md.getColumnCount();


you send an empty executequery you have to fill it .try it .replace also were you have to
Michael Monchu
Greenhorn

Joined: Oct 20, 2010
Posts: 16
Hi Rob

I used the example that you have provided, and it's working perfectly. Thanks a lot.

Regards
Michael
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable won't display records from database