Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: updating Java Objects(Longblob) in mysql databasel problem

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default updating Java Objects(Longblob) in mysql databasel problem

    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.
    Last edited by chronoz13; February 11th, 2012 at 04:23 AM.


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: updating Java Objects(Longblob) in mysql databasel problem

    i dont think its about serialization, i was able to break down the problem and see that the TreeSet is the thing i need to deal with, i tried to minimize the Thesaurus class into this with only one collection object, a single Set/TreeSet object

    public class Thesaurus implements Serializable {
     
        private static final long serialVersionUID = 3781342618719432265L;
        private Set<String> setObject;
     
        public Thesaurus() {
     
            setObject = new TreeSet<String>();
        }
     
        public void addValue(String s) {
     
            setObject.add(s);
        }
     
        public Set getSet() {
     
            return setObject;
        }
     
        public void setSet(Set<String> set) {
     
            setObject = set;
        }
    }

    and the sample main program with this
     
    public static void main(String[] args) {
     
            Database db = new Database();
            db.connect();
            Thesaurus thes = db.getThesaurus();
     
            System.out.println(thes.getSet());
     
            Thesaurus newEntry = new Thesaurus();
            newEntry.setSet(thes.getSet());
            newEntry.addValue("Value 1"); // the next run will try to add "Value 2"
     
            db.updateThesaurus(newEntry, thes);
        }

    but no matter how i try, it doesnt modify the exisiting TreeSet object, what is the thing im missing?, by the way i just learned this from a book, and i added some practice with saving the things on a database.
    Last edited by chronoz13; February 11th, 2012 at 05:46 AM.

  3. #3
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: updating Java Objects(Longblob) in mysql databasel problem

    Quote Originally Posted by chronoz13 View Post
    i dont think its about serialization
    I'm not so sure. Your SQL UPDATE depends on the serialized bytes of your object being identical from run to run - that seems like it should be extremely fragile and unlikely to work very well for retrieval. How many rows does your PreparedStatement.executeUpdate tell you have been updated?

  4. The Following User Says Thank You to Sean4u For This Useful Post:

    chronoz13 (February 11th, 2012)

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: updating Java Objects(Longblob) in mysql databasel problem

    Why serialize the object into the database? A relational database is perfectly suited to hold thesaurus data, and scale much better than saving data with a serialized object (which as Sean mentioned, can be not only fragile but not very scalable or efficient)

  6. The Following User Says Thank You to copeg For This Useful Post:

    chronoz13 (February 11th, 2012)

  7. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: updating Java Objects(Longblob) in mysql databasel problem

    Why serialize the object into the database?
    oh thank you so much for enlightening me, atleast i know that i have some problem in designing my own program structure, hmmmm so how can i still use these things i learned in my design of thesaurus?

    - create a relational database
    - retrieve those data and put it all in a map?(sequence it, inside my java program?)
    - should i let my database be relational? and let my Java program handle all the collection?

    is this a good way?, this is what i was thinking this last night, but im not sure or ill say confused,

    and by the way, i was wrong blaming the TreeSet class, im still digging down why my serialized class is not working, i found that even i change those collection interfaces, whether i use List or ArrayList or Set or TreeSet alone in the Thesaurus class they still dont update themselves every retrieval,

    ill also leave a vague question and ill search a little bit while waiting for some answers, when and where should i do serializiation in programs? thank you so much again, that was a little relief

  8. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: updating Java Objects(Longblob) in mysql databasel problem

    i manage to solve the problem, this is my first time using this Collection interfaces, i didnt pay attention on my assignment statements,

    and i just cant add values on the Map or Set objects, i need to "REMAP", the whole entire map to a new one, before i can add

    this was my mistake, im just passing a reference copy from old to new
    currentMap = newMap;

    and i resovle it this way
    newMap.putAll(currentMap);

    and for the Map's Set value,
    i need to re-iterate and retrieve and set the current Set collection in the Map's value
    public void setMap(Map<String, Set> currentMap) {
     
            Map<String, Set> tempMap = new TreeMap<String, Set>();
     
            for (Map.Entry<String, Set> entry : currentMap.entrySet()) {
     
                Set<String> tempSet = new TreeSet<String>();
                tempSet.addAll(currentMap.get(entry.getKey()));
     
                tempMap.put(entry.getKey(), tempSet);
            }
     
            this.thesaurusMap.putAll(tempMap);
        }
    , i manage to resolve the problem, but again, thanks for advising me for a better program structure, i just wanted to solve this one. thanks so much
    Last edited by chronoz13; February 12th, 2012 at 05:12 AM.

Similar Threads

  1. Updating TableModel if changing data on DataBase(MySQL)
    By Farrukhjon in forum AWT / Java Swing
    Replies: 2
    Last Post: September 13th, 2011, 08:59 AM
  2. Problem with Java/MySQL query...
    By RiskyShenanigan in forum JDBC & Databases
    Replies: 2
    Last Post: March 28th, 2011, 03:50 AM
  3. Problem with updating empty JTable
    By byubi in forum AWT / Java Swing
    Replies: 1
    Last Post: May 15th, 2010, 02:06 AM
  4. Connectivity problem:MySQL and Java
    By Pragya in forum JDBC & Databases
    Replies: 2
    Last Post: January 22nd, 2010, 03:58 AM
  5. application Task problem - updating JTree
    By idandush in forum AWT / Java Swing
    Replies: 2
    Last Post: June 18th, 2009, 03:15 AM