mardi 11 août 2015

How do I store and retrieve a BLOB type from sqlite?

I have a java class (Shell) that stores all the information I need to load for my application. Currently, it is saved into a .txt file and loaded from said file.

This class is responsible for saving and loading the Shell:

public void saveShell() throws IOException {
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(
            new FileOutputStream(getPath()));

    objectOutputStream.writeObject(new Date());
    objectOutputStream.writeBoolean(true);
    objectOutputStream.writeFloat(1.0f);

    objectOutputStream.writeObject(shl);
    objectOutputStream.flush();
    objectOutputStream.close();
    System.out.println("Successfully saved");
}

public Shell loadShell() throws FileNotFoundException, IOException, ClassNotFoundException {
    ObjectInputStream objectInputStream = new ObjectInputStream(
            new FileInputStream(getPath()));

    Date date = (Date) objectInputStream.readObject();
    System.out.println(date);
    System.out.println(objectInputStream.readBoolean());
    System.out.println(objectInputStream.readFloat());

    Shell readShell = (Shell) objectInputStream.readObject();
    System.out.println("Shell Loaded");

    objectInputStream.close();
    System.out.println("Object output stream closed");

    return readShell;
}

However, now that I'm trying to distribute the application, using a file seems less viable than using a database so I have already set up SQLite.

Since Shell has everything I need, I'd like to store it as a BLOB and be able to retrieve the object later. I am new to databases so I don't quite know the specific java methods that I should use. I know that this question is very similar to How do i store and retrieve a blob from sqlite but its answer is in C#. Plus, most examples I've found are always storing pictures and images with a certain URL, there aren't many examples with objects.

Aucun commentaire:

Enregistrer un commentaire