0

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.

Community
  • 1
  • 1
Maslor
  • 1,821
  • 3
  • 20
  • 44
  • What is the problem exactly? Setting up the sqlite table to store blobs, the needed SQL or how to use the JDBC to store blob ... or how to get blob data from a pojo and reconstruct pojo from blob? For the latter 2 see "Serialization". – Fildor Aug 11 '15 at 10:09
  • I know how to setup the database from the terminal using standard SQL commands, the problem is that I don't know how to use it from a java application. I would like a sample code so I could get an idea. Kind of like the post i mentioned. – Maslor Aug 11 '15 at 10:20

1 Answers1

0

I got it working. Basically I added two functions: saveShellDB and loadShellDB:

public void saveShellDB() throws ClassNotFoundException, IOException { 
        Class.forName(classForName); 
        Connection connection = null; 
        try 
        { 
          // create a database connection 
          connection = DriverManager.getConnection(connectionPath); 
          Statement statement = connection.createStatement(); 
          statement.setQueryTimeout(30);  // set timeout to 30 sec. 
          File file = new File("shell.txt"); 
          FileInputStream fis = new FileInputStream(file); 
          PreparedStatement ps = connection.prepareStatement("INSERT INTO shell (name, shl) VALUES (?, ?)"); 
          ps.setString(1, file.getName()); 
          ps.setBinaryStream(2, fis, (int)file.length()); 
          ps.executeUpdate(); 
          ps.close(); 
          fis.close(); 


          System.out.println("SQL save done"); 
        } 
        catch(SQLException e) 
        { 
          // if the error message is "out of memory", 
          // it probably means no database file is found 
          System.err.println(e.getMessage()); 
        } 
        finally 
        { 
          try 
          { 
            if(connection != null) 
              connection.close(); 
          } 

          catch(SQLException e) 
          { 
            // connection close failed. 
            System.err.println(e); 
          } 
        } 
      } 


    public void loadShellDB() throws ClassNotFoundException, SQLException, IOException { 
            Class.forName(classForName); 
        Connection conn = DriverManager.getConnection(connectionPath); 

        String sql = "SELECT name, shl FROM shell"; 
        PreparedStatement stmt = conn.prepareStatement(sql); 
        ResultSet resultSet = stmt.executeQuery(); 
        while (resultSet.next()) { 
          File shl = new File(fileOutputPath); 
          FileOutputStream fos = new FileOutputStream(shl); 

          byte[] buffer = new byte[1];         
          InputStream is = resultSet.getBinaryStream(2); 
          while (is.read(buffer) > 0) { 
            fos.write(buffer); 
          } 
          fos.close(); 
        } 
        conn.close(); 
        System.out.println("SQL Load Done"); 
      } 

Then, I just had to call them from my old save functions:

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"); 

    saveShellDB(); //here! 
} 

public Shell loadShell() throws FileNotFoundException, IOException, ClassNotFoundException { 
    loadShellDB(); //here! 
        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; 
}
Maslor
  • 1,821
  • 3
  • 20
  • 44