2014-10-08 49 views
0

我使用HSQL 2.3.2和我有一個表的結構是這樣的:如何將二進制數據追加到HSQL(2.3.2)blob?

create table test (name varchar(255), data blob); 

我知道如何二進制數據插入到表:

// ... insert omitted ... 
PreparedStatement ps = connection.prepareStatement("update test set data = ? where name = 'chris'"); 

final Blob bb = connection.createBlob(); 

final OutputStream out = bb.setBinaryStream(bb.length()+1); 
out.write(("[test "+System.currentTimeMillis()+"]").getBytes("UTF-8")); 
out.flush(); 
out.close(); 

ps.setBlob(1,bb); 

ps.execute(); 

其可以使用PS .setBinaryStream(1,...);好吧。

但是如何添加/追加數據到現有的blob?我tryed:

Statement stmt = connection.createStatement(); 
ResultSet rs = stmt.executeQuery("select * from test where name = 'chris'"); 

if(rs.next()) 
{ 
    Blob b = rs.getBlob("data"); 
    if(b!=null) 
    { 
    System.out.println("Blob size: " + b.length()); 

    System.out.println("content: " + StringUtils.asString(b.getBinaryStream())); 

    final OutputStream out = b.setBinaryStream(b.length()); 
    out.write(("[test "+System.currentTimeMillis()+"]").getBytes("UTF-8")); 
    out.flush(); 
    out.close(); 

    System.out.println("Blob size after write: " + b.length()); 

} 
else 
{ 
    System.out.println("no blob found."); 
} 

,但我得到的是:

Blob size: 20 
content: [test 1412778939148] 
Exception in thread "main" java.sql.SQLFeatureNotSupportedException: feature not supported 
    at org.hsqldb.jdbc.JDBCUtil.notSupported(Unknown Source) 
    at org.hsqldb.jdbc.JDBCBlobClient.setBinaryStream(Unknown Source) 
    at aniclo.server.STest.main(STest.java:73) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:483) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) 

所以這意味着HSQL犯規支持附加的數據?或者我錯過了什麼? 似乎有一個JDBCBlobClient類,它提到它使用指向blob的指針而不是內存中的二進制數組表示。有人能告訴我如何與這個班級合作嗎?

我的應用程序必須支持附加數據。所以我現在可以想到的解決方法是創建一個表並將所有數據塊放在那裏(比如postgres LOB表),並且一旦我將所有的塊添加到一起並將它們移動到最終表中,在思考時就已經頭疼了它...

謝謝,

回答

0

HSQLDB支持更新和附加數據blob。而不是Blob.setBinaryStream(...)使用Blob.setBytes(...)。

相關問題