代码之家  ›  专栏  ›  技术社区  ›  Aran

如何在java中将运行时生成的字符串写入postgresql blob[关闭]

  •  -1
  • Aran  · 技术社区  · 7 年前

    我期望的是:

     Open PostgreSQL connection
     For loop
      continuously save data to DB as blob
     End loop
     Close Connection
    
    1 回复  |  直到 7 年前
        1
  •  -1
  •   Aleh Maksimovich    7 年前

    请参阅 Chapter 7. Storing Binary Data PostgreSQL JDBC文档。他们的例子 LargeObject 似乎正是你想要的:

    // All LargeObject API calls must be within a transaction block
    conn.setAutoCommit(false);
    
    // Get the Large Object Manager to perform operations with
    LargeObjectManager lobj = ((org.postgresql.PGConnection)conn).getLargeObjectAPI();
    
    // Create a new large object
    int oid = lobj.create(LargeObjectManager.READ | LargeObjectManager.WRITE);
    
    // Later you will be able to access data loading it by `oid` identifier.
    // Or you can save it's value to some row in database
    
    // Open the large object for writing
    LargeObject obj = lobj.open(oid, LargeObjectManager.WRITE);
    
    // Do your job here and stream the content
    // Multiple obj.write calls expected
    
    // Close the large object
    obj.close();
    
    // Finally, commit the transaction.
    conn.commit();
    

    但一般来说,使用这种方法没有什么意义。将数据附加到本地文件(临时?)是一种更有效的方法。