簡體   English   中英

如何從sqlite存儲和檢索BLOB類型?

[英]How do I store and retrieve a BLOB type from 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方法。 我知道這個問題與我如何從sqlite中存儲和檢索blob非常相似,但其答案在C#中。 另外,我發現的大多數示例始終都使用特定的URL存儲圖片和圖像,沒有很多帶有對象的示例。

我知道了 基本上,我添加了兩個功能: 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; 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM