如何处理java.io.InvalidClassException?

时间:2013-11-23 01:57:55

标签: java database serialization ooad or-mapper

请告诉我我做了什么这个例外即将到来,而且我不知道如何删除它

  

java.io.InvalidClassException:applyonline.Applicant;当地班级   不兼容:stream classdesc serialVersionUID = 8333391523914038903,   本地类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类

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;
    }  
}**

1 个答案:

答案 0 :(得分:2)

您使用的是不同版本的申请人类。也许您首先将Applicant类对象写入数据库,然后修改了该类。这就是你收到这条消息的原因。

再次将对象写入数据库,然后尝试反序列化。