我正在使用for循环(百万计数)在运行时生成一些数据。我喜欢将它作为单行blob保存到PostgreSQL DB中,因为在变量中保存所有字符串将占用更多RAM。我怎样才能在java中实现这一点。
我的期望是:
Open PostgreSQL connection For loop continuously save data to DB as blob End loop Close Connection
答案 0 :(得分:-1)
请参阅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();
但通常使用这种方法没什么意义。将数据附加到本地文件(临时?)是一种更有效的方法。