I am using phpMyAdmin through xampp as my host, I can add data just fine but when I run the application it doesn’t show any data. Could someone please check my code. I am new to programming in java so please bear with me.
public DefaultTableModel getmodel(){
DefaultTableModel model = (DefaultTableModel) jtbl.getModel();
model.setRowCount(0);
show_data_in_jtable();
return null;
}
public ArrayList<User> getuserList()
{
ArrayList<User> userList = new ArrayList<User>();
Connection Conn2 = m2.getConnection();
String query = "select * from Student ";
Statement st;
ResultSet rs;
try{
st = Conn2.createStatement();
rs = st.executeQuery(query);
User user_var;
while(rs.next())
{
user_var = new User(rs.getInt("id"),rs.getString("fname"),rs.getString("lname"),rs.getString("mname"),rs.getInt("age"),rs.getString("Gender"),rs.getString("course"));
userList.add(user_var);
}
}
catch (Exception error)
{
error.printStackTrace();
}
return userList;
}
// display data on Jtable
public void show_data_in_jtable()
{
ArrayList<User> list = getuserList();
DefaultTableModel model = (DefaultTableModel) jtbl.getModel();
Object[] column = new Object[5];
for(int i = 0; i < list.size(); i++)
{
column [0] = list.get(i).getId();
column [1] = list.get(i).getFirstName();
column [2] = list.get(i).getLastName();
column [3] = list.get(i).getLastName();
column [4] = list.get(i).getMiddleName();
column [5] = list.get(i).getGender();
column [6] = list.get(i).getCourse();
model.addRow(column);
}
}
2
Answers
Did you set your model to a Jtable?
If you did that what is result of getuserList() function? Did you debug your code?
Check this How to Use Tables
This isn’t really an answer to your question, as I think your problem stems from not adding the model correcting to the table, however I would encourage you to not use the DefaultTableModel. Create your own model by extending AbstractTableModel, it will make your life easier in the long run: