Dear Listers,
I have create a project in java using netbeans. Have designed the user interface where I have a jtable and a button.
When i created the interface it automatically populated the code and I have done nothing
When i click the button it shoiuld call the class to connect to the database and execute the sql query and put it in an array list and then populate the jtable.But for some reason this is not happening I have tried a lot and desperately looking for help. This is the code

private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
// HashMap<Integer,Counselor> results = new HashMap<Integer, Counselor>();
ArrayList<Counselor> users = new ArrayList<Counselor>();
Statement st = null;
ResultSet rs = null;

    Connection con = ContactEditor.getConnection();
    Counselor u;
    try
    {
    st = con.createStatement();
    rs= st.executeQuery("Select * from counselor");
   // jTable1.setModel(DbUtils.resultSetToTableModel(rs));

    while(rs.next())
    {
    Integer id = rs.getInt("id");
            String firstName = rs.getString("firstName");
            String nickName = rs.getString("nickName");
            String lastName = rs.getString("lastName");
            String telephone = rs.getString("telephone");
            String email = rs.getString("email");
            Date memberSince = rs.getDate("memberSince");

            u = new Counselor(id,firstName,nickName,lastName,telephone,email,memberSince);
           // results.put(id, u);
            users.add(u);                
    }                       

    }
    catch(SQLException ex)
    {
        ex.printStackTrace();
    }

    DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
  //  System.out.println("The default model is"+ model.getColumnCount());
    Object[] rowData  = new Object[model.getColumnCount()];
    for(int i=0; i<users.size(); i++)
    {
        //System.out.println("The user size is"+ users.size());
        rowData[0] = users.get(i).getId();
        rowData[1] = users.get(i).getFirstName();
        rowData[2] = users.get(i).getNickName();
        rowData[3] = users.get(i).getLastName();
        rowData[4] = users.get(i).getTelephone();
        rowData[5] = users.get(i).getEmail();
        rowData[6] = users.get(i).getMemberSince();

        System.out.println("Reached here");

        model.addRow(rowData);
    }

    //jTable1.setModel(model);
}                                       
  • for why reason(s) there is/are two arrays, models, ArrayList and DefaultTableModel

  • add records from database to DefaultTableModel directly, then to add model to JTable

  • search for TableFromDatabase, ResultSetTableModel

I am able to crack the solution. this for the benefit of people who have already posted such kind of questions in the forum already.
Here is the solution:

 Connection con = ContactEditor.getConnection();
        Counselor u;
        try
        {
        st = con.createStatement();
        rs= st.executeQuery("Select * from counselor");
       //jTable1.setModel(DbUtils.resultSetToTableModel(rs));
        while(rs.next())
        {


        Integer id = rs.getInt("id");
                String firstName = rs.getString("firstName");
                String nickName = rs.getString("nickName");
                String lastName = rs.getString("lastName");
                String telephone = rs.getString("telephone");
                String email = rs.getString("email");
                Date memberSince = rs.getDate("memberSince");

                u = new Counselor(id,firstName,nickName,lastName,telephone,email,memberSince);
               // results.put(id, u);
                users.add(u);                
        }                       

        }
        catch(SQLException ex)
        {
            ex.printStackTrace();
        }

        DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
        //System.out.println("The default model is"+ model.getColumnCount());
         model.setRowCount(users.size());
        int row =0;
         for(Counselor data: users)
           {
               model.setValueAt(data.getId(), row, 0);
               model.setValueAt(data.getFirstName(), row, 1);
               model.setValueAt(data.getNickName(),row, 2);
               model.setValueAt(data.getLastName(), row, 3);
               model.setValueAt(data.getTelephone(),row,4);
               model.setValueAt(data.getEmail(), row, 5);
               model.setValueAt(data.getMemberSince(), row, 6);

            row++;   
           }

          jTable1.setModel(model);
         ColumnAdjuster ca = new ColumnAdjuster(jTable1);
         ca.adjustColumns();

    }

Once I changed the above code it works and all the data that is in the database is brought into the jtable.

as shown in the attachment.
If there are any better solutions please advise.

regards
Vilas dbsearch_results.pngdbsearch_results.pngdbsearch_results.png

This question has already been answered. Start a new discussion instead.