aspose file tools
The moose likes JDBC and the fly likes Store Vector in MySql database Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Store Vector in MySql database" Watch "Store Vector in MySql database" New topic
Author

Store Vector in MySql database

Nikhilesh Reddy
Greenhorn

Joined: Jun 27, 2001
Posts: 1
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

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.

Vector v = new Vector() ;
v.addElement("Test") ;
ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
ObjectOutputStream oos = new ObjectOutputStream( baos ) ;
oos.writeObject( v ) ;
oos.flush() ;
oos.close() ;
"Insert into test (obj) values( '" + baos.toString() + "' ) "
"select obj from test"
execute query
String str = resultset.getString("obj") ;
ByteArrayInputStream bais = new ButeArrayInputStream( str.getBytes() )
ObjectInputStream ois = new ObjectInputStream( bais ) ;
Vector v = (Vector)ois.readObject() ;

Now you get the original vector

------------------
Benny Stelter
Greenhorn

Joined: Jun 23, 2003
Posts: 4
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
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
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
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
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
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Store Vector in MySql database
 
Similar Threads
Store Java object in MySQL Database
Obtain fresh records from database for next and previous buttons
retrieve table name
How to store PublicKey and PrivateKey in database??
How to store PublicKey and PrivateKey in database??