permaculture playing cards*
The moose likes Swing / AWT / SWT and the fly likes Populating JTable from Oracle DB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Populating JTable from Oracle DB" Watch "Populating JTable from Oracle DB" New topic
Author

Populating JTable from Oracle DB

Vincent Martinez
Greenhorn

Joined: Sep 27, 2001
Posts: 3
I need some guidance on how I can populate a JTable from a DB. I think I may have to extend AbstractTableModel and retrieve the data in this implementation. Then pass that to the JTable? I am not sure. I just want to know the best way to show the users this table that has a fixed number or rows and cells. Any code reference or other ideas would be greatly appreciated. Thanks.
I already can connect to the DB and execute queries. I just don't know what to do with the recordset once I have it.
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Just what you said. Put the data into the Model and add it the the JTable.
Vincent Martinez
Greenhorn

Joined: Sep 27, 2001
Posts: 3
Do I need to extend the AbstractTabelModel or is there another TableModel I can use? Thanks for you help.
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
You could look at DefaultTableModel. Or you could just use the JTable constructor which take either Object[][] or Vector and leave it at that. It really all depends on what you have to do.
The Java tutorial on tables is pretty good. It should help you decide what path is best for your requirements.
Matt Hansen
Ranch Hand

Joined: Jul 31, 2001
Posts: 34
You can use the DefaultTableModel and then call setDataVector to populate it. See http://java.sun.com/j2se/1.3/docs/api/javax/swing/table/DefaultTableModel.html#set DataVector
Or, if your table is static just pass the data in using the constructor JTable(Object[][] rowData, Object[] columnNames) and then you don't have to worry about the model.
[This message has been edited by Matt Hansen (edited September 27, 2001).]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Populating JTable from Oracle DB