Definitely don't use java.io.Serializable
. Just write binary data as-is to its target. Serializing would only add unnecessary overhead and would make the saved data unusable for other tools than Java.
I also wouldn't push it all in a single field in a single row. It makes it all tight coupled and storing/retrieving the individual entries may be more expensive. Rather store each in its own row. You can if necessary link/reference the one and other by another column with a foreign key.
Now the Java code, the JDBC API offers PreparedStatement#setBinaryStream()
to save binary data in flavor of an InputStream
into the database. There's also the setBytes()
method for the case you've it in a (memory hogging) byte[]
. Then, to retrieve it, you can just use either ResultSet#getBinaryStream()
or getBytes()
.
You can on the other hand also just store those files in the local disk file system the usual Java IO way using FileOutputStream
and read them using FileInputStream
. You can if necessary store file paths/names in the database. This decouples the binary data from the database which makes it less portable, but better reuseable for other purposes.
See also