【发布时间】: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 表),一旦我得到所有块将它们附加在一起并将它们移动到最终表中,当想到它...
谢谢,
【问题讨论】: