jeudi 5 février 2015

How do I refresh a JList with updated SQLite database information whenever items are added/deleted?

I'm trying to make a program right now that will edit the Employee's info database using SQLite. On the left side of my frame I have a JList and in the menu I can add employee's, save/edit them, and delete them.


I have it set up to call this method when the frame is made:



try {
tempName = null;
query = "Select FirstName,LastName FROM EmployeeInfo";
pst4 = connection.prepareStatement(query);
rs4 = pst4.executeQuery();

while(rs4.next()){
tempName = rs4.getString("FirstName") + " " + rs4.getString("LastName");
listNames.add(tempName);
}
list.setListData(listNames);

pst4.close();
rs4.close();

}catch (Exception e1) {
JOptionPane.showMessageDialog(null, "Problem loading Employee List." + e1);
e1.printStackTrace();
}


and then after a Save, Delete, or Add I call this method to "refresh" it: (almost identical to the first method)



listNames.removeAllElements();
try {
tempName = null;
query = "Select FirstName,LastName FROM EmployeeInfo";
pst4 = connection.prepareStatement(query);
rs4 = pst4.executeQuery();

while(rs4.next()){
tempName = rs4.getString("FirstName") + " " + rs4.getString("LastName");
listNames.add(tempName);
}
list.setListData(listNames);

pst4.close();
rs4.close();

}catch (Exception e1) {
JOptionPane.showMessageDialog(null, "Problem loading Employee List." + e1);
e1.printStackTrace();
}


listNames is a Vector<String>


My delete and save buttons work find on updating the JList with this method, but for my add one it's having issues. I have it set up to open up a new JDialog Frame with a WindowListener on it. Whenever the window closes it calls the same exact method I've been using above but I get a NullPointException. It seems to work fine even with the error but I feel like there's a much better way to handle this.


Any help is appreciated.


Aucun commentaire:

Enregistrer un commentaire