How to store and retrive vectors in mysql database. I try to store vector using setObject() method in mysql BLOB column and I try to retrive the vector using Vector myVect = (Vector) rs.getObject("dbVect"); but there is ClassCastException.Please some one help me to solve this very urgent. Thanks
I had the same problem some time back. I solved it but I do not have the code with me now. So I give you a skeleton or a rough sketch of the code below. There may be a few syntax errors. Hope you can follow.
This does not work on my project. I store some objects into my vector and store it to my mysql db. That works sofar. If I try to read the vector from the db this error appears: java.io.InvalidClassException: java.util.Vector; local class incompatible: stream classdesc serialVersionUID = -2804478835259887871, local class serialVersionUID = -2767605614048989439 Would be great if somebody could help. Thanx!
The previous post was on the right track with ByteArrayOutputStream. But you do not want to use toString() on the baos variable. You should use toByteArray() instead. toString() will distort the information within the stream which is causing your invalid class exception.
If you're going to serialize, store the result as binary data. Never, ever, store the stuff as a string (or char(N) or varchar(N) or nchar(N) or nvarchar(N) or varchar2(N) or text or any other character-based data type). Character data can be subjected to character set conversions and what have you, and you can never be sure that the random binary goo you get back is the same as the random binary goo you put in. If you have to use some character type, then encode the serialized data, e.g. using Base64. - Peter
Joined: Jun 23, 2003
Well, I figured out that when I use baos.toString() I get the same as if I write it to a file and open it with the text editor. Saving/loading the vector to/from a file works. So baos.toString() can't be that wrong. In the mysql db I use the BLOB type (what else should I use?) for the vector. I also found out, that if I remove that line Vector v = (Vector) ois.readObject(); I'm getting NO error message. I read the vector from db like Mohamed Yousuff described it. [ June 24, 2003: Message edited by: Benny Stelter ]
Joined: Jun 23, 2003
Well I found a way now and it is that easy! Storing into db:
Reading from db:
Well I found an example like this on another forum and no one other than MARK MATTHEWS (MYSQL AB) posted it, so I think it should be the right way The methods setObject() and getObject() do the serializing stuff for you, so you don't have to mess around with streams, nice eh? [ July 03, 2003: Message edited by: Benny Stelter ]