• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question about JTable and populating a DB

 
Mahmoud Hadad
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
guys i'm so confused about this.do 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 JTable.so is there is anyway i can make a direct connection between the JDBC and JTable?
 
Kai Witte
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic