Hi everyone
I am making a system where i have to refresh the database after updating it. I am using a JTable and a mysql database, but i have to close down the application so that the updates can be implemented.Is there a way of refreshing the connection session, or refreshing an entitymanager at real time rather than closing it down first?
Please Help Guys.
Thanks

Hi joe_ojah,

Have you try fireTableDataChanged(); and

class TableListener implements TableModelListener {
        
        public void tableChanged(TableModelEvent e) {
            for (int i=0; i< getRowCount(); i++) {
                for (int j=0; j< getColumnCount()-1; j++)
                    System.out.print(getValueAt(i, j) + "\t");
                System.out.println();
            }
        }
    }

or you have to do separated function which download vector. Do you use a vector or similar?

-make

hey i dont know how well u know to program in java. but it is advisible to use a vector or an Arraylist to grab the data from the database, then populate the JTable from the vector;
you can use a function:

private ArrayList getInformation()
{
   ArrayList<String> arraylist = null;
   
   resultSet = "query";
   while(resultSet.next)
   {
      // Declaring A Vector to hold that row of information
      Vector<String> vector = new Vector<String>;
      vector.add(resultSet.getString("Whatever")); //a field (say firstName)
      vector.add(resultSet.getString("Whatever")); // another field (say lastName)
      vector.add(resultSet.getString("Whatever")); // another field(say DOB)
      
      // adding that vector as a row in the arraylist
      arrayList.add(vector);
   }

   return arrayList;
}

to populate the JTable(Say in another class), use:

ArrayList<String> arrayList = getInformation();
JTable table = new JTable();
table.setModel(new defaultTableModel(arrayList.toString);

and to refresh the table:
in your actionPerformed method:
just repopulate the arrayList : ArrayList<String> arrayList = getInformation(); then reset the table Model: table.setModel(new defaultTableModel(arrayList.toString); NOTE: the table and arraylist has to be public to that class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.