ArrayList to Byte and vice versa

Hi I need to store ArrayList in database of my project, for that I need some suggestion to store ArrayList into DB by means of some techniques like ArrayList to Byte[] and store byte[] in DB and retreive my byte[] from DB and convert into my original ArrayList in Java

Answers


I assume you're talking about Java's ArrayList (you didn't mention the language you have in mind).

In fact, there was a vary similar question: Serializing Arraylists

Just serialize a list, and insert it to BLOB column. Then you can fetch it from DB and deserialize it.

Edit:

This is a piece of code you can use to serialize your ArrayList:

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try { 
        ObjectOutputStream ooStream = new ObjectOutputStream(baos);
        ooStream.writeObject(myList);
        ooStream.close();
    } catch(IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            baos.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    byte[] serializedData = baos.toByteArray();

Having the data as byte[] you can now store it in bytea / BLOB column.

I hope this helps.


Need Your Help

How do I write a single-file Django application?

python django web-applications

I want to write a very small Django application in a single file, requiring all the appropriate modules and stuff, and then be able to run that as a normal Python script, like this: