I have a JTable on a frame, whose contents i want to change when user chooses a different item in a combo box. The function that loads the table is as follows: -
CODE
public void populateSubCategories( String strSQL){
Vector columnNames=new Vector();
Vector data=new Vector();
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost/Pricing",
usr, pwd);
if(!con.isClosed()){
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(strSQL);
ResultSetMetaData md=rs.getMetaData();
int columns=md.getColumnCount();
for (int i=1;i<=columns;i++){
columnNames.addElement(md.getColumnName(i));
}
while(rs.next()){
System.out.println("records found..." + strSQL);
Vector row=new Vector(columns);
for(int i=1;i<=columns;i++){
row.addElement(rs.getObject(i));
}
data.addElement(row);
}
rs.close();
stmt.close();
tblSubMainCategories=new JTable(data,columnNames);
}
} catch(Exception e) {
System.err.println("Exception: " + e);
} finally {
try {
if(con != null)
con.close();
} catch(SQLException e) {}
}
}
What i want is to refresh the table with code as follows: -
CODE
populateSubCategories("SELECT * FROM sub_categories WHERE Id=" + Id)
The JTable only loads on the first call to this function and then never refreshes on subsequent calls with a different criteria. How should i make it refresh?