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