Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to save an instance of a Serializable Java class to a MySQL database. As far as I know I have two options:

  1. Create a table that contains all fields as columns and re-create the object from the field-data saved in the database.
  2. Serialize the instance -> get the byte array -> save the byte array to the database -> re-create the instance from the byte array.

My Question: Which way works faster and which way needs less space?

And my second question: How would I easilly write and get the byte array from the mysql database using jdbc?

share|improve this question
    
How would I write the byte array to the database and read in again? –  MinecraftShamrock Jul 15 '13 at 19:34

1 Answer 1

up vote 0 down vote accepted

Saving the serialized byte array would maybe save space as you wouldn't have all of the meta data associated with table columns, headers, etc... Having said that, I don't know that there would be any noticeable difference in speed or storage space by saving the individual fields in columns in the database versus one column with the object bytes. If you're going to serialize it and save it, you might as well save it to a file and not use a database at all. Not to mention, as your objects and model change, loading older versions could be problematic. Maintainability would be a nightmare as well.

Personally, I'd never save a serialized byte array of an object in a database unless there was a very specific business case or reason to do so. I'd just create the table, columns, and persist it that way using JDBC or your favorite persistenace framework (like Hibernate). Saving it as a serialized byte array only seems to limit what you can do with the data. If you don't want to create the database, tables, columns, etc... then consider just serializing it and writing to a file. That would probably save some space and time as you wouldn't have to maintain a database server. Granted, the more objects you have, the more files, and the harder it would be to search and query that data.

TL;DR: I'd just create the database tables for the data you're trying to save. I don't see any noticeable benefits from saving it in a database as a serialized byte array.

share|improve this answer
1  
Thank you. I'll also have to display the data on a website and I cant deseerialize it in the web-server. I'll simply use the fields in the database. –  MinecraftShamrock Jul 15 '13 at 19:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.