Search code examples
javadatabaseserializationooador-mapper

How to handle java.io.InvalidClassException?


Please tell me what i do this exception is coming and and i don't know how to remove it

java.io.InvalidClassException: applyonline.Applicant; local class incompatible: stream classdesc serialVersionUID = 8333391523914038903, local class serialVersionUID = -6432228733925744354

    public class Applicant implements Serializable{

        public String getId() {
            return id;
        }

        public String getPassword() {
            return password;
        }

        public void setId(String id) {
            this.id = id;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public Applicant() {
        }
        public boolean checkLogin(String id,String pasword) throws SQLException, IOException, ClassNotFoundException
        {
            db dbhandlerobj=new db();
            ArrayList<Object> appList;
 /*  ----> */   appList = dbhandlerobj.getObject("Applicant","ApplicantInfo"); /* <------*/
            for(Object obj: appList)
            {
                Applicant app=(Applicant)obj;
                System.out.println("ID:::::"+app.getId());
                if(app.getId().equals(id) && app.getPassword().equals(pasword))
                {
                    return  true;
                }
            }
            return false;
        }
        private String id;
        private String password;
    }

OR Mapper class

public class db {
    Connection con;
    String host="jdbc:derby://localhost:1527/Hamza";
    String userName="Hamza";
    String pasword="123";
    public db() throws SQLException {

        this.con =DriverManager.getConnection(host, userName, pasword);
    }
    public void storeObject(Object object,String name) throws IOException, SQLException
    {
        PreparedStatement ps;
        ByteArrayOutputStream baos =new ByteArrayOutputStream();
        ObjectOutputStream obos=new ObjectOutputStream(baos);
        obos.writeObject(object);
        obos.flush();
        obos.close();
        baos.close();
        byte []data=baos.toByteArray();
        String sql="insert into "+name+" values(?)";
        ps=con.prepareStatement(sql);
        ps.setObject(1,data);
        ps.executeUpdate();
    }
    public ArrayList<Object> getObject(String tableName,String columnName) throws SQLException, IOException, ClassNotFoundException 
    {
        PreparedStatement ps;
        String sql="select * from "+tableName;
        ps=con.prepareStatement(sql);
        ResultSet rs=ps.executeQuery();
        ArrayList<Object> studentList=new ArrayList<Object>();
        while(rs.next())
        {
            try (ByteArrayInputStream bais = new ByteArrayInputStream(rs.getBytes(columnName))) {
                ObjectInputStream obis;
                obis = new ObjectInputStream(bais);
                studentList.add((Object)obis.readObject()); //<----ERROR IN THIS METHOD
            }
        }        
        return studentList;
    }  
}**

Solution

  • You are using a different version of the Applicant class. Maybe you first wrote the Applicant class object to the database and after that, you modified the class. That's why you are getting this message.

    Write the object to the database again and then try to deserialize it.