Firstly I have a database connection which is set up and works, now the problem- I have a search button which has an actionlistener connected and when it is clicked it is supposed to populate a JTable but the table is not being populated and I can't figure out why! Below shows a snippet of my connection and the code that tries to populate the table.
public void search()
{
btnSearch.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
//String name = txtname.getText();
String dataSourceName = "securitySystem";
String dbUrl = "jdbc:odbc:" + dataSourceName;
try{
//Type of connection driver used
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Connection variable or object param: dbPath, userName, password
Connection con = DriverManager.getConnection(dbUrl, "", "");
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery("select * from accesshistory");
String name = "";
DefaultTableModel model;
model = new DefaultTableModel();
tableAccess = new JTable(model);
model.addColumn("Full Name");
while(rs.next())
{
name = rs.getString("Name");
model.addRow(new Object[]{name});
}
statement.close();
con.close();
}catch (Exception e) {
try {
throw e;
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
Than you should make a Class that extends from `defaultTableModel` in this class you can overwrite methodes you need. such as `addColumn` and `addRow` if you want i can give you an example Class. – Stone Dec 09 '13 at 20:34