1.3.2.3.5. Blob/Clob操作

Blob新增保存例子代码如下:

public void testSaveBLob() throws BusinessException {
    Lobtest lob = new Lobtest();
    long id = 1L, start = System.currentTimeMillis(), end;
    String file = "e:\\MyProjects\\test.rar";
    lob.setTid(id);
    try {
        InputStream is = new FileInputStream(new File(file));
        byte[] bytes = IOUtility.toByteArray(is);1
        is.close();
        lob.setTblob(bytes);2
        DBAgent.save(lob);3
        end = System.currentTimeMillis();
        System.out.println("Saved:" + (end - start));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

1

将要保存的二进制数据转为byte数组

2

将byte数组设置到PO对象中

3

保存PO对象即可

Blob读取和更新例子代码如下:

public void testGetAndUpdateBLob() throws BusinessException {
    Lobtest lob = new Lobtest();
    long id = 1L, start = System.currentTimeMillis(), end;
    String file = "e:\\MyProjects\\test.rar";
    String ofile = "e:/test.rar";
    lob.setTid(id);
    try {
        lob = (Lobtest) DBAgent.get(Lobtest.class, id);
        byte[] bytes = lob.getTblob();1
        OutputStream os = new FileOutputStream(new File(ofile));
        IOUtility.copy(bytes, os);
        os.flush();
        os.close();
        end = System.currentTimeMillis();
        System.out.println("Readed:" + (end - start));
        start = end;

        InputStream is = new FileInputStream(new File(file));
        bytes = IOUtility.toByteArray(is);
        is.close();
        lob.setTblob(bytes);2
        DBAgent.update(lob);3
        end = System.currentTimeMillis();
        System.out.println("Updated:" + (end - start));
        start = end;
    } catch (Exception e) {
        e.printStackTrace();
    }
}

1

正常查询PO对象,获取Blob数据的byte数组

2

用新的byte数组更新Blob数据

3

更新保存PO即可更新Blob数据

Clob新增保存例子代码如下:

public void testSaveCLob() throws BusinessException {
    long id = 2L, size = 100000, start = System.currentTimeMillis(), end;
    Lobtest lob = new Lobtest();
    lob.setTid(id);
    StringBuffer sb = new StringBuffer((int) size);
    for (int i = 0; i < size; i++)
        sb.append('a');
    lob.setTclob(sb.toString());1
    DBAgent.save(lob);2
    end = System.currentTimeMillis();
    System.out.println("Saved:" + (end - start));
}

1

将要保存的Clob数据作为String传入

2

正常保存PO对象即可保存Clob数据

Clob读取和更新例子代码如下:

public void testGetAndUpdateCLob() throws BusinessException {
    long id = 2L, size = 100000, start = System.currentTimeMillis(), end;
    Lobtest lob = (Lobtest) DBAgent.get(Lobtest.class, id);
    end = System.currentTimeMillis();
    System.out.println("Readed:" + lob.getTclob().length() + ":" + (end - start));1
    start = end;
    StringBuffer sb = new StringBuffer((int) size);
    for (int i = 0; i < size; i++)
        sb.append('b');
    lob.setTclob(sb.toString());2
    DBAgent.update(lob);3
    System.out.println("Updated:" + (end - start));
    start = end;
}

1

正常查询PO对象,获取Clob数据作为String

2

用新的String数据更新Clob数据

3

更新保存PO即可更新Clob数据