Java: Serializing String [] Array for storing in MySQL database?

Yes, I know this is bad practice and I should normalize my tables instead. Put aside, is it possible to serialize a String [] array and store it in a database?

I am from the mild and forgiving world of PHP, where the serialize () function is called and converts an array to a string.

Is there an equivalent to such heresy in Java? Besides normalization, are there more elegant ways to store String Arrays in the database?

If applicable, I am using the jdbc driver for my MySQL connections.

+2


source to share


6 answers


Yes. You can serialize any Java objects and store the serialized data in MySQL.

If you are using normal serialization (ObjectOutputStream) the output is always binary. Even String is serialized to binary data. So you have to Base64 encode the stream or use a binary column like BLOB.



This is in contrast to PHP, which serialization () converts everything to text.

You can also use XML Serialization in Java (XMLEncoder), but it is very verbose.

+4


source


If you are thinking about raw arrays, you are still writing PHP in Java.

Java is an object oriented language. An array of strings is really not an abstraction.

You will get great advice telling you that it is possible to serialize this array of strings to BLOBs that you can easily store in MySQL, and you can tell yourself that forgiving is a virtue.



But I'm going to remind you that you are missing something by not thinking about items. They are really about abstraction and encapsulation and deal with things at a higher level than simple metals, strings and arrays.

It would be a good exercise to try and create an object that can encapsulate an array or other more complex data structure of child objects that were larger than strings. There would be a 1: m ratio between parent and child that better reflects the problem you were actually trying to solve. This will be much more object oriented design than the one you are suggesting here.

+4


source


There are various good serialization / deserialization libraries out there that will automatically convert JavaBeans to / from XML and JSON strings. I had a good experience: XStream .

Java's native support for serialization can do the same, and you can write your own serialization / deserialization methods to invoke Java.

You can also use your own serialization techniques, such as converting to and from comma delimited data (CSV) format.

I would choose a library such as XStream primarily in the belief that there is a very good reason not to normalize the data.

+2


source


You don't want to serialize the array. I'm not sure why you are serializing it in PHP as well, because implode () and explode () would be more appropriate. You should really normalize your data, but beyond that, you could very easily google a solution to convert an array to a string.

0


source


If you really don't want to normalize these values ​​in a separate table, where each row will be on its own line, then just convert your array to a comma-separated list of values ​​(perhaps avoiding the commas somehow). Perhaps quoting each line so that "str1", "str2".

Google for a CSV RFC for a specification of how this should be properly escaped.

0


source


But, of course, the most logical thing would be to store each line as its own record with a suitable identifier. This will probably be less coding than serialization - a simple loop through the elements of an array - and will lead to clean database design, not confusion.

0


source







All Articles