i tried searching at google using this key phrase "updating java objects in mysql" but i cant find a problem like what i have,
i have 2 classes, 1 that is serializable(Thesaurus class) and 1 that runs a simple program and saves the object of that class(Thesaurus) in my database.
heres the structure of the Thesaurus class
public class Thesaurus implements Serializable { private static final long serialVersionUID = 3781342618719432265L; private Map<String, Set> thesaurus; public Thesaurus() { thesaurus = new TreeMap<String, Set>(); } public void addThesaurusEntry(String word) { this.thesaurus.put(word, new TreeSet<String>()); } public void removeEntry(String word) { thesaurus.remove(word); } public Set getSynonyms(String word) { return thesaurus.get(word); } public Map getThesaurus() { return thesaurus; } }
here is the simple program (only the main method, i removed some of it to lessen the code, ill post the rest if necessary and requested for clearer analization)
public static void main(String[] args) { Database db = new Database(); db.connect(); Thesaurus thes = db.getThesaurus(); try { System.out.println(thes.getSynonyms("Word")); } catch (NullPointerException e) { System.err.println("Word Entry Does not Exist"); e.printStackTrace(); } Thesaurus newEntry = new Thesaurus(); newEntry.put(thes.getThesaurus()); newEntry.addThesaurusEntry("Word"); newEntry.addSynonym("Word", "Salita"); db.updateThesaurus(newEntry, thes); }
and here is the update method of my class that udpate's the object in the database
public void updateThesaurus(Object newValue, Object oldValue) { sql = "update thesaurus set wordEntry = ? where wordEntry = ?"; try { PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql); ByteArrayOutputStream baos1 = new ByteArrayOutputStream(); ObjectOutputStream oos1 = new ObjectOutputStream(baos1); ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); ObjectOutputStream oos2 = new ObjectOutputStream(baos2); oos1.writeObject(newValue); oos1.flush(); oos1.close(); baos1.close(); oos2.writeObject(oldValue); oos2.flush(); oos2.close(); baos2.close(); ps.setObject(1, baos1.toByteArray()); ps.setObject(2, baos2.toByteArray()); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
i tried to update the values in the database by 2 objects, the newValue and the oldValue which represents Thesaurus objects, but i dont see any changes, when i try to retrieve those.
i follow the flow step-by-step, but i cant modify the current object inside the database, am i missing something here? thanks in advance
id like to add how i expect with the flow of the program
this will retrieve a Thesaurus object from the database, which is always only one
Thesaurus thes = db.getThesaurus();
i need to create a new Thesaurus object because it will be used to compare which is the old and new value that will be used to update the database
and the second line newEntry.put retrieves the current map
Thesaurus newEntry = new Thesaurus(); newEntry.put(thes.getThesaurus()); // it gives a copy of the current map to this new thesaurus object
// i can add more keys on the map with this statement, in every run of the program, i can update the database // newEntry.addThesaurusEntry("Different word"); // but i cant add more different objects(String synonym) on the Set object of a certain key(word), database object doesnt update newEntry.addSynonym("Word", "Salita"); db.updateThesaurus(newEntry, thes);
what i want is, to modify the synonyms(Set) of any word on the Map(i.e i can add more and more, each run of the program)
it seems like the Set object of my Thesaurus' Map instance cannot update its value in the database.. please help, and im sorry for a long statement, im trying to state my problem as possible as i can, please help.. when i learned this interfaces, i found out that this is very handy, and i need to resolve this issue, because i might be using this in some of my projects in the future.