Hi!
Straight to the problem:
I'm building this server application which will monitor SQL databases. I want it to remember any number of connections so I've made a few methods which I thought would do the trick, except they don't.
The program should add any new SQL connection to a List<MyConnection>, then serialize it to a file "connections.txt", and finally to a JComboBox.
PROBLEM:
When I add the first connection, it works properly since the file needs to be generated.
Then I proceed to add a second one, and for that I found that I have to delete the file to generate a new one in it's place.
In theory, that one should contain both connections, but it only returns the first one to the JCB and it catches this error:
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: com.mysql.jdbc.SingleByteCharsetConverter
CODE:
This method saves the list to a file:
public void saveList(String name) {
File file = new File(name);
if (file.exists()) {
file.delete();
System.out.println("I deleted " + name);
}
try {
ObjectOutputStream oos;
try (FileOutputStream fos = new FileOutputStream(name)) {
oos = new ObjectOutputStream(fos);
Iterator it = Connectionslist.iterator();
while (it.hasNext()) {
oos.writeObject((MyConnection) it.next());
System.out.println("Serialized: " + (MyConnection) it.next());
}
fos.flush();
fos.close();
}
oos.flush();
oos.close();
} catch (Exception e) {
System.out.println("Problem serializing: " + e);
}
}
This method reads from the same file:
public void loadList(String name) throws FileNotFoundException, IOException, ClassNotFoundException {
Connectionslist.clear();
ObjectInputStream ois;
try (FileInputStream fis = new FileInputStream(name)) {
ois = new ObjectInputStream(fis);
Connectionslist.add((MyConnection) ois.readObject());
System.out.println("Deserialized: " + (MyConnection) ois.readObject());
fis.close();
}
ois.close();
}
And this one takes objects from the list and puts them in a combobox:
public JComboBox list2combobox(List list, JComboBox jcb) {
jcb.removeAllItems();
for (Object item : list) {
jcb.addItem(item);
}
return jcb;
}