2017-10-28 242 views
-1

我正在使用for循環(百萬計數)在運行時生成一些數據。我喜歡將它作爲Blob單行保存到PostgreSQL數據庫中,因爲將所有字符串保存在變量中將佔用更多的RAM。我怎樣才能在java中實現這一點。如何將運行時生成的字符串寫入java中的postgresql blob

什麼,我想到的是:

 
Open PostgreSQL connection 
For loop 
    continuously save data to DB as blob 
End loop 
Close Connection 

回答

-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(); 

但一般有使用這種方法沒有什麼意義。將數據附加到本地文件(臨時?)是一種更高效的方法。

相關問題