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
Mohamed Yousuff
Ranch Hand
Joined: Jun 23, 2001
Posts: 73
posted
0
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!
Mike Alfree
Greenhorn
Joined: Jun 23, 2003
Posts: 1
posted
0
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.
Benny Stelter
Greenhorn
Joined: Jun 23, 2003
Posts: 4
posted
0
Thanx for help, but now this message appears: java.io.StreamCorruptedException: invalid stream header That streaming stuff is confusing me a little bit
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
posted
0
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
Benny Stelter
Greenhorn
Joined: Jun 23, 2003
Posts: 4
posted
0
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 ]
Benny Stelter
Greenhorn
Joined: Jun 23, 2003
Posts: 4
posted
0
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 ]