如何从sqlite存储和检索BLOB类型?

时间:2015-08-11 10:00:18

标签: java sql sqlite

我有一个java类( Shell ),它存储了我需要为我的应用程序加载的所有信息。目前,它被保存到.txt文件中并从所述文件加载。

此类负责保存和加载 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;
}

但是,现在我正在尝试分发应用程序,使用文件似乎不如使用数据库那么可行,所以我已经设置了SQLite。

由于 Shell 具有我需要的所有内容,因此我希望将其存储为BLOB并能够稍后检索该对象。我是数据库的新手,所以我不太了解我应该使用的具体java方法。我知道这个问题与How do i store and retrieve a blob from sqlite非常相似,但答案是在C#中。此外,我发现的大多数示例总是存储带有特定URL的图片和图像,没有很多带对象的示例。

1 个答案:

答案 0 :(得分:0)

我得到了它的工作。基本上我添加了两个函数:saveShellDBloadShellDB

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

然后,我只需要从旧的保存功能中调用它们:

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; 
}
相关问题