File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes A question about JTable and populating a 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 "A question about JTable and populating a DB" Watch "A question about JTable and populating a DB" New topic

A question about JTable and populating a DB

Mahmoud Hadad
Ranch Hand

Joined: Jan 09, 2006
Posts: 67
Hi all,
guys i'm so confused about i need to implement my own model to extract data from my DB ?
is there is anyway beside using the setValueAt()method and change the row ,index and value to extract data from my DB ?because it seems to me alot of overhead which will affect my App perfomrance?
i want to extract data from a result set object and show it in a is there is anyway i can make a direct connection between the JDBC and JTable?
Kai Witte
Ranch Hand

Joined: Jul 17, 2004
Posts: 356

the more direct (coupled) this connection between table visualization and database table is, the less flexible and maintainable and the more complicated it is.

In many cases a logical record is stored as a row in a table. You would have an interface for that type, for example Person or Animal or House or Book. Your data tier should be designed independently from the presentation tier. Most likely the best choice is to use a persistence framework, for example Hibernate, but you can also implement all on your own, for example by using a DAO. Either way it is quite simple in most cases.

Create a custom TableModel. The simples way to do that is to extend AbstractTableModel, although one could argue about the purity of that approach. Implement the abstract methods of AbstractTableModel appropriately, and add a method to add a record of the type mentioned above (addPerson(Person p) or addAnimal(Animal a) or ...). Maybe you also need a method to get a record at a particular index?

And that's all.


Kai Witte's business website Kai Witte's private homepage
I agree. Here's the link:
subject: A question about JTable and populating a DB
It's not a secret anymore!