【问题标题】:How to append binary data to a HSQL(2.3.2) blob?如何将二进制数据附加到 HSQL(2.3.2) blob?
【发布时间】:2015-02-18 09:02:33
【问题描述】:

我正在使用 HSQL 2.3.2,并且我的表结构如下:

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? 我试过了:

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 表),一旦我得到所有块将它们附加在一起并将它们移动到最终表中,当想到它...

谢谢,

【问题讨论】:

    标签: java blob hsqldb


    【解决方案1】:

    HSQLDB 支持将数据更新和附加到 blob。而不是 Blob.setBinaryStream(...) 使用 Blob.setBytes(...)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-20
      • 2012-05-08
      • 1970-01-01
      • 2014-09-25
      • 2015-06-09
      • 1970-01-01
      • 2012-04-04
      相关资源
      最近更新 更多