0

Firstly I have a database connection which is set up and works, now the problem- I have a search button which has an actionlistener connected and when it is clicked it is supposed to populate a JTable but the table is not being populated and I can't figure out why! Below shows a snippet of my connection and the code that tries to populate the table.

public void search()
{
    btnSearch.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent ae) 
        {
            //String name = txtname.getText();

            String dataSourceName = "securitySystem";
            String dbUrl = "jdbc:odbc:" + dataSourceName;

            try{
                //Type of connection driver used    
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

                //Connection variable or object param: dbPath, userName, password
                Connection con = DriverManager.getConnection(dbUrl, "", "");

                Statement statement = con.createStatement();

                ResultSet rs = statement.executeQuery("select * from accesshistory");

                String name = ""; 

                DefaultTableModel model;
                model = new DefaultTableModel(); 
                tableAccess = new  JTable(model);

                model.addColumn("Full Name");

                while(rs.next())  
                {
                    name = rs.getString("Name");      
                    model.addRow(new Object[]{name});
                }

                statement.close();
                con.close();
            }catch (Exception e) {
                try {
                    throw e;
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }                       
        }
    });
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • you can try to add `model.fireTableDataChanged()` and `model.fireTableStrukruteChanged()` after the while loop in my personal opinion this isn't the best way to handl with `JTables` and `DefaultTableModel` – Stone Dec 09 '13 at 20:28
  • could you give me a direction in which to go from here then, what is the best practice in this scenrio? – martin doherty Dec 09 '13 at 20:29
  • as i learned it in school you should make a Database Class where you handy all SQL queries.
    Than you should make a Class that extends from `defaultTableModel` in this class you can overwrite methodes you need. such as `addColumn` and `addRow` if you want i can give you an example Class.
    – Stone Dec 09 '13 at 20:34
  • @Stone, you should NOT invoke a fireXXX() method. That is the responsibility of the TableModel. Your application should not invoke those methods directly. – camickr Dec 09 '13 at 20:39
  • @camickr really? until now i have alway done it. of course i have done it in the `TableModelClass` and not from the GUI. i am programming java since 3 years and nobody told me not to do so. also the teacher do so. – Stone Dec 09 '13 at 20:42
  • @Stone, that was my point, it should always be done in the TableModel class (so you are doing it correctly), not the application code. Your original suggestions was to use model.fireXXX(), which implies the code is not part of the TableModel. – camickr Dec 09 '13 at 20:43

2 Answers2

1

You seem to allocate new table component inside search() method. But this new table is not even added to a container. Do not reallocate the table component, instead use setModel() to refresh data. You can also update the existing model. See How to Use Tables for examples.

Also, for better user experience and performance do not execute long-running task such as database interaction on Event Dispatch Thread. Use an auxiliary thread, or a SwingWorker for that purpose. See Concurrency in Swing for details and examples.

tenorsax
  • 21,123
  • 9
  • 60
  • 107
  • 1
    +1, yes changes should be make to the model. The table.setModel(...) method should be invoked AFTER all the data from the ResultSet has been read to the table is updated all at once instead of once for every row. – camickr Dec 09 '13 at 20:42
0
  • First please for design purpose and robustness do never create a Database connection in an event handler would be better if added as a singleton and then called as static
  • Second take a look at this Post I guess what you are facing is an issue over JTableModel object declaration and allocation

Good luck

Community
  • 1
  • 1
Yehia Awad
  • 2,898
  • 1
  • 20
  • 31