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"}
};