java 如何将运行时生成的字符串写入 postgresql blob

How to write runtime generated string into postgresql blob in java

我正在使用 for 循环(百万计数)在 运行 时间生成一些数据。我喜欢将它作为单行的 blob 保存到 PostgreSQL DB 中,因为将所有字符串保存在变量中会占用更多的 RAM。我怎样才能在 java.

中实现这一点

我期望的是:

 打开 PostgreSQL 连接
 For循环
  连续将数据作为 blob 保存到 DB
 结束循环
 关闭连接

请参阅 PostgreSQL JDBC 文档的 Chapter 7. Storing Binary Data。他们的 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();

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