0

I'm struggling to find out how to add the data from my MySQL database to my Table Model (which extends AbstractTableModel) using Netbeans. At the moment I can retrieve the data and display it in the Netbeans output window, but I can't, for the life of me, seem to be able to display it in my table model! I've hard coded some dummy data in a 2D object array and that displays without any hassles. Can someone...anyone please advise me on how I can achieve this. Thanks in advance.

switch (button) {

        case "All":
            try {
                String sql = "SELECT * FROM Employee";
                ResultSet rs = st.executeQuery(sql);

                while(rs.next()) {
                   String eid = rs.getString(1); 
                   String efname = rs.getString(2); 
                   String elname = rs.getString(3); 
                   String etype = rs.getString(4);

                   MyEmployeeTableModel model = new MyEmployeeTableModel();
                   model.getData(eid, efname, elname, etype);
                }

                scrollpane.setVisible(true);

            } catch (Exception ex) {}                
            break;

class MyEmployeeTableModel extends AbstractTableModel {

String eid;
String efname;
String elname;
String etype;

public void getData(String eid, String efname, String elname, String etype) {
    this.eid = eid;
    this.efname = efname;
    this.elname = elname;
    this.etype = etype;

    printDetails();
}

public void printDetails() {
    System.out.println("ID: " + eid + ", " + "Firstname: " + efname + ", " + "Lastname: " + elname + ", " + "Type: " + etype);
}

String[] columnNames = {"ID", "Firstname", "Lastname", "Type"};

Object[][] data = {
    {"101", "Joe", "Blogs", "Other"},
    {"102", "Mary", "Jane", "Salesperson"},
    {"103", "John", "Doe", "Other"},
    {"104", "Kathy", "Bates", "Other"},
    {"105", "Jim", "Beam", "Salesperson"},
    {"106", "Sue", "Black", "Other"},
    {"107", "James", "White", "Other"},
    {"108", "John", "Brown", "Salesperson"},
    {"109", "Martha", "Smith", "Other"},
    {"110", "Lisa", "McDonald", "Salesperson"}
};
Marc Delisle
  • 8,879
  • 3
  • 29
  • 29
PBT
  • 21
  • 2
  • You haven't included your `getValueAt` method in your question, but if, as I suspect, it's just returning elements from `data`, and you never change that array, then it shouldn't surprise you that the JTable's rows never change. – VGR Nov 05 '15 at 21:44
  • Is there a way for me to add my eid, efname, elname and etype variables to the data array so I can display it in the Jtable rows, or am I going about it the wrong way? – PBT Nov 08 '15 at 23:45
  • An array cannot be resized. You will need to use a List implementation, such as [ArrayList](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html). – VGR Nov 09 '15 at 00:22

0 Answers0