hsqldb getBlob()始终返回长度为0的blob

时间:2013-04-16 16:47:45

标签: java database jdbc blob hsqldb

编辑: 好吧我有点“解决”它,我将图像对象存储为其他类型(可序列化) 在我的hsqldb中,我不会将此标记为已解决,因为原始问题仍然存在, 但这“解决了”我的问题,因为我现在能够存储我的图像并检索它们, 如果有人知道__ **我做错了什么,请善待我。

(这真是令人头疼,但是我仍然认为我没有做错任何事情,所以如果你知道分享的话请...-))

提前谢谢你!


我目前正在尝试使用以下代码将图像插入hsqldb:

String sql = "INSERT INTO IMAGE VALUES(NULL, ?, ?);";
        BlobInputStream bis = new BlobInputStream(i.getData());
    int x = 0;
    try {
        PreparedStatement pS = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
        pS.setBlob(1, bis);
        pS.setString(2, i.getAlt_text());
        pS.executeUpdate();
        ResultSet set = pS.getGeneratedKeys();
        set.next();
        x = set.getInt("ID");
        set.close();
        c.commit();
        return x;
    } catch (SQLException e) {
        throw new DataBackendException(e);
    }

BlobInputStream是我写的一个简单的Class,它符合输入流iface

    private class BlobInputStream extends InputStream {
    private final byte[] data;
    private int pos = -1;
    public BlobInputStream(byte[] data) {
        this.data=data;
    }
    @Override
    public int read() throws IOException {
        pos++;
        if(!(pos<data.length)) return -1;
        return (int)data[pos];
    }

}

一切都很好,我一直用这段代码测试它:

@Test
public void testImageStoreRecoverDelete() throws DataBackendException {




    byte[] oneMeg = new byte[152655];

    for(int i=0;i<oneMeg.length;i++) {
        oneMeg[i] = (byte)13;
    }


    Image i = new Image();
    i.setId(0);
    i.setData(oneMeg);
    i.setAlt_text("HEYHEYHEY");

    int id = layer.saveImage(i);
    i.setId(id);

    Image i2 = layer.getImage(id);


    assertEquals(i, i2);

    layer.deleteImage(id);

    Image i3 = layer.getImage(id);

    assertNull(i3);

}

(为Image对象实现了equals方法) 我用来检索图像的代码如下:

    @Override
public Image getImage(int id) throws DataBackendException {
    String sql = "SELECT * FROM IMAGE WHERE id="+id;

    try {
        ResultSet set = c.prepareCall(sql).executeQuery();
        if(!set.next()) return null;
        Image i = new Image();
        i.setId(set.getInt("ID"));
        i.setAlt_text(set.getString("ALTTEXT"));
        Blob b = set.getBlob("DATA");           
        i.setData(IOUtils.toByteArray(b.getBinaryStream()));
        set.close();
        b.free();
        return i;
    } catch (Exception e) {
        throw new DataBackendException(e);
    }
}

(IOUtils来自apache commons-io)

但问题是: 当我直接从我的驱动器读取图像并保存它时,blob总是以0字节的长度返回:

    public static void main(String[] args) throws Exception {
    FileInputStream iS = new FileInputStream(new File("/home/ubuser/Downloads/clouds.jpg"));
    byte[] data = IOUtils.toByteArray(iS);//read(iS);
    Image i = new Image();
    i.setData(data);

    ServiceLayer layer = DataBackendManager.getDataBackend().getServiceLayer();
    int id = layer.saveImage(i);
    print("id: "+id);
    Image i2 = layer.getImage(id);

    print("data: "+i2.getData());
    print("length: "+i2.getData().length);

    FileOutputStream stream = new FileOutputStream("/home/ubuser/Desktop/test.jpg");
    stream.write(i2.getData());
    stream.flush();
    stream.close();
}

输出:     id:1     数据:[B @ a39ab89     长度:0

哦,我的BLOB定义为:

创建表格图像(     由默认为身份生成的ID INTEGER(从1开始,由1递增)not null,     DATA BLOB(4M)not null,     ALTTEXT VARCHAR(35),     约束IID主键(ID) );

任何帮助将不胜感激: - )

0 个答案:

没有答案