how to write and read object into database

Posted on 2005-04-14
Last Modified: 2010-03-31
Iam using serializable to write/read object from/to database, but I switched my jdk to new version and Iam getting exceptions while reading the serialized object from my databse which is serialized by old jdk api. I want to know what is the problem, can I solve this problem by externalizable instead of serializable.
Question by:kyama1
    LVL 3

    Accepted Solution

    You cannot deserialize objects, that have changed over java versions. The reason is, that fields may have been changed within the objects.

    Sad to say, but there is no solution to your problem. Read the Java Documentation (the part of Serialisation), and you see, that thare is a serialId generated from the structure of the class. In new jdk, if class changes, the serialId changes, and you cannot read in old objects.

    If you want to persist objects over more than one version, you have to write your own representation objects, and store them (This is one way), or store the stuff in an XML Stream (you will for sure be able to read this thing in 150 years) :)
    LVL 3

    Expert Comment

    oh, one solution might be, to just add a static long named "serialId=<somebignumberofyourchoice>" to your class before serializing it. Then it deserializes with new jdks fine, AFAIK!
    LVL 15

    Assisted Solution

    Just replacing the JDK (without changing your code) and getting this exception means that your serialized objects were holding
    some JDK objects which changed their signature and didn't not have a serialVersionUID value. That is surprising because Java
    API code tend to have a serialVersionUID in its Serializable classes. Can you tell which class the exception points to?
    If you also changed your code (and this is the cause of the problem) then you can solve it in two ways.
    1. as Mig-O suggested add to your current class a serialVersionUID value (see
    for how to generate the value) which *was generated for your old version of the class*.
    2. implement "private void readObject( in) throws IOException, ClassNotFoundException" in your offending Serializable
    class or its Container and inside use the readFields method ( ) to read and populate the fields that you are interested in. (don't call super.defaultReadObject())

    LVL 15

    Expert Comment

    BTW, option 2 should be used together with option 1 if you are applying the change to the offending class itself and not to its parent class.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    FizzBuzz challenge 9 65
    java continue statement 10 52
    Unable to open debugger port in Intellij idea 6 38
    wordlen challenge 3 29
    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
    This video teaches viewers about errors in exception handling.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now