2

Hi,

I have database in MySql and want to get my table to be bind with Swing JTable. Now my DAO class retrieves data from my table and stores it into java.util.List. What approaches I could use to bind db table with JTable?

devger
  • 703
  • 4
  • 12
  • 26
  • There is no good way to do this, probably allot of complex answers to this but they could be simplified to that you have your application state located in 2 positions, rather just send updates every few minutes, but this doesn't scale nicely. – Derrops Jul 20 '15 at 02:45

2 Answers2

1

As you have data fetched from Database wrapped by DAO, using DAO put those information in relevant row/column of your JTable.

Here are SO Question and answer for your requirement. Hope they will help you.

Populate JTable Using List

Java GUI aplication, load data to Jtable from a list<objects>

How to add data to JTable created in design mode?

Other Resources.

http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

MadMike
  • 1,391
  • 1
  • 16
  • 38
Alpesh Gediya
  • 3,706
  • 1
  • 25
  • 38
  • Thanks, but I found that it is better to use JTable(tablemodel) constructor with connection to database. Does someone has an expirience with using TableModel? – devger May 26 '13 at 15:57
0
 Session sesion = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = null;
    try {
        tx = sesion.beginTransaction();
        List today = sesion.createQuery("FROM class WHERE something").list();
     for (Iterator iterator = today.iterator(); iterator.hasNext();){
        Salidas Sal = (Salidas) iterator.next(); 
        tablemodel.addRow(new Object[]{
            //`enter code here`columns
            Sal.getId(),
            Sal.getUsuarios().getNombre().toString(),
            Sal.getCantidadPrestada(),
            Sal.getCantidadPedida(),
            Sal.getFechaSalida()});
        }
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    } finally {
        sesion.close();
    }