views:

994

answers:

6

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

I am from the lenient and forgiving world of PHP, where invoking the serialize() function and would convert the array into a string.

Is there an equivalent of doing such heresy in Java? Apart from normalization, are there more elegant ways of storing String Arrays in the database?

In case it's applicable, I am using the jdbc driver for my MySQL connections.

+2  A: 

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

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

This is different from PHP, whose serialize() converts everything into text.

You can also use the XML serialization in Java (XMLEncoder) but it's very verbose.

ZZ Coder
A: 

You don't want to serialize the array. I'm not sure why you'd serialize it in PHP either, because implode() and explode() would be more appropriate. You really should normalize your data, but aside from that, you could very easily Google a solution for converting an array to a string.

Droo
+4  A: 

If you're thinking in terms of raw arrays, you're still writing PHP in Java.

Java's an object-oriented language. An array of Strings really isn't much of an abstraction.

You'll get perfectly good advice here telling you that it's possible to serialize that array of Strings into a BLOB that you can readily store in MySQL, and you can tell yourself that leniency is a virtue.

But I'll going to remind you that you're losing something by not thinking in terms of objects. They're really about abstraction and encapsulation and dealing with things at a higher level than bare metal ints, Strings, and arrays.

It'd be a good exercise to try and design an object that might encapsulate an array or another more sophisticated data structure of child objects that were more than Strings. There'd be a 1:m relationship between parent and child that would better reflect the problem you were really trying to solve. That would be a far more object-oriented design than the one you're proposing here.

duffymo
A: 

If you really don't want to normalize this values into a separate table where each string would be in its own row, then just convert your array to a list of comma separated values (possibly escaping commas somehow). Maybe quoting each string so that "str1","str2".

Google for CSV RFC for spec on how this should be properly escaped.

A: 

But surely the more logical thing to do would be to save each string as its own record with a suitable identifier. That would probably be less coding than serializing -- a simple loop through the elements of the array -- and would result in a clean database design, rather than some gooey mess.

Jay
Jim: Thanks for the correction on "it's"! I often get a chuckle out of other people who make that sort of grammatical error -- like a grocery store around here that had a big sign in the window, "We want to be you're food store!" -- I laughed and lauged. And then I did it myself! I must hide my face in shame. :-(
Jay
Jay: I'm right there with you, love to laugh at these sorts of mistakes, but finding that I make them too! :-(
Jim Ferrans
+2  A: 

There are various good serialization/deserialization libraries that automatically convert JavaBean objects to/from XML and JSON strings. One I've had good experience with is XStream.

Java's built-in support for serialization can do the same thing, and you can write custom serialization/deserialization methods for Java to call.

You can roll your own serialization methods too, eg converting to and from a comma-separated value (CSV) format.

I'd opt for a library like XStream first, assuming there's a very compelling reason not to normalize the data.

Jim Ferrans